beta.blog

FASM: Directory Listing

by on Jan.21, 2011, under Programming

This article provides information about how to list a directory’s content and get the names of its files with the help of the Win32 API. This code is therefore incompatible with Linux based operating systems (unless you’re using Wine).

The source code below gets all files located at C:\test\ and outputs their names via a MessageBoxA.

format PE GUI 4.0
entry start

include 'win32a.inc'

start:
    push DST_PATH
    call EnumFiles

    DST_PATH db 'C:\test\*.*', 0        ; Directory to list files from. Change it according to your needs.

    proc EnumFiles, szPath
    locals
    hFindFile dd ?
    endl
    sub esp, 0x140                      ; Free some space on the stack for our WIN32_FIND_DATA struct
    mov ebx, esp                        ; ebx now holds our struct
    push ebx
    push [szPath]
    call [FindFirstFileA]
    test eax, eax
    jz .list_end
      mov [hFindFile], eax
      lea esi, [ebx+0x2C]               ; = FileName
      mov ecx, [ebx+0x20]               ; = FileSize
.fileloop:
    push ebx
    push [hFindFile]
    call [FindNextFileA]
    test eax, eax
    jz .list_end                        ; If there're no more files, exit the loop
      mov ecx, [ebx+0x20]               ; = FileSize (in Bytes). Is 0 when it's a directory
      invoke  MessageBoxA,HWND_DESKTOP,esi,txt_msg_caption,0  ; =>> Show a MessageBox containing the name of the current file/directory <==
      jmp .fileloop
.list_end:
    add esp, 0x140                      ; Restore stack
    invoke  MessageBoxA,HWND_DESKTOP,txt_msg_text,txt_msg_caption,0
    call [ExitProcess]

; Custom Data: Some Strings which will be shown in a MessageBoxA later:
section '.data' data readable writeable
          txt_msg_caption db 'TEST',0
          txt_msg_text    db '[ Listing the directory has been finished ]',0

; Imported functions and corresponding names of DLL files:
section '.idata' import data readable writeable
  library kernel,'KERNEL32.DLL',\
          user,'USER32.DLL'

  import kernel,\
         ExitProcess,'ExitProcess',\
         FindFirstFileA,'FindFirstFileA',\
         FindNextFileA,'FindNextFileA'

  import user,\
         MessageBoxA,'MessageBoxA'

ret     ; Not really needed
endp
C:\test\

1 Comment for this entry

  • RDRush

    Insane!

    No one can accuse you of taking the easy way out. Although resources are still thin for FASM, it has come light years down the path to real success.

Leave a Reply

*

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!