News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

FileExist question

Started by brixton, June 05, 2006, 03:37:52 PM

Previous topic - Next topic

brixton

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
If you love somebody, set them free.
If they return, they were always yours. If they don't, they never were..

Ossa

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
Website (very old): ossa.the-wot.co.uk

P1

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)

brixton

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
If you love somebody, set them free.
If they return, they were always yours. If they don't, they never were..

P1

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)

brixton

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).
If you love somebody, set them free.
If they return, they were always yours. If they don't, they never were..

The Svin


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.

Ossa

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
Website (very old): ossa.the-wot.co.uk

Ian_B

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

The Svin

Well, it doesn't change anything :)
then the proc (exist) returns zero if file doesn't exist and non zero if exits ;)

evlncrn8

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...

Ian_B

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.

brixton

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.
If you love somebody, set them free.
If they return, they were always yours. If they don't, they never were..

P1

  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)

PBrennick

Yes,
That is what he said...
QuoteMy app has to wait until there are no bak files, until it does something.

Paul
The GeneSys Project is available from:
The Repository or My crappy website