Page 1 of 1

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

Posted: Mon May 29, 2006 4:15 am
by Joakim Christiansen
What is the fastest way of cheking if a file exists?
I currently use this: If FileSize(File) > 0

Posted: Mon May 29, 2006 4:40 am
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.

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

Posted: Mon May 29, 2006 4:44 am
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........

Posted: Mon May 29, 2006 4:58 am
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).

Posted: Mon May 29, 2006 10:02 am
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

Posted: Mon May 29, 2006 10:25 am
by Fred
FileSize() does exactly that so it should be the fastest way.

Posted: Sun Sep 07, 2008 5:52 pm
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)

Posted: Wed Sep 10, 2008 1:37 pm
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.

Posted: Wed Sep 10, 2008 1:42 pm
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.

Posted: Wed Sep 10, 2008 1:52 pm
by Rescator

Posted: Wed Sep 10, 2008 2:10 pm
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")

Posted: Thu Sep 11, 2008 7:51 am
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:

Posted: Thu Sep 11, 2008 4:51 pm
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