Author |
Topic: NEW!! Easy to use file exists tool (Read 645 times) |
|
michael
Senior Member
member is offline


Posts: 335
|
 |
NEW!! Easy to use file exists tool
« Thread started on: Jun 12th, 2016, 1:46pm » |
|
This was a last minute thought and I just had to make this happen as it is also required in my automatic renaming tool that must keep checking for a file and renaming it after it appears .. ( for the HEXAGON 3D stuff)
I didn't have much time to slap this together. Perhaps someone would like to improve it.. Keep in mind if you add code that shapes the final product, I will add your name on the final program as a contributor when it is perfected. I plan to put the final product on my forum and on Richards forum, as it technically should be a non windows platform tool also..
I tested it on a file that exists and it works .. so its all good..
And after this is done I would like to make a file listing program that is non windows based. ( perhaps a private file tracking tool) Code:
result$= FNcheckfile("junky.txt")
PRINT "file exists status: ";result$
END
DEF FNcheckfile(name$)
LOCAL event$,pass%
event$="":pass%=0
ON ERROR LOCAL event$="no":PRINT "it doesnt exist":pass%=1
IF pass%=0 THEN A=OPENIN(@usr$+name$)
REM apparently the created file wont return an error until you try to read or write to it.
IF pass%=0 THEN INPUT#A,a$
IF pass%=0 THEN CLOSE#A
IF pass%=0 THEN event$="yes"
ON ERROR OFF
=event$
|
« Last Edit: Jun 12th, 2016, 2:11pm by michael » |
Logged
|
I like making program generators and like reinventing the wheel
|
|
|
David Williams
Developer
member is offline

meh

Gender: 
Posts: 452
|
 |
Re: NEW!! Easy to use file exists tool
« Reply #1 on: Jun 12th, 2016, 2:53pm » |
|
on Jun 12th, 2016, 1:46pm, michael wrote: This was a last minute thought and I just had to make this happen as it is also required in my automatic renaming tool that must keep checking for a file and renaming it after it appears .. ( for the HEXAGON 3D stuff)
I didn't have much time to slap this together. Perhaps someone would like to improve it.. Keep in mind if you add code that shapes the final product, I will add your name on the final program as a contributor when it is perfected. I plan to put the final product on my forum and on Richards forum, as it technically should be a non windows platform tool also..
I tested it on a file that exists and it works .. so its all good..
And after this is done I would like to make a file listing program that is non windows based. ( perhaps a private file tracking tool) Code:
result$= FNcheckfile("junky.txt")
PRINT "file exists status: ";result$
END
DEF FNcheckfile(name$)
LOCAL event$,pass%
event$="":pass%=0
ON ERROR LOCAL event$="no":PRINT "it doesnt exist":pass%=1
IF pass%=0 THEN A=OPENIN(@usr$+name$)
REM apparently the created file wont return an error until you try to read or write to it.
IF pass%=0 THEN INPUT#A,a$
IF pass%=0 THEN CLOSE#A
IF pass%=0 THEN event$="yes"
ON ERROR OFF
=event$
|
|
I hope I'm not nitpicking, but using all those IF pass%=0 THEN statements is a little inefficient. You could put them all into a single IF...THEN block:
Code:
DEF FNcheckfile(name$)
LOCAL event$,pass%
event$="":pass%=0
ON ERROR LOCAL event$="no":PRINT "it doesnt exist":pass%=1
IF pass%=0 THEN
A=OPENIN(@usr$+name$)
INPUT#A,a$
CLOSE#A
event$="yes"
ENDIF
ON ERROR OFF
=event$
|
|
Logged
|
|
|
|
michael
Senior Member
member is offline


Posts: 335
|
 |
Re: NEW!! Easy to use file exists tool
« Reply #2 on: Jun 13th, 2016, 05:14am » |
|
Thanks for that. It does take me a few edits to get to that point. I am a slow programmer, even though I post a lot of programs here.. But I do plan to attempt to make better tools. We need a organized list of tools that really get the job done well for everyone that visits this forum looking for easy tools. I also plan to modify the WAV snippets that Richard made so they are tools also. Who knows, maybe he might reopen BBC later and make the tools into new commands for BBC. It all would depend on the interest that people show.
|
« Last Edit: Jun 13th, 2016, 05:15am by michael » |
Logged
|
I like making program generators and like reinventing the wheel
|
|
|
DDRM
Administrator
member is offline


Gender: 
Posts: 321
|
 |
Re: NEW!! Easy to use file exists tool
« Reply #3 on: Jun 13th, 2016, 08:09am » |
|
Hi Michael,
The manual says OPENIN returns 0 (rather than an error) if the file does not exist, and a quick check confirms that here. That's why it doesn't return an error until you try to read or write to the file. That's tricky in itself, since you may not always know what the content of the file is, so you may not know what to try to read.
Your routine will return after one pass through - so you don't really need pass% at all - you could just use A (which should probably be local).
Code:
DEFFNcheckfile(name$)
LOCAL a%
a%=OPENIN(name$)
IF a%=0 THEN ="no" ELSE CLOSE#a%:="yes"
Note that I've closed the file again once I've checked it, since otherwise if you try to open it later you will get a new channel number and the old one is going to get left open unless you are careful, since you won't have a handle to close it with.
Note also that I HAVEN'T closed the file if the command fails (a%=0), since CLOSE#0 will close ALL open files, which may well not be what you want to happen....
OPENIN will allow multichannel opening, but if you used OPENUP (allowing write as well as read access), a second OPENUP will fail...
Hope that's helpful.
D
|
|
Logged
|
|
|
|
michael
Senior Member
member is offline


Posts: 335
|
 |
Re: NEW!! Easy to use file exists tool
« Reply #4 on: Jun 13th, 2016, 3:28pm » |
|
Here is Richards solution.
Quote:A 'file exists' function in BBC BASIC is straightforward:
DEF FNfile_exists(f$) LOCAL F% F% = OPENIN(f$) IF F% CLOSE #F% : = TRUE = FALSE |
|
|
|
Logged
|
I like making program generators and like reinventing the wheel
|
|
|
|