Page 1 of 1

IsFileExist [Windows]

Posted: Mon Mar 10, 2014 1:22 pm
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")


Re: IsFileExist [Windows]

Posted: Mon Mar 10, 2014 1:31 pm
by Tranquil
Whats wrong with FileSize() PB command?

Return values:

-1 File not found
-2 File is a directory

Re: IsFileExist [Windows]

Posted: Mon Mar 10, 2014 1:32 pm
by Thunder93
heh.

Re: IsFileExist [Windows]

Posted: Mon Mar 10, 2014 1:52 pm
by PB
> Whats wrong with FileSize() PB command?

Doesn't work correctly on 32-bit apps running on a 64-bit PC.

Re: IsFileExist [Windows]

Posted: Mon Mar 10, 2014 1:53 pm
by Thorsten1867

Code: Select all

Procedure FileExist(File.s)
  If FileSize(File) >= 0
    ProcedureReturn #True
  EndIf
  ProcedureReturn #False
EndProcedure

Re: IsFileExist [Windows]

Posted: Mon Mar 10, 2014 1:57 pm
by PB
See my post above. :)

Re: IsFileExist [Windows]

Posted: Mon Mar 10, 2014 2:08 pm
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

Re: IsFileExist [Windows]

Posted: Mon Mar 10, 2014 2:10 pm
by PB

Re: IsFileExist [Windows]

Posted: Mon Mar 10, 2014 2:35 pm
by Kiffi
thx!

Re: IsFileExist [Windows]

Posted: Mon Mar 10, 2014 3:16 pm
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")


Re: IsFileExist [Windows]

Posted: Mon Mar 10, 2014 3:43 pm
by Thorsten1867
PB wrote:See my post above. :)
I use it in all my 32Bit programs under Windows 7 (64Bit)

Re: IsFileExist [Windows]

Posted: Mon Mar 10, 2014 3:56 pm
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.

Re: IsFileExist [Windows]

Posted: Mon Mar 10, 2014 6:26 pm
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

Re: IsFileExist [Windows]

Posted: Mon Mar 10, 2014 6:47 pm
by Thunder93
Hi RichardL. Yours works but not in the way the OP version was made to.