News:

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

search file

Started by franlou, November 25, 2007, 11:56:00 AM

Previous topic - Next topic

franlou

Hi,
excuse my bad english
I want to found the path of  'wordpad.exe'
I use a subroutine of bitRake; I tried to contact him but it is not possible
there is this subroutine:
-----------------------------------
;
;      Recursive File Search
;
;   Purpose: Traversal across an entire hard drive
;
;   The SearchForFile function should work in any program provided you include the PROTO
;   below. If it doesn't, email me!
;
;   Special Thanks: Win32ASM.cjb.net message board, bitRAKE!
;   email: kornbizkit536@hotmail.com
;
;   Note: This is a console mode program and builds with Project|Console Assemble & Link
;

   ; I make these as macros because its easier to understand the code..
   @GetToChar MACRO
      scasb   ; check for zero
      jnz $-1 ; loop until zero is found
      
      dec edi      ; go back one char
   ENDM
   
   @ClearFN MACRO
      lea edi,WFD.cFileName   ; put address of file name in EDI ( WIN32_FIND_DATA structure)
      mov ecx,260      ; we're gonna do this 260 times
      xor al,al      ; clear AL
      rep stosb      ; clear all chars in WFD.cFileName
   ENDM


.data
PathAndAppExe    db    128 dup (0)      
FileSearched   db   "wordpad.exe",0
StartDir       db    "C:",256 dup (0)   ; changez ici avec n'importe quel disque

.code
;--------------------------------------------------
SearchForFile PROC StartPath:DWORD,FileToFind:DWORD
;--------------------------------------------------
;Entrée : StartPath : Disque pour Rechercher       :ex " C:"
;      : FileToFind: nom du fichier à rechercher   :ex "wordpad.exe"
;Sortie: ADDR PathAndAppExe= chemin + nom du fichier recherché

   LOCAL   WFD:WIN32_FIND_DATA   ; used for file/folder search
   LOCAL   fPath[260]:BYTE      ; used to store StartPath locally
   LOCAL   fPath2[260]:BYTE   ; we add sub-folders names onto this to form full paths
   LOCAL   hFind:DWORD         ; find handle

   ; Below is just some little data's that we need in order for function to work
   
   jmp @F
   WildCard db "\*",0         ; search ALL files
   ;CRLF db 13,10,0         ; tell me you don't know what this is
   ;foundat db "Found: ",0      ; tell the user we found a file that matches
   @@:
      lea edi,fPath

      push edi         ; save EDI in stack
      mov esi,StartPath   ; we are copying supplied StartPath to our buffer
      mov ecx,256         ; all 256 bytes
      rep movsb         ; copy path
      pop edi            ; put the path back in EDI


      xor al,al         ; clear AL
      @GetToChar         ; Find the first zero
      mov al,'\'         ; now equals Drive:\Path\*
      stosb            ; e.g.: C:\Windows\*
      mov al,'*'
      stosb         
      
      @ClearFN         ; clears the cFileName field in Win32_Find_Data
         
      invoke FindFirstFile,addr fPath,addr WFD   ; find first file

         
      push eax   
      mov hFind,eax      ; save FindHandle
      pop ebx            ; put handle in EBX
      
      .while ebx > 0      ; while a file is found..
         lea esi,WFD.cFileName
         lodsw            ; get first two chars
         .if AX!=02E2Eh && AX!=0002Eh   ; '..' and '.'      

         Â  lea edi,WFD
         Â  mov eax,[edi]   ; file attributes
         Â  .if ax & FILE_ATTRIBUTE_DIRECTORY   ; is it a directory?
            sub esi,2         ; undo the lodsw
            lea edi,fPath2      ; load up the secondary path in EDI
            push edi         ; save it on the stack...
         
            xor al,al         ; clear secondary path
            mov ecx,260         ; ..
            rep stosb
            
            mov edi,[esp]         ; restore EDI
            
            lea eax,fPath         ; first path
            
            invoke lstrcpy,edi,eax     ; copy first to second
         Â                mov al,'*'            ; get to the end....
            @GetToChar
            
            mov byte ptr [edi],00h     ; delete the wildcard
            
            invoke lstrcat,edi,esi     ; tack on the new directory name
   
            pop edi            ; restore EDI from stack

            pushad               ; must save ALL regs or errors will ocur :)
         Â       invoke SearchForFile,edi,FileToFind  ; call function again
         Â     popad               ; restore all regs
         Â     
         Â  .else
         Â 
         Â     sub esi,2            ; undo the lodsw
         Â     invoke lstrcmpi,FileToFind,esi   ; case insensitive compare
         Â     or eax,eax            ; are they equal?
         Â     jz found_file         ; if eax=0 they are equal
         Â     
         Â 
         Â  .endif
         
         .endif
         @ClearFN                  ; Clear the cFileName field again
         invoke FindNextFile,hFind,addr WFD
         
         mov ebx,eax
      
      .endw

__cls_fnd:

         invoke FindClose,hFind         ; close it up
      ret
found_file:   ; we found a file, so we report it to the user
   lea edi,fPath2
   invoke lstrcpy,edi,addr fPath
   mov al,'*'
   scasb
   jnz $-1

   dec edi
   mov byte ptr [edi],00h
   invoke lstrcpy,ADDR PathAndAppExe,ADDR fPath ;ex: "C:\Programs Files\Accessoires\*"
   lea edi,WFD.cFileName
   
   invoke lstrcat,addr fPath2,edi
   invoke lstrcpy,ADDR PathAndAppExe,ADDR fPath2
   jmp __cls_fnd
ret
SearchForFile ENDP

----------------------------------
This is working correctly with W98
but not, since I use it on WXP2 :
I cannot  find the path of 'wordpad.exe' or yes it can , but it not return when it found:

when it find "C:\Program Files\Windows NT\Accessoires\wordpad.exe" it's OK
Here it don't return and it look for again to find "C:Windows\system32\dllcache\wordpad.exe" !(I display this with a MessageBox"), then it return here???
There is no repertory "dllcahe" in repertory "C:Windows\system32\"???
I try differents solutions but I got always finally :"C:Windows\system32\wordpad.exe" and it not stop when it find the good repertory "C:\Program Files\Windows NT\Accessoires\wordpad.exe"
thanks for your help




lingo

"I use a subroutine of bitRake"

It is a newbie code, hence it is NOT "a subroutine of bitRake"  :wink

donkey

Quote from: lingo on November 25, 2007, 01:16:20 PM
"I use a subroutine of bitRake"

It is a newbie code, hence it is NOT "a subroutine of bitRake"  :wink

I agree, the author is unnamed and only thanking bitRake. I think if you search around you will find quite a few better examples than that, I have one in WinExplorer but it isn't very good so I would avoid it. Never was very interested in improving the search function after first coding.
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable

hutch--

I wonder the virtue of the pseudo history lesson when the name on the code is kornbizkit536.

RE the original question, the code does not appear to preserve EBX ESI and EDI correctly so while it may run on win98, it will fail on some Windows versions.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

franlou

I thank you
I have not understand your phrase Hutch:
"I wonder the virtue of the pseudo history lesson when the name on the code is kornbizkit536"
If I understand I am going to preserve EB ESI & EDI; I wil tell you my results

Mark Jones

Quote from: franlou on November 25, 2007, 04:06:21 PM
"I wonder the virtue of the pseudo history lesson when the name on the code is kornbizkit536"

What Hutch means here, is that the original coder of this piece appears to be young and inexperienced, based on the name "kornbizkit536." Someone older and more experienced would probably use their real name or a better name.

As for searching for files, see FindFirstFile and FindFirstFileEx at MSDN. This might help:

http://msdn2.microsoft.com/en-us/library/aa364418.aspx

Experiment and let us know if you get stuck.
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

Shantanu Gadgil

Maybe you can try this: http://www.masm32.com/board/index.php?topic=3026.msg32540#msg32540
and add your filename comparing routine to it ?

HTH,
Shantanu
To ret is human, to jmp divine!