Share your advanced PureBasic knowledge/code with the community.
RASHAD
PureBasic Expert
Posts: 4954 Joined: Sun Apr 12, 2009 6:27 am
Post
by RASHAD » Mon Mar 10, 2014 1:22 pm
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
Posts: 952 Joined: Mon Apr 28, 2003 2:22 pm
Location: Europe
Post
by Tranquil » Mon Mar 10, 2014 1:31 pm
Whats wrong with FileSize() PB command?
Return values:
-1 File not found
-2 File is a directory
Tranquil
Thunder93
Addict
Posts: 1788 Joined: Tue Mar 21, 2006 12:31 am
Location: Canada
Post
by Thunder93 » Mon Mar 10, 2014 1:32 pm
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
Posts: 7581 Joined: Fri Apr 25, 2003 5:24 pm
Post
by PB » Mon Mar 10, 2014 1:52 pm
> 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.
Thorsten1867
Addict
Posts: 1372 Joined: Wed Aug 24, 2005 4:02 pm
Location: Germany
Post
by Thorsten1867 » Mon Mar 10, 2014 1:53 pm
Code: Select all
Procedure FileExist(File.s)
If FileSize(File) >= 0
ProcedureReturn #True
EndIf
ProcedureReturn #False
EndProcedure
PB
PureBasic Expert
Posts: 7581 Joined: Fri Apr 25, 2003 5:24 pm
Post
by PB » Mon Mar 10, 2014 1:57 pm
See my post above.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Kiffi
Addict
Posts: 1504 Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9
Post
by Kiffi » Mon Mar 10, 2014 2:08 pm
PB wrote: Doesn't work correctly on 32-bit apps running on a 64-bit PC.
uh, not good! Do you have a link?
Greetings ... Kiffi
Hygge
PB
PureBasic Expert
Posts: 7581 Joined: Fri Apr 25, 2003 5:24 pm
Post
by PB » Mon Mar 10, 2014 2:10 pm
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Kiffi
Addict
Posts: 1504 Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9
Post
by Kiffi » Mon Mar 10, 2014 2:35 pm
thx!
Hygge
RASHAD
PureBasic Expert
Posts: 4954 Joined: Sun Apr 12, 2009 6:27 am
Post
by RASHAD » Mon Mar 10, 2014 3:16 pm
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
Thorsten1867
Addict
Posts: 1372 Joined: Wed Aug 24, 2005 4:02 pm
Location: Germany
Post
by Thorsten1867 » Mon Mar 10, 2014 3:43 pm
PB wrote: See my post above.
I use it in all my 32Bit programs under Windows 7 (64Bit)
PB
PureBasic Expert
Posts: 7581 Joined: Fri Apr 25, 2003 5:24 pm
Post
by PB » Mon Mar 10, 2014 3:56 pm
> 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
Posts: 532 Joined: Sat Sep 11, 2004 11:54 am
Location: UK
Post
by RichardL » Mon Mar 10, 2014 6:26 pm
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
Thunder93
Addict
Posts: 1788 Joined: Tue Mar 21, 2006 12:31 am
Location: Canada
Post
by Thunder93 » Mon Mar 10, 2014 6:47 pm
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