The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: ragdog on April 17, 2008, 12:09:42 PM

Title: find files
Post by: ragdog on April 17, 2008, 12:09:42 PM
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
Title: Re: find files
Post by: evlncrn8 on April 17, 2008, 01:37:35 PM
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
Title: Re: find files
Post by: ragdog on April 17, 2008, 02:03:10 PM
   
I give *.* search for all the files in a drive
I want it with example *. txt
Title: Re: find files
Post by: evlncrn8 on April 17, 2008, 03:52:48 PM
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?
Title: Re: find files
Post by: Tedd on April 17, 2008, 05:46:57 PM
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)
Title: Re: find files
Post by: ragdog on April 17, 2008, 06:29:00 PM
@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
Title: Re: find files
Post by: ragdog on April 18, 2008, 03:25:35 PM
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
Title: Re: find files
Post by: cobold on April 21, 2008, 01:37:55 AM
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]
Title: Re: find files
Post by: ragdog on April 26, 2008, 02:41:53 PM
thanks for your help and example :U
Title: Re: find files
Post by: Synfire on April 26, 2008, 02:48:35 PM
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".