Hey all,
Is there any FileExist function? I can't see it in WIN32.HLP, and it would be really great if I could even use wildcards with this. For example, if there are any .bak files present, delete them all. I can achieve the same effect via batch files, but it's very messy. Any help appreciated!
Thanks,
-brixton
No, I havn't seen one, but this will tell you if a file exists:
FileExists PROC pszFile:DWORD
invoke CreateFile, pszFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL
.if eax!=INVALID_HANDLE_VALUE
invoke CloseHandle, eax
; return TRUE
mov eax, TRUE
ret
.endif
; return FALSE
xor eax, eax
ret
FileExists ENDP
If you need to use wild cards, try using the FindFirstFile, FindNextFile and FindClose combination.
Ossa
In the MASM32 Library there is:
exist
exist proc lpszFileName:DWORD
Description
This function tests if a file exists at the path and file name in the zero terminated string.
Parameter
1. lpszFileName The zero terminated string that has the path & name of the file to test.
Return Value
If the return value in eax is zero, the file does not exist, if it is 1, then it exists.
Regards, P1 :8)
Hey P1, long time, no chat.
I don't think I can use wildcards with the MASM32 'exist' procedure, but it's a useful one for other situations!
For the record, Ossa's FindFirstFile is absolutely perfect. There's only 1 of the specified filetype, but I don't know the filename. GetFirstFile works perfectly using *.bak (for example).
Thanks for your interest,
-brixton
Quote from: brixton on June 05, 2006, 08:48:17 PM
Hey P1, long time, no chat.
Well, will you look at that. It's an antique british chatter. A rare find mate. Back from .... :green2
Regards, P1 :8)
I don't think I understand your humour but.. Right back at ya :dance: :lol
Got the problem solved by-the-way, I had some problems because I didn't FindClose so eventually the search handle went to the roof until it resulted in an error - thus claiming a file didn't exist when it really did (I was checking for -1 on return).
exist proc lpszFileName:DWORD
LOCAL wfd :WIN32_FIND_DATA
invoke FindFirstFile,lpszFileName,ADDR wfd
invoke FindClose,eax
ret
exist endp
If file isn't found FindFirstFile would return - 1 and passin - 1 to FindClose would lead that it returns 0. Otherwise FindClose would return 1.
Quote from: The Svin on June 05, 2006, 11:36:09 PM
If file isn't found FindFirstFile would return - 1 and passin - 1 to FindClose would lead that it returns 0. Otherwise FindClose would return 1.
:eek Wow... nice solution. :clap:
Ossa
Quote from: The Svin on June 05, 2006, 11:36:09 PM
Otherwise FindClose would return 1.
Much as I hate to contradict The Svin, officially it will return
non-zero, not 1. The API doesn't guarantee a
particular non-zero result, even if one usually occurs across most versions of Windows. Assuming a return value of 1 may not work in the
next Windows version.
Ian_B
Well, it doesn't change anything :)
then the proc (exist) returns zero if file doesn't exist and non zero if exits ;)
totally easier to just to GetFileAttributes.. if it doesnt exist, the return is -1, if it passes, then check the return value to see if its a folder or whatnot...
That won't work with wildcards, though, which was part of brixton's original specification of the problem. Thanks for pointing this out, though, it is indeed a valid alternative if you have a particular filename to check.
Hey all, thanks for the interesting reading.
Eventually I settled on
..
@@:
push 10
call Sleep
push OFFSET findfirstfilestruct
push OFFSET existrule
call FindFirstFile
cmp eax, -1
je @F
push eax
call FindClose
jmp @B
@@:
...
This is perfect as existrule is a string like this: existrule BYTE "c:\ollydbg\*.bak*, 0
My app has to wait until there are no bak files, until it does something.
jmp @B
Endless loop, if the file exists. No timeouts, no warnings, just a stuck program. You must have a better memory than me. Or do you have super users?
Regards, P1 :8)
Yes,
That is what he said...
QuoteMy app has to wait until there are no bak files, until it does something.
Paul
PBrennick is right :U
I'm surprised no-one noticed this mistake here!
Quote from: brixton on June 07, 2006, 11:10:53 PM..
@@:
push 10
call Sleep
push OFFSET findfirstfilestruct
push OFFSET existrule
call FindFirstFile
cmp eax, -1
je @F
push eax
call FindClose
jmp @B
@@:
...
This would result in an error if I kept calling this proc because I don't close the search handle. Just had this exact problem and couldn't see why my program was looping forever when it shouldn't :dance:
Brixton, the true clean-style programming is unreachable;) good example for another thread just started a few time ago about all kind of nasty "leaks"... By the way you are not "no-one";)
Regards,
asmfan
brixton,
You could also use:
ReadDirectoryChangesW
which can run in a thread and monitor a directory for file creation, modification, deletion, move from, and move to. You could respond to a notification from this API and check for the file which has changed, or simply do your FindFirstFile. The main advantage is you would not have to loop constantly--or with a delay--to monitor for a change.
Attached is a FASM program which demonstrates ReadDirectoryChangesW. Put the program in the directory you want to monitor and it will put an entry in a ListView for all changed files. The only thing that was found not to work was if a file was simply opened and closed. The first access triggers a notification, but another notification is not triggered for 1 hour.
hth,
farrier
[attachment deleted by admin]
Thankyou both for your replies, very informative indeed! I will have to look into that ReadDirectoryChangesW, looks very interesting.
And asmfan, I consider myself 'no-one' on this forum :(
Brixton,
You cannot be no-one because I am no-one.
no-one
So you are :lol
Uh oh, first sign of madness; I'm talking to no-one!
-brixton
Don't worry, no-one cares. :bdg
Did someone speak? Oh, no-one did :U