Page 1 of 1

file thing

Posted: Mon Aug 14, 2006 8:57 am
by Seb McClouth
Hey

I need to do some file stuff...

I need to create a new file... but if it exists... I want to add a new line...

I don't have anything this far yet...

grtz

Posted: Mon Aug 14, 2006 9:10 am
by Z!re

Code: Select all

open "file" for append as #1

Posted: Mon Aug 14, 2006 9:15 am
by Seb McClouth
How can someone check if the file is new or existing?

Posted: Mon Aug 14, 2006 10:16 am
by Z!re
Seb McClouth wrote:How can someone check if the file is new or existing?
DIR$()
Or just check if the file is 0 bytes long.. Does it really matter?

Posted: Mon Aug 14, 2006 11:36 am
by sid6.7
bummer i hate not being at home(2 more weeks)


ask MONEO he made me a peice of code for
our DEEBEE program that checks for an
existing file...worked great!....

Posted: Mon Aug 14, 2006 12:35 pm
by Seb McClouth
it matters because the program is deleting a file and recreating it... for that reason I need.

Thx Sid I'll ask him.

Posted: Mon Aug 14, 2006 7:09 pm
by moneo
**********************************************************
Hey Moneo
Hows you? Me doing great.
I have question and Sid6.7 told me you might be some help.
For a program I need to remove a file, create a new one, and add lines to the file.
But the adding thing is kinda blury, because first I need a check to see if the file is empty or already contains some text. If it does contain text I need to put the new text after the old one.
You think you can help me on this one?
grtz
Seb
**********************************************************
Hi Seb,
I got your above message on PM. I'm posting my reply here because other people have already posted stuff.

In your first post you said: "I need to create a new file... but if it exists... I want to add a new line..."

In your last post you also said: "it matters because the program is deleting a file and recreating it... for that reason I need."

Sid6.7 says I helped on the DEEBEE program to check for an existing file. I checked this program and there's no "file exist" logic there. I wrote a subroutine to "delete" a file by opening it for output and closing it, which makes it a zero-length-file.

Anyway, these are the operations that I understand you want to perform:

1) REMOVE (OR DELETE) A FILE.
If you know the filename and you definitely want to delete it, whether it exists or not, do this:
OPEN "file" FOR OUTPUT AS #9
CLOSE #9
KILL "file"

The OPEN FOR OUTPUT makes it a zero-length-file, which exists for the KILL.

2) CREATE A NEW FILE.
If you definitely want to create this file as a new file, whether it already exists of not, and whether it already has data on it or not, do this:
OPEN "file" FOR OUTPUT AS #9

3) ADD LINES TO AN EXISTING FILE.
If you want to add lines to an existing file, whether or not the file already exists, do this:
OPEN "file" FOR APPEND AS #9

Except for the DELETE, you could probably use the OPEN FOR APPEND for all the other cases. I think that's what Z!re was trying to tell you.

In summary, Based on what you say you want to do, I don't think you need to make any decision regarding whether the file exists or not. File exist logic can get hairy, and the only good way to do it is to use assembler logic.

Hope that helps..... Regards..... Moneo

Posted: Tue Aug 15, 2006 12:42 am
by Seb McClouth
thx everybody...
Moneo you explaining did the trick. I've implented the code and it works just like I wanted it too.

grtz