How could get a list of the files in a directory?
I've used dir>text.txt up untill now, but I'm tired of
cutting out all the junk when I just need the file names.

I remember in dos you could use the interrupts fairly easily,
but for windows i cant see any api that would do what i
need. All I need are a couple API pointed out to me, Thanks.
Posted on 2002-01-12 16:40:43 by matthew
findfirstfile, findnextfile

I'll send you some sourcecode tomorrow if you wish I have some at my parents that I saved from the HD crash :)
Posted on 2002-01-12 16:49:02 by Hiroshimator
this file seeker might help you. ;)

it only have a diffrence, list all paths and inside of them and files inside the directory....

u only have to change the code, to save the find'ed files on another file.

bye! hoppe it helps u!

jean / coder7345
Posted on 2002-01-12 17:09:49 by coder
Here's an example in an unfinished command.com replacement (look at the red proc):


; ###############################

.386
.model flat, stdcall
option casemap :none

; ###############################

include \masm32\include\windows.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc
include \masm32\include\shell32.inc

includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\shell32.lib

; ###############################

CPut PROTO :DWORD
CPutErr PROTO :DWORD
CGet PROTO
GetCmd PROTO
ListLong PROTO
ChangeDir PROTO
Execute PROTO

; ###############################

.data
; ################## console strings
szConTitle db "WinShell Konsole v0.10", 0
szConAbout db "Copyright 2001 by bAZiK", 13,10, 0
szNewLine db 13, 10, 0
szError db "Error: Command or File not found!", 13,10,13,10, 0
szTimeFormat db "HH:mm:ss", 0
szDateFormat db "dd.MM.yyyy", 0
szChgError db "Error: Directory not found!", 13,10, 0
szShellError db "Error: File not found!", 13,10, 0

; ################## console commands
szExit db "exit", 0
szll db "ll", 0
szcd db "cd", 0
szx db "x", 0

; ################## wsprintf templates
szConPrompt db "%s > ", 0
szListFormat db "%s %s %s %35s %13lu", 0
szTotalFormat db "Total Size: %lu Byte(s)", 13,10, 0

; ################# structures
wfd WIN32_FIND_DATA <>
syst SYSTEMTIME <>
ft FILETIME <>

; ###############################

.data?
szOutputBuffer db 512 dup (?)
szInputBuffer db 512 dup (?)
szCmdBuffer db 512 dup (?)
szDirBuffer db 512 dup (?)
szListBuffer db 512 dup (?)
szAttribBuffer db 20 dup (?)
szTimeBuffer db 20 dup (?)
szDateBuffer db 20 dup (?)
szTotalBuffer db 512 dup (?)
szChgBuffer db 512 dup (?)
szShellBuffer db 512 dup (?)
hFind dd ?
hInput dd ?
hOutput dd ?
hWritten dd ?
hRead dd ?
hMenu dd ?
hConWin dd ?
lpTotalSize dd ?

; ###############################

.code

start:
invoke GetStdHandle, STD_INPUT_HANDLE
mov hInput, eax
invoke GetStdHandle, STD_OUTPUT_HANDLE
mov hOutput, eax
invoke SetConsoleTitle, addr szConTitle

invoke SetConsoleTextAttribute, hOutput, FOREGROUND_RED or \
FOREGROUND_GREEN or \
FOREGROUND_BLUE or \
FOREGROUND_INTENSITY or \
BACKGROUND_BLUE
invoke CPut, addr szConTitle
invoke CPut, addr szNewLine
invoke CPut, addr szConAbout
invoke SetConsoleTextAttribute, hOutput, FOREGROUND_RED or \
FOREGROUND_GREEN or \
FOREGROUND_BLUE or \
FOREGROUND_INTENSITY
invoke CPut, addr szNewLine

@@:
call CGet
call GetCmd
cmp byte ptr [szCmdBuffer], 0 ; return gedr?ckt
je @B

invoke lstrcmp, addr szCmdBuffer, addr szExit
.if eax == 0
jmp @F
.endif

invoke lstrcmp, addr szCmdBuffer, addr szll
.if eax == 0
call ListLong
jmp @B
.endif

invoke lstrcmp, addr szCmdBuffer, addr szcd
.if eax == 0
call ChangeDir
invoke CPut, addr szNewLine
jmp @B
.endif

invoke lstrcmp, addr szCmdBuffer, addr szx
.if eax == 0
call Execute
invoke CPut, addr szNewLine
jmp @B
.endif

invoke CPutErr, addr szError
jmp @B
@@:

invoke ExitProcess, 0

; ###############################

Execute proc
invoke RtlZeroMemory, addr szShellBuffer, 512
lea edi, szInputBuffer
lea esi, szShellBuffer
add edi, 2 ; 'x '
@@:
mov al, byte ptr [edi]
cmp al, 13
je @F
mov byte ptr [esi], al
inc edi
inc esi
or al, al
jnz @B
@@:
invoke ShellExecute, 0, 0, addr szShellBuffer, 0, \
addr szDirBuffer, SW_SHOWNORMAL
cmp eax, ERROR_FILE_NOT_FOUND
jne @F

invoke CPutErr, addr szShellError
@@:
ret

Execute endp

; ###############################

ChangeDir proc
invoke RtlZeroMemory, addr szChgBuffer, 512
lea edi, szInputBuffer
lea esi, szChgBuffer
add edi, 3 ; 'cd '
@@:
mov al, byte ptr [edi]
cmp al, 13
je @F
mov byte ptr [esi], al
inc edi
inc esi
or al, al
jnz @B
@@:
invoke SetCurrentDirectory, addr szChgBuffer
cmp eax, 0
jne @F

invoke CPutErr, addr szChgError
@@:
ret
ChangeDir endp

; ###############################
[COLOR=red]
ListLong proc
mov eax, 0
mov lpTotalSize, eax

invoke CPut, addr szNewLine
invoke lstrlen, addr szDirBuffer
lea edi, szDirBuffer
add edi, eax
mov byte ptr [edi], 42 ; *

invoke FindFirstFile, addr szDirBuffer, addr wfd
mov hFind, eax

@@:
lea edi, szAttribBuffer
mov eax, wfd.dwFileAttributes

.if (eax & FILE_ATTRIBUTE_DIRECTORY)
mov byte ptr [edi], 100 ; d
.else
mov byte ptr [edi], 45 ; -
.endif

inc edi

.if (eax & FILE_ATTRIBUTE_ARCHIVE)
mov byte ptr [edi], 97 ; a
.else
mov byte ptr [edi], 45 ; -
.endif

inc edi

.if (eax & FILE_ATTRIBUTE_SYSTEM)
mov byte ptr [edi], 115 ; s
.else
mov byte ptr [edi], 45 ; -
.endif

inc edi

.if (eax & FILE_ATTRIBUTE_HIDDEN)
mov byte ptr [edi], 104 ; h
.else
mov byte ptr [edi], 45 ; -
.endif

inc edi

.if (eax & FILE_ATTRIBUTE_READONLY)
mov byte ptr [edi], 114 ; r
.else
mov byte ptr [edi], 45 ; -
.endif

invoke FileTimeToLocalFileTime, addr wfd.ftLastWriteTime, addr ft
invoke FileTimeToSystemTime, addr ft, addr syst
invoke GetTimeFormat, 0, 0, addr syst, \
addr szTimeFormat, addr szTimeBuffer, 20
invoke GetDateFormat, 0, 0, addr syst, addr szDateFormat, \
addr szDateBuffer, 20

mov eax, MAXDWORD
mov ebx, wfd.nFileSizeHigh
imul ebx
add eax, wfd.nFileSizeLow
mov edx, eax

add lpTotalSize, edx

invoke wsprintf, addr szListBuffer, addr szListFormat, \
addr szAttribBuffer, addr szDateBuffer, \
addr szTimeBuffer, addr wfd.cFileName, edx

invoke CPut, addr szListBuffer
invoke CPut, addr szNewLine

invoke RtlZeroMemory, addr szListBuffer, 512 ; clear buffer
invoke FindNextFile, hFind, addr wfd
invoke GetLastError
cmp eax, ERROR_NO_MORE_FILES
jne @B

invoke CPut, addr szNewLine

invoke wsprintf, addr szTotalBuffer, addr szTotalFormat, lpTotalSize
invoke CPut, addr szTotalBuffer

invoke CPut, addr szNewLine
invoke CloseHandle, hFind

ret
ListLong endp
[/color]
; ###############################

CPutErr proc lpText:DWORD
invoke SetConsoleTextAttribute, hOutput, FOREGROUND_RED or \
FOREGROUND_INTENSITY
invoke CPut, lpText
invoke SetConsoleTextAttribute, hOutput, FOREGROUND_RED or \
FOREGROUND_GREEN or \
FOREGROUND_BLUE or \
FOREGROUND_INTENSITY
ret
CPutErr endp

; ###############################

CPut proc lpText:DWORD
invoke lstrlen, lpText
invoke WriteFile, hOutput, lpText, eax, addr hWritten, 0
mov eax, hWritten
ret
CPut endp

; ###############################

CGet proc
invoke RtlZeroMemory, addr szDirBuffer, 512
invoke GetCurrentDirectory, 512, addr szDirBuffer
lea edi, szDirBuffer
add edi, eax
cmp byte ptr [edi-1], "\"
je @F
mov byte ptr [edi], "\"
@@:
invoke wsprintf, addr szOutputBuffer, addr szConPrompt, addr szDirBuffer
invoke CPut, addr szOutputBuffer
invoke RtlZeroMemory, addr szInputBuffer, 512
invoke ReadFile, hInput, addr szInputBuffer, 512, addr hRead, 0
mov eax, hRead
ret
CGet endp

; ###############################

GetCmd proc
invoke RtlZeroMemory, addr szCmdBuffer, 512
lea edi, szInputBuffer
lea esi, szCmdBuffer
@@:
mov al, byte ptr [edi]
cmp al, 32
je @F
cmp al, 13
je @F
mov byte ptr [esi], al
inc edi
inc esi
or al, al
jnz @B
@@:
ret
GetCmd endp

; ###############################

end start

; ###############################
Posted on 2002-01-12 17:50:22 by bazik