News:

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

easy way to determine weather file exists

Started by Telefunken, July 16, 2006, 09:32:49 PM

Previous topic - Next topic

Telefunken

What is the easiest way to determine weather already exists or not? I tried using CreatFile and then using GetLastError to see if the ERROR_FILE_EXISTS error was passed, but that didn't work.

zooba

That is the easiest way, except for one thing.

CreateFile expects the file to exist, so it isn't going to pass an error because it does. Check for ERROR_FILE_NOT_FOUND instead, and make sure you use OPEN_EXISTING in the call so you don't accidentally create the file.

Cheers,

Zooba :U

zcoder

Maybe this function will help



file_exist proc lpFileName:DWORD

    LOCAL wfd :WIN32_FIND_DATA

    invoke FindFirstFile,lpFileName,addr wfd
    .if eax == INVALID_HANDLE_VALUE
        mov eax,0
    .else
         invoke FindClose,eax
         mov eax,1
    .endif
    ret
file_exist endp



Zcoder....
Back in 1979, My computer ran so fine.
And there was no such thing,
As a Microsoft Crashed Machine.
http://zcoder.110mb.com
http://www.dietzel.com/partner/idevaffiliate.php?id=345_6  Free Domain Names

Telefunken

Thanks for the replys

This seems to work for me
invoke CreateFile,addr file,GENERIC_WRITE,FILE_SHARE_READ,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL
invoke GetLastError
.if eax == ERROR_FILE_EXISTS
invoke MessageBox,NULL,addr text,addr errcap,MB_OK
jmp exists
.endif


:dance:

gwapo

Hi,

Probably the easiest way to check whether a file exist or not, is by getting its attributes using GetFileAttributes function. This function will return numerical values of the file attributes (if the file is existing), or the function returns -1 if the file is not existing.


invoke GetFileAttributes, addr szFilename
cmp eax, -1
jmp FileIsNotExisting

; In here, file is existing
jmp @f

FileIsNotExisting:

@@:


Regards,

-chris

Shantanu Gadgil

To Telefunken,
The code you have written:
Quoteinvoke CreateFile,addr file,GENERIC_WRITE,FILE_SHARE_READ,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL
would CREATE a new file if not existing, right? The decision making process would be fine if _THAT_ is what you want anyway, but could the algo be more like this...
check if file exists
if exists something1
else something2


One might not always _WANT_ to create a file if does not exist, right?

zooba has pointed out the same. I think using OPEN_EXISTING would a more "logical"  :bg :bg way of checking!!!
To ret is human, to jmp divine!

Telefunken

Thats true, but for my purposes, I might as well create the file if it does not exists. But its good to know the other ways work as well. Thanks

zooba

In that case, use OPEN_ALWAYS. If the file doesn't exist it will be created. If it does exist it will be opened.

If you intend to overwrite the file and don't care what was in it, just use CREATE_ALWAYS.

If the file existed before the CreateFile call, the call will succeed and GetLastError will return ERROR_ALREADY_EXISTS (for both OPEN_ and CREATE_ALWAYS)

Cheers,

Zooba :U

skywalker

Quote from: Telefunken on July 17, 2006, 01:37:30 AM
Thanks for the replys

This seems to work for me
invoke CreateFile,addr file,GENERIC_WRITE,FILE_SHARE_READ,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL
invoke GetLastError
.if eax == ERROR_FILE_EXISTS
invoke MessageBox,NULL,addr text,addr errcap,MB_OK
jmp exists
.endif


I thought I saw a routine somewhere that used GetLastError and returned the error condition for each failure.I did a search and looked thru the examples too. Maybe I was dreaming.

Thanks.




evlncrn8

erm... GetFileAttributes looks much much easier, with less overhead from createfile->getlasterror and so on....

Mark Jones

Quote from: evlncrn8 on July 31, 2006, 07:49:22 AM
erm... GetFileAttributes looks much much easier, with less overhead from createfile->getlasterror and so on....

I've used GetFileAttributes and it has always worked perfectly for this. :)
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

jdoe

Hi,

So GetFileAttributes seems to be the best way to be sure a file exist but if the path passed to GetFileAttributes is a folder path then the returned value won't be INVALID_FILE_ATTRIBUTES and it will looks like the file exist even if it is a folder. To me it is a problem so I wrote a function to make sure it is not a folder.


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Return non-zero if file path exist
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

.586

.MODEL FLAT, STDCALL

OPTION CASEMAP:NONE


INCLUDE \masm32\include\kernel32.inc


FILE_ATTRIBUTE_DIRECTORY   EQU 10h
INVALID_FILE_ATTRIBUTES    EQU -1


.CODE

OPTION PROLOGUE:NONE
OPTION EPILOGUE:NONE

AzmtPathFileExistA PROC p_lpszFilePath:DWORD

   push dword ptr [esp+4]
   call GetFileAttributesA
   test eax, (INVALID_FILE_ATTRIBUTES and FILE_ATTRIBUTE_DIRECTORY)
   jz @F

   xor eax, eax

@@:
   ret 4

AzmtPathFileExistA ENDP

OPTION PROLOGUE:PROLOGUEDEF
OPTION EPILOGUE:EPILOGUEDEF

END



I think the returned value is more reliable this way.

jdoe