The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: brixton on June 05, 2006, 03:37:52 PM

Title: FileExist question
Post by: brixton on June 05, 2006, 03:37:52 PM
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
Title: Re: FileExist question
Post by: Ossa on June 05, 2006, 03:57:17 PM
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
Title: Re: FileExist question
Post by: P1 on June 05, 2006, 08:06:48 PM
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)
Title: Re: FileExist question
Post by: brixton on June 05, 2006, 08:48:17 PM
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
Title: Re: FileExist question
Post by: P1 on June 05, 2006, 10:04:06 PM
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)
Title: Re: FileExist question
Post by: brixton on June 05, 2006, 10:30:56 PM
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).
Title: Re: FileExist question
Post by: The Svin on June 05, 2006, 11:36:09 PM

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.
Title: Re: FileExist question
Post by: Ossa on June 06, 2006, 10:02:08 AM
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
Title: Re: FileExist question
Post by: Ian_B on June 06, 2006, 01:26:02 PM
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
Title: Re: FileExist question
Post by: The Svin on June 06, 2006, 04:30:09 PM
Well, it doesn't change anything :)
then the proc (exist) returns zero if file doesn't exist and non zero if exits ;)
Title: Re: FileExist question
Post by: evlncrn8 on June 07, 2006, 03:16:57 PM
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...
Title: Re: FileExist question
Post by: Ian_B on June 07, 2006, 10:41:41 PM
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.
Title: Re: FileExist question
Post by: brixton on June 07, 2006, 11:10:53 PM
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.
Title: Re: FileExist question
Post by: P1 on June 08, 2006, 01:45:28 PM
  jmp @BEndless 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)
Title: Re: FileExist question
Post by: PBrennick on June 08, 2006, 01:56:33 PM
Yes,
That is what he said...
QuoteMy app has to wait until there are no bak files, until it does something.

Paul
Title: Re: FileExist question
Post by: brixton on June 08, 2006, 02:11:33 PM
PBrennick is right  :U
Title: Re: FileExist question
Post by: brixton on June 12, 2006, 06:05:40 PM
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:
Title: Re: FileExist question
Post by: asmfan on June 12, 2006, 06:45:30 PM
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
Title: Re: FileExist question
Post by: farrier on June 12, 2006, 06:57:26 PM
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]
Title: Re: FileExist question
Post by: brixton on June 12, 2006, 07:12:02 PM
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 :(
Title: Re: FileExist question
Post by: no-one on June 12, 2006, 08:38:36 PM
Brixton,
You cannot be no-one because I am no-one.

no-one
Title: Re: FileExist question
Post by: brixton on June 12, 2006, 09:18:32 PM
So you are  :lol

Uh oh, first sign of madness;  I'm talking to no-one!

-brixton
Title: Re: FileExist question
Post by: no-one on June 12, 2006, 09:19:42 PM
Don't worry, no-one cares. :bdg
Title: Re: FileExist question
Post by: brixton on June 12, 2006, 10:13:27 PM
Did someone speak?  Oh, no-one did  :U