How to check if a file exist? Newbie

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
rkn704
Newbie
Posts: 3
Joined: Tue Apr 21, 2015 7:28 am
Location: Chippenham. UK

How to check if a file exist? Newbie

Post by rkn704 »

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.
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Re: How to check if a file exist? Newbie

Post by burger2227 »

No ERROR necessary:

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  
Usage in a program:

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
QB64 has the _FILEEXISTS function that does the same thing.
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
rkn704
Newbie
Posts: 3
Joined: Tue Apr 21, 2015 7:28 am
Location: Chippenham. UK

Re: How to check if a file exist? Newbie

Post by rkn704 »

Thanks for the reply. Looks like it is time to lean about Functions, I will give it a go.
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Re: How to check if a file exist? Newbie

Post by burger2227 »

SUB and FUNCTION procedures are placed after the end of code in a program.

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  
Qbasic will compile it and DECLARE them for you. QB64 does not require any DECLARE statements.

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
rkn704
Newbie
Posts: 3
Joined: Tue Apr 21, 2015 7:28 am
Location: Chippenham. UK

Re: How to check if a file exist? Newbie

Post by rkn704 »

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
Post Reply