I want to delete a file if it already exists. There will be occasions when it does not exist. Is there a neat way of doing this.
I propose to use the KILL command to delete the file(s) and if a 'File not Found' error is returned use the ON ERROR RESUME NEXT to move on to the next command/function.
Thanks in advance.
How to check if a file exist? Newbie
- burger2227
- Veteran
- Posts: 2466
- Joined: Mon Aug 21, 2006 12:40 am
- Location: Pittsburgh, PA
Re: How to check if a file exist? Newbie
No ERROR necessary:
Usage in a program:
QB64 has the _FILEEXISTS function that does the same thing.
Code: Select all
FUNCTION Exist% (filename$)
f% = FREEFILE
OPEN filename$ FOR APPEND AS #f%
IF LOF(f%) THEN Exist% = -1 ELSE Exist% = 0: CLOSE #f%: KILL filename$ 'delete empty files
CLOSE #f%
END FUNCTION
Code: Select all
INPUT "Enter a file name: ", file$
IF Exist%(file$) THEN OPEN file$ FOR INPUT AS #1: found% = -1 'function call demo
CLOSE #1
IF found% THEN PRINT "File exists!" ELSE PRINT "File not found!"
END
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
Re: How to check if a file exist? Newbie
Thanks for the reply. Looks like it is time to lean about Functions, I will give it a go.
- burger2227
- Veteran
- Posts: 2466
- Joined: Mon Aug 21, 2006 12:40 am
- Location: Pittsburgh, PA
Re: How to check if a file exist? Newbie
SUB and FUNCTION procedures are placed after the end of code in a program.
Qbasic will compile it and DECLARE them for you. QB64 does not require any DECLARE statements.
http://www.qb64.net/wiki/index.php/FILEEXISTS
Code: Select all
INPUT "Enter a file name: ", file$
IF Exist%(file$) THEN OPEN file$ FOR INPUT AS #1: found% = -1 'function call demo
CLOSE #1
IF found% THEN PRINT "File exists!" ELSE PRINT "File not found!"
END
FUNCTION Exist% (filename$)
f% = FREEFILE
OPEN filename$ FOR APPEND AS #f%
IF LOF(f%) THEN Exist% = -1 ELSE Exist% = 0: CLOSE #f%: KILL filename$ 'delete empty files
CLOSE #f%
END FUNCTION
http://www.qb64.net/wiki/index.php/FILEEXISTS
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
Re: How to check if a file exist? Newbie
Thanks for the assistance. I did have to add another CLOSE #F%
Code: Select all
FUNCTION EXIST% (FILENAME$)
F% = FREEFILE
OPEN FILENAME$ FOR APPEND AS #F%
IF LOF(F%) THEN
EXIST% = -1
CLOSE #F% ' without this I got 'File already Open' if the file did exist.
ELSE
EXIST% = 0
CLOSE #F%
KILL FILENAME$
CLOSE #F%
END IF
END FUNCTION