IsFileExist [Windows]

Share your advanced PureBasic knowledge/code with the community.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

IsFileExist [Windows]

Post by RASHAD »

Find if file exist for windows x86 - x64

Code: Select all

;=======================================
; Snippet:          IsFileExist;
; Author:           RASHAD
; Date:              Mar 10, 2014
; Target OS:      Windows
;=======================================
 
 Procedure IsFileExist(File$) 
     OpenLibrary(0, "Kernel32.dll")
     GetFunction(0, "IsWow64Process")
     CallFunction(0,"IsWow64Process",GetCurrentProcess_(), @IsWow64ProcessFlag)
 If IsWow64ProcessFlag <> 0 
     CompilerIf  #PB_Compiler_Processor =  #PB_Processor_x86
         wfd.WIN32_FIND_DATA
         GetFunction(0, "Wow64DisableWow64FsRedirection") 
         CallFunction(0,"Wow64DisableWow64FsRedirection",Flag)
         hFile = FindFirstFile_(@File$,wfd)
         FindClose_(hFile)
         GetFunction(0, "Wow64RevertWow64FsRedirection")
         CallFunction(0,"Wow64RevertWow64FsRedirection",Flag)
         CloseLibrary(0)
      CompilerEndIf
 Else 
       hFile = FileSize(File$)
 EndIf
 
If hFile > 0
    Debug "File exist"
 Else
    Debug "No"
 EndIf
 EndProcedure
 
 IsFileExist("C:\Windows\System32\mblctr.exe")

Egypt my love
Tranquil
Addict
Addict
Posts: 952
Joined: Mon Apr 28, 2003 2:22 pm
Location: Europe

Re: IsFileExist [Windows]

Post by Tranquil »

Whats wrong with FileSize() PB command?

Return values:

-1 File not found
-2 File is a directory
Tranquil
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: IsFileExist [Windows]

Post by Thunder93 »

heh.
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: IsFileExist [Windows]

Post by PB »

> Whats wrong with FileSize() PB command?

Doesn't work correctly on 32-bit apps running on a 64-bit PC.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
Thorsten1867
Addict
Addict
Posts: 1372
Joined: Wed Aug 24, 2005 4:02 pm
Location: Germany

Re: IsFileExist [Windows]

Post by Thorsten1867 »

Code: Select all

Procedure FileExist(File.s)
  If FileSize(File) >= 0
    ProcedureReturn #True
  EndIf
  ProcedureReturn #False
EndProcedure
Translated with http://www.DeepL.com/Translator

Download of PureBasic - Modules
Download of PureBasic - Programs

[Windows 11 x64] [PB V5.7x]
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: IsFileExist [Windows]

Post by PB »

See my post above. :)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
Kiffi
Addict
Addict
Posts: 1504
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: IsFileExist [Windows]

Post by Kiffi »

PB wrote:Doesn't work correctly on 32-bit apps running on a 64-bit PC.
:shock: uh, not good! Do you have a link?

Greetings ... Kiffi
Hygge
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: IsFileExist [Windows]

Post by PB »

I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
Kiffi
Addict
Addict
Posts: 1504
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: IsFileExist [Windows]

Post by Kiffi »

thx!
Hygge
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Re: IsFileExist [Windows]

Post by RASHAD »

Code: Select all

;==================================================
; Snippet:          IsFileExist;
; Author:           RASHAD
; Date:              Mar 10, 2014
; Target OS:      Windows
;==================================================
 
 Procedure IsFileExist(File$) 
     OpenLibrary(0, "Kernel32.dll")
     GetFunction(0, "IsWow64Process")
     CallFunction(0,"IsWow64Process",GetCurrentProcess_(), @IsWow64ProcessFlag)
 If IsWow64ProcessFlag <> 0 And SizeOf(Integer) = 4
         wfd.WIN32_FIND_DATA
         GetFunction(0, "Wow64DisableWow64FsRedirection") 
         CallFunction(0,"Wow64DisableWow64FsRedirection",Flag)
         hFile = FindFirstFile_(@File$,wfd)
         FindClose_(hFile)
         GetFunction(0, "Wow64RevertWow64FsRedirection")
         CallFunction(0,"Wow64RevertWow64FsRedirection",Flag)
 Else 
       hFile = FileSize(File$)
 EndIf
 If IsLibrary(0)
     CloseLibrary(0)
 EndIf 
 If hFile > 0
    Debug "File exist"
 Else
    Debug "No"
 EndIf
 EndProcedure
 
 IsFileExist("C:\Windows\System32\mblctr.exe")

Egypt my love
User avatar
Thorsten1867
Addict
Addict
Posts: 1372
Joined: Wed Aug 24, 2005 4:02 pm
Location: Germany

Re: IsFileExist [Windows]

Post by Thorsten1867 »

PB wrote:See my post above. :)
I use it in all my 32Bit programs under Windows 7 (64Bit)
Translated with http://www.DeepL.com/Translator

Download of PureBasic - Modules
Download of PureBasic - Programs

[Windows 11 x64] [PB V5.7x]
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: IsFileExist [Windows]

Post by PB »

> I use it in all my 32Bit programs under Windows 7 (64Bit)

It only affects system files, as proven in my other thread.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
RichardL
Enthusiast
Enthusiast
Posts: 532
Joined: Sat Sep 11, 2004 11:54 am
Location: UK

Re: IsFileExist [Windows]

Post by RichardL »

Hi,
I have used this for some years... I think I brought it across from my GFA days.
Never tested on 64 bit tho'
Can be used to test for drives, directories or files.

RichardL

Code: Select all

Procedure Exist(File$)                           ; Check a drive/directory/file exists, without system requesters etc.
  Protected EFlag.l, OldErrorMode.l, Junk.l
  
  OldErrorMode = SetErrorMode_(1)   ; Turn off screen error messages
  If GetFileAttributes_(@File$)=-1  ; (-1 = Fail)
    Junk.l=GetLastError_()          ; Get last error, to flush system
    SetLastError_(0)                ; Set error to zero
    EFlag = #False                  ; Return value to flag FAIL
  Else
    EFlag = #True                   ; Return value to flag a PASS
  EndIf
  SetErrorMode_(OldErrorMode)       ; Reset the error flags
  ProcedureReturn EFlag
EndProcedure
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: IsFileExist [Windows]

Post by Thunder93 »

Hi RichardL. Yours works but not in the way the OP version was made to.
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
Post Reply