How to check if a file exist?

Just starting out? Need help? Post your questions and find answers here.
Hurga
Enthusiast
Enthusiast
Posts: 148
Joined: Thu Jul 17, 2003 2:53 pm
Contact:

How to check if a file exist?

Post by Hurga »

Hi
I m a complete newby to purebasic. And i cant find a commmand to check if a file is already existing or not
Can s o help me please?

Thx
Pupil
Enthusiast
Enthusiast
Posts: 715
Joined: Fri Apr 25, 2003 3:56 pm

Post by Pupil »

Check out the FileSize() command..
User avatar
wichtel
User
User
Posts: 71
Joined: Fri May 02, 2003 11:14 am
Location: Germany
Contact:

Post by wichtel »

Code: Select all

If ReadFile(0,filenam$)
  ; file exists
Else
  ; file does not exist
EndIf
PB 5.40 LTS, W7,8,10 64bit and Mint x64
Berikco
Administrator
Administrator
Posts: 1326
Joined: Wed Apr 23, 2003 7:57 pm
Location: Belgium
Contact:

Post by Berikco »

no need to open the file for reading

Code: Select all

If FileSize(FileName$) = -1
; file does not exist
endif
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

Or:

To check for a file:

Code: Select all

If ExamineDirectory(1, ".", filename$) = 0
    ; file does not exist
Else
    ; file exists
EndIf
To check for a folder:

Code: Select all

If ExamineDirectory(1, foldername$, "*.*") = 0
    ; folder does not exist
Else
    ; folder exists
EndIf
:twisted:
--Kale

Image
Doobrey
Enthusiast
Enthusiast
Posts: 218
Joined: Sat Apr 26, 2003 4:47 am
Location: Dullsville..population: me
Contact:

Post by Doobrey »

wichtel wrote:

Code: Select all

If ReadFile(0,filenam$)
  ; file exists
Else
  ; file does not exist
EndIf
Nope..that routine could fail if another app has already opened the file in exclusive mode..
User avatar
wichtel
User
User
Posts: 71
Joined: Fri May 02, 2003 11:14 am
Location: Germany
Contact:

Post by wichtel »

learned :)

I prefer now Kale's version.
PB 5.40 LTS, W7,8,10 64bit and Mint x64
Fred
Administrator
Administrator
Posts: 16617
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

The preferred way is the FileSize() one (faster, smaller..).
StarWarsFan
Enthusiast
Enthusiast
Posts: 169
Joined: Sat Mar 14, 2015 11:53 am

Re: How to check if a file or folder exists?

Post by StarWarsFan »

I wrote two short and simple (1 line!) procedures to do both, maybe it will be useful for somebody:

Code: Select all

Procedure ExistDIR(dir$) : ProcedureReturn ExamineDirectory(#PB_Any,dir$,"") : EndProcedure
Procedure ExistFILE(file$) : If FileSize(file$)>-1 : r=#True : Else : r=#False : EndIf : ProcedureReturn r : EndProcedure
Cheers!
Image - There is usually a lot of "try this, maybe do that" but ONLY an example that one can test for themself and get an immediate result actually brings people forward.
User avatar
NicTheQuick
Addict
Addict
Posts: 1224
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: How to check if a file or folder exists?

Post by NicTheQuick »

StarWarsFan wrote: Mon Sep 06, 2021 10:23 am I wrote two short and simple (1 line!) procedures to do both, maybe it will be useful for somebody:

Code: Select all

Procedure ExistDIR(dir$) : ProcedureReturn ExamineDirectory(#PB_Any,dir$,"") : EndProcedure
Procedure ExistFILE(file$) : If FileSize(file$)>-1 : r=#True : Else : r=#False : EndIf : ProcedureReturn r : EndProcedure
Cheers!
If you examine a directory you always have to finish it too with FinishDirectory(). You just created a memory leak.

Also if you read the help you can see that FileSize returns -2 if the path is a directory. So there is really no need for ExamineDirectory in the first place.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
Kiffi
Addict
Addict
Posts: 1353
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: How to check if a file or folder exists?

Post by Kiffi »

NicTheQuick wrote: Mon Sep 06, 2021 10:57 am
StarWarsFan wrote: Mon Sep 06, 2021 10:23 am...
...
plus the unnecessary use of the variable 'r'.
Hygge
highend
Enthusiast
Enthusiast
Posts: 123
Joined: Tue Jun 17, 2014 4:49 pm

Re: How to check if a file exist?

Post by highend »

So there is really no need for ExamineDirectory in the first place
There can be?

Try to find out (e.g. on Windows 7) if a file or folder in an overlong path exists.

FileSize() will report that it does not while ExamineDirectory() shows the correct result

Ofc the path is prefixed with "\\?\" | "\\?\UNC" (on a local / UNC path)
Cyllceaux
Enthusiast
Enthusiast
Posts: 458
Joined: Mon Jun 23, 2014 1:18 pm
Contact:

Re: How to check if a file or folder exists?

Post by Cyllceaux »

StarWarsFan wrote: Mon Sep 06, 2021 10:23 am I wrote two short and simple (1 line!) procedures to do both, maybe it will be useful for somebody:

Code: Select all

Procedure ExistDIR(dir$) : ProcedureReturn ExamineDirectory(#PB_Any,dir$,"") : EndProcedure
Procedure ExistFILE(file$) : If FileSize(file$)>-1 : r=#True : Else : r=#False : EndIf : ProcedureReturn r : EndProcedure
Cheers!
A little bit better

Code: Select all

Procedure ExistDIR(dir$) : ProcedureReturn Bool(FileSize(dir$) = -2): EndProcedure
Procedure ExistFILE(file$) : ProcedureReturn Bool(FileSize(file$) > -1) : EndProcedure
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: How to check if a file exist?

Post by mk-soft »

Macro is enough ...

Code: Select all

Macro ExistsDirectory(_directory_)
  Bool(FileSize(_directory_) = -2)
EndMacro

Macro ExistsFile(_file_)
  Bool(FileSize(_file_) > -1)
EndMacro
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Post Reply