The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: cman on April 19, 2012, 11:03:02 PM

Title: Checking Files
Post by: cman on April 19, 2012, 11:03:02 PM
Whats the most efficient way to check if a file is empty? Thanks for any input!
Title: Re: Checking Files
Post by: qWord on April 19, 2012, 11:06:12 PM
GetFileSize()
Title: Re: Checking Files
Post by: dedndave on April 20, 2012, 12:01:10 AM
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
Title: Re: Checking Files
Post by: jj2007 on April 20, 2012, 06:34:30 AM
include \masm32\MasmBasic\MasmBasic.inc   ; download (http://www.masm32.com/board/index.php?topic=12460)
   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
Title: Re: Checking Files
Post by: dedndave on April 20, 2012, 02:54:29 PM
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