News:

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

Checking Files

Started by cman, April 19, 2012, 11:03:02 PM

Previous topic - Next topic

cman

Whats the most efficient way to check if a file is empty? Thanks for any input!

qWord

FPU in a trice: SmplMath
It's that simple!

dedndave

GetFileAttributesEx

        xor     eax,eax
        push    eax
        push    eax
        sub     esp,sizeof WIN32_FILE_ATTRIBUTE_DATA-8
        INVOKE  GetFileAttributesEx,offset szFileName,eax,esp
        add     esp,sizeof WIN32_FILE_ATTRIBUTE_DATA-8
        pop     edx                                      ;EDX = file size high dword
        pop     eax                                      ;EAX = file size low dword
        or      eax,edx
        jnz     file_is_not_empty

file_is_empty_or_is_not_a_file_or_does_not_exist:


i hate guys that use really long labels   :P

jj2007

include \masm32\MasmBasic\MasmBasic.inc   ; download
   Init
   Open "O", #1, "Empty.txt"
   Close #1
   Print Str$("no such file:\t%i\n", Lof("BadFile.txt"))
   Print Str$("empty file:\t%i\n", Lof("Empty.txt"))
   Inkey Str$("regular file:\t%i\n", Lof("\Masm32\include\Windows.inc"))
   Exit
end start

no such file:   -1
empty file:     0
regular file:   977412

:wink

dedndave

another option...
        xor     eax,eax
        push    eax
        push    eax
        sub     esp,sizeof WIN32_FILE_ATTRIBUTE_DATA-12
        push    eax
        INVOKE  GetFileAttributesEx,offset szFileName,eax,esp
        pop     ecx                                      ;ECX = file attributes
        add     esp,sizeof WIN32_FILE_ATTRIBUTE_DATA-12
        pop     edx                                      ;EDX = file size high dword
        pop     eax                                      ;EAX = file size low dword


if the function fails (which indicates the object does not exist), EAX, ECX, and EDX will all be 0
if the object exists, whether it be a file, folder, or device, ECX will be non-zero