News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

file lister

Started by zedd151, April 22, 2024, 03:20:19 AM

Previous topic - Next topic

zedd151

lister is a program that will list (seperately) all files and folders in a selected drive, directory or folder.

It is not a command line tool, nor does it have a GUI.
It simply does its sole job, announces when it is finhed, then exits.

Upon running the executable, a Browse for Folder dialog appears. Select a drive, directory or folder.
Output will be given in two files namely files.txt and dirs.txt in the folder where the program is located.

files.txt will list all files in the selected drive, directory or folder including the full path name.
dirs.txt will list all the subfolders in the selected drive, directory or folder including the full path name.

Once the program finishes listing all the files, a message box announces "done!".
After closing the message box, the program simply closes.

include \masm32\include\masm32rt.inc

    getfile     proto :dword, :dword, :dword, :dword
    getfolder   proto :dword
    zstrcat     proto :dword, :dword
    zstrcpy     proto :dword, :dword
    zstrlen     proto :dword

.data

    pth         db 512 dup (0)
    all         db "\*.*", 0
    hfiles      dd 0
    hdirs       dd 0
   
.code
start:
fn CreateFile, "files.txt", 0C0000000h, 3, 0, 2, 0, 0
mov hfiles, eax
fn CreateFile, "dirs.txt", 0C0000000h, 3, 0, 2, 0, 0
mov hdirs, eax

invoke getfolder, addr pth
invoke getfile, addr pth, addr all, hfiles, hdirs
invoke CloseHandle, hfiles
invoke CloseHandle, hdirs
fn MessageBox,0,"done!",0,0
invoke ExitProcess, 0

getfolder proc lpBuffer:dword
local bi :BROWSEINFO, lpIDList :dword
    push ecx
    mov bi.hwndOwner, 0
    mov bi.pidlRoot, 0
    mov bi.pszDisplayName, 0
    mov bi.lpszTitle, 0
    mov bi.ulFlags, BIF_RETURNONLYFSDIRS or BIF_DONTGOBELOWDOMAIN
    mov bi.lpfn, 0
    mov bi.lParam, 0
    mov bi.iImage, 0
    invoke SHBrowseForFolder,addr bi
    mov lpIDList, eax
    .if lpIDList == 0
      mov eax, 0
      push eax
      jmp @F
    .else
      invoke SHGetPathFromIDList,lpIDList,lpBuffer
      mov eax, 1
      push eax
      jmp @F
    .endif
    @@:
    invoke CoTaskMemFree,lpIDList
    invoke zstrlen, lpBuffer
    mov ecx, lpBuffer
    add ecx, eax
    dec ecx
    cmp byte ptr [ecx], "\"
    jnz @f
    mov byte ptr [ecx], 0
    @@:
    pop eax
    pop ecx
    ret
getfolder endp

getfile proc pathz:dword, allfilez:dword, hfilz:dword, hdirz:dword
local FindData:WIN32_FIND_DATA, hFind:dword, fwrit:dword, dwrit:dword
local szBuff[512]:byte, szTemp[512]:byte, bsl[4]:byte, crlf[4]:byte
    push esi
    push edi
    push ebx
    push edx
    push ecx
    lea eax, bsl
    mov dword ptr [eax], "\"
    lea eax, crlf
    mov dword ptr [eax], 0A0Dh
    invoke zstrcpy, addr szBuff, pathz
    invoke zstrcat, addr szBuff, allfilez
    invoke FindFirstFile, addr szBuff, addr FindData
  .if eax != INVALID_HANDLE_VALUE
    mov hFind, eax
    xor edi, edi
  .while eax != 0
    .if byte ptr FindData.cFileName != '.'
      mov eax, FindData.dwFileAttributes
      .if al >= 10h && al < 20h
        invoke zstrcpy, addr szBuff, pathz
        invoke zstrcat, addr szBuff, addr bsl
        invoke zstrcat, addr szBuff, addr FindData.cFileName
        invoke zstrcat, addr szBuff, addr crlf
        invoke zstrlen, addr szBuff
        mov esi, eax
        invoke WriteFile, hdirz, addr szBuff, esi, addr dwrit, 0
        invoke zstrcpy, addr szBuff, pathz
        invoke zstrcat, addr szBuff, addr bsl
        invoke zstrcat, addr szBuff, addr FindData.cFileName
        invoke getfile, addr szBuff, allfilez, hfilz, hdirz
      .endif
        mov eax, FindData.dwFileAttributes
      .if al >= 20h && al < 30h || al < 10h || al == 80h
        invoke zstrcpy, addr szTemp, pathz
        invoke zstrcat, addr szTemp, addr bsl
        invoke zstrcat, addr szTemp, addr FindData.cFileName
        invoke zstrcat, addr szTemp, addr crlf
        invoke zstrlen, addr szTemp
        mov esi, eax
        invoke WriteFile, hfilz, addr szTemp, esi, addr fwrit, 0
      .endif
    .endif
    invoke FindNextFile, hFind, addr FindData
  .endw
    invoke FindClose, hFind
  .endif
    pop ecx
    pop edx
    pop ebx
    pop edi
    pop esi
    ret
getfile endp

zstrlen proc src:dword
    push ecx
    mov ecx, src
    xor eax, eax
    @@:
    cmp byte ptr [ecx+eax], 0
    jz @f
    inc eax
    jmp @b
    @@:
    pop ecx
    ret
zstrlen endp

zstrcpy proc dst:dword, src:dword
    push ecx
    push edx
    push ebx
    mov edx, dst
    mov ecx, src
    xor eax, eax
    @@:
    mov bl, [ecx+eax]
    cmp bl, 0
    jz @f
    mov [edx+eax], bl
    inc eax
    jmp @b
    @@:
    mov byte ptr [edx+eax], 0
    pop ebx
    pop edx
    pop ecx
    ret
zstrcpy endp

zstrcat proc dst:dword, src:dword
    push ecx
    push edx
    push ebx
    mov edx, dst
    mov ecx, src
    xor eax, eax
    @@:
    cmp byte ptr [edx], 0
    jz @f
    inc edx
    jmp @b
    @@:
    mov bl, [ecx+eax]
    cmp bl, 0
    jz @f
    mov [edx+eax], bl
    inc eax
    jmp @b
    @@:
    mov byte ptr [edx+eax], 0
    pop ebx
    pop edx
    pop ecx
    ret
zstrcat endp

end start

Anyone can use this code and modify it in any way they see fit.
Disclaimer: Only tested on Windows 7.
:tongue: