News:

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

find files

Started by ragdog, April 17, 2008, 12:09:42 PM

Previous topic - Next topic

ragdog

hi

can your  help me
fix the plroblem with search for files with same extension on a drive



Find_Files proc ppatn:DWORD
    LOCAL hSrch :DWORD
    LOCAL wfd   :WIN32_FIND_DATA
    invoke FindFirstFile,CTEXT("*.txt"),addr wfd
    mov hSrch, eax
    .IF hSrch != INVALID_HANDLE_VALUE
      @loopy:
        mov eax,wfd.dwFileAttributes
        test eax,FILE_ATTRIBUTE_DIRECTORY
        .IF (zero?)
              lea esi, wfd.cFileName
           invoke SendMessage,hList, LB_GETCURSEL, 0, 0
              inc eax
           invoke SendMessage,hList, LB_INSERTSTRING, eax,esi
           invoke SendMessage,hList, LB_SETCURSEL, eax, 0
        .ELSE
            ;skip "." and ".." 'directories'
            mov eax,DWORD PTR [wfd.cFileName]
            cmp eax,002eh   ;"."
            je @next
            cmp eax,002e2eh ;".."
            je @next

            ;recurse to next directory level
            invoke SetCurrentDirectory, addr wfd.cFileName
            invoke Find_Files,CTEXT("*.txt")
           
        .ENDIF
      @next:
        invoke FindNextFile,hSrch,addr wfd
        cmp eax, 0
        jne @loopy

        invoke FindClose,hSrch
    .ENDIF
    invoke SetCurrentDirectory, CTEXT("..")   ; drop back to next lower directory
    ret
Find_Files endp


thanks in forward

evlncrn8

well it looks like the minute you hit a folder / directory you're changing to it and not continuing the enumeration of the current folder....
explaining what you mean by 'fix' may also help

ragdog

   
I give *.* search for all the files in a drive
I want it with example *. txt

evlncrn8

well the bug still stands.. the error is that you're expecting the names to all come in a nice neat order.. sadly they do not always do that...
and the minute you hit a folder, you go into it and search in there... 

.IF (zero?) also looks very strange to me...

why not debug through the code, and match up the names and see if you see them all?

Tedd

Looks familiar.. http://www.masm32.com/board/index.php?topic=5844.msg43407#msg43407

As per the comment at the bottom -- it seems that changing the current-directory invalidates the WIN32_FIND_DATA state, so after recursing into a directory and returning, FindNext doesn't work correctly.

The way to do it is to use full paths, and pass the full directory path to each recursive call of the function.
(I have an example if you get desperate :wink)
No snowflake in an avalanche feels responsible.

ragdog

@tedd

i have this source from your link :bg

   
it works not with a extension exmpl: *.txt,*.mp3 ........


.data
szFilter        db "*.*",0
DirBack         db "..",0
szPath          db "c:\masm32\",0

.code

invoke SetCurrentDirectory,ADDR szPath
invoke CreateThread,0,0,addr ScanForFiles,0,0,addr ThreadID


ScanForFiles proc   
LOCAL fnd      :WIN32_FIND_DATA
LOCAL hFind    :DWORD

               invoke FindFirstFile, addr szFilter, addr fnd             
               mov hFind, eax
                             
               .while eax > 0
                             
                          lea esi,fnd.cFileName                       ; skip "." y ".."
                          cmp [esi],byte ptr "."           
                           je nextf
         
                     .if  fnd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY   

                                   ; *********  IS A DIR  *************       
                          invoke SetCurrentDirectory, ADDR fnd.cFileName ; into dir
                          invoke GetCurrentDirectory, 1024, ADDR hDir     
                          invoke ScanForFiles                            ; recursive call
                          invoke SetCurrentDirectory, ADDR DirBack       ; go back
                               
                     .else         ; *********  IS A FILE  ************
                          invoke GetCurrentDirectory,1024, ADDR hDir
                          invoke AddItem, ADDR fnd.cFileName ,addr hDir,0
                     .endif
                  nextf: 
                          invoke FindNextFile, hFind, addr fnd              ; next
               .endw
              invoke FindClose,hFind
         ret
ScanForFiles endp

ragdog

hi

can your help me please or have your a working source code
for example: find all *.asm on my drive c: ?!

thanks in forward

greets
ragdog

cobold

ragdog,

your code doesn't work with szFilter set to something else than "*.*"
If you invoke FindFirstFile, addr szFilter, addr fnd
with szFilter = "*.asm" your code NEVER comes into this .if-branch
.if  fnd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY
because most likely there is no DIRECTORY with extension *.asm.

To find all files with "*.ext" within a "path" you've got to do a recursive search with pattern "*.*" which goes recursively through all directories within "path".
Within this call another procedure that performs the file-search by "*.ext"

I give you a working sample!

regards - cobold





[attachment deleted by admin]

ragdog

thanks for your help and example :U

Synfire

Quote from: cobold on April 21, 2008, 01:37:55 AM
To find all files with "*.ext" within a "path" you've got to do a recursive search with pattern "*.*" which goes recursively through all directories within "path".
Within this call another procedure that performs the file-search by "*.ext"

Or rather than doing two seperate searches, just do the one search for "*.*" then, if it's not a directory, check file extension for ".asm".