What is the fastest way of cheking if a file exists?

Just starting out? Need help? Post your questions and find answers here.
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

What is the fastest way of cheking if a file exists?

Post by Joakim Christiansen »

What is the fastest way of cheking if a file exists?
I currently use this: If FileSize(File) > 0
I like logic, hence I dislike humans but love computers.
User avatar
travismcgee
New User
New User
Posts: 9
Joined: Mon May 29, 2006 2:16 am

Post by travismcgee »

Here's a line from one of my programs, dunno if you'll like it or not:

Code: Select all

If ExamineDirectory(0,GetCurrentDirectory(),"Sqlite3.dll") = 0
  ; it isn't there, handle condition
Else
  ; it is there, handle condition
Endif
I have no idea how fast it is.
Phoenix
Enthusiast
Enthusiast
Posts: 141
Joined: Sun Sep 04, 2005 2:25 am

Re: What is the fastest way of cheking if a file exists?

Post by Phoenix »

Joakim Christiansen wrote:What is the fastest way of cheking if a file exists?
I currently use this: If FileSize(File) > 0
Better check for >-1 because a file can be a size of 0 bytes........
User avatar
travismcgee
New User
New User
Posts: 9
Joined: Mon May 29, 2006 2:16 am

Post by travismcgee »

Yes, the way you're already doing it is probably better than what I got. And as the docs say that a return value of -1 means file not found, it seems like the best approach (with phoenix's note applied).
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Post by gnozal »

Windows only but fast.

Code: Select all

Procedure FileExists(FileName.s) 
  Protected Result.l, Find.WIN32_FIND_DATA 
  Result = FindFirstFile_(@FileName, Find) 
  FindClose_(Result) 
  If Result <> #INVALID_HANDLE_VALUE 
    ProcedureReturn #True 
  EndIf 
  ProcedureReturn #False 
EndProcedure
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

FileSize() does exactly that so it should be the fastest way.
User avatar
bingo
Enthusiast
Enthusiast
Posts: 210
Joined: Fri Apr 02, 2004 12:21 pm
Location: germany/thueringen
Contact:

Post by bingo »

Code: Select all

Import "ntdll.lib"
RtlDoesFileExists_U(file.p-unicode)
EndImport

Debug RtlDoesFileExists_U("c:\your.file")
... the fastest way (xp/vista) 8)
["1:0>1"]
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Post by Rescator »

Yeah, but that one is not documented in the PSDK and not guaranteed to exist in the future at all or OS variants. Use with caution.
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post by Fluid Byte »

Actually it's not documented anywhere. Only found 591 sites on Google wich is very rare and the over the half is in related to ReactOS and Wine.
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Post by Rescator »

PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

Maybe this is fastest?

Code: Select all

Debug PathFileExists_("c:\program files\internet explorer\iexplore.exe")
Debug PathFileExists_("c:\program files\internet explorer\iexplore.exee")
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Post by Shardik »

Rescator wrote:Yeah, but that one is not documented in the PSDK and not guaranteed to exist in the future at all or OS variants. Use with caution.
That's correct. Nevertheless bingo's code even works with WinNT4 SP6... :wink:
RichardL
Enthusiast
Enthusiast
Posts: 532
Joined: Sat Sep 11, 2004 11:54 am
Location: UK

Post by RichardL »

I wrote and have used this for many years and it has never thrown a wobbly... Windoze only.

Code: Select all

Procedure.l Exist(File$)                               ;- Check a drive + file exists, without system requesters etc.
  ; Check if a drive or drive+file exists
  ; Return -1 if exists, else 0
  ; *** Never *** displays a system error even if device is missing
  ; such as a multi card interface with no cards plugged in.
  
  Protected EFlag.l, OldErrorMode.l, Junk.l
  
  OldErrorMode = SetErrorMode_(1)   ; Turn off screen error messages
  If GetFileAttributes_(@File$)=-1  ; Get file butes. -1 = fail
    Junk.l=GetLastError_()          ; Get last error, to flush system
    SetLastError_(0)                ; Set error to zero
    EFlag.l = 0                     ; Return value to flag FAIL
  Else
    EFlag.l = -1                    ; Return value to flag a PASS
  EndIf
  SetErrorMode_(OldErrorMode)       ; Reset the error flags
  ProcedureReturn EFlag
EndProcedure
Post Reply