Page 1 of 1

Power FileFinder

Posted: Mon Jun 04, 2007 1:33 am
by kinglestat
You might say you have seen this before. But all filefinders in my case have been useless, and I wanted the ability to keep searches in memory with certain info. Though I do need some tweaking. Still its 0230 and I still have to ask some questions. I also need to thank Trond for his excellent DynamicLists which I am using in the utility.

Code: Select all

; Dynamic  recursive file finder wih stored results
; KingLestat
; June/2007
; PB v4.02

EnableExplicit

Macro GlobalNewDynamicListPointer(Name, Type) 
  If 0 
    Global NewList Name.Type() 
  EndIf 
EndMacro 

Macro NewDynamicListPointer(Name, Type) 
  If 0 
    NewList Name.Type() 
  EndIf 
EndMacro 

Macro BindListTemplate(Type) 
  Procedure BindList#Type(List.Type(), ID.q) 
    !mov eax, [p.v_ID] 
    !mov edx, [p.v_ID+4] 
    !mov ecx, [p.v_ID-4] 
    !mov [ecx], eax 
    !add ecx, 4 
    !mov [ecx], edx 
  EndProcedure 
EndMacro 

Macro DynamicNewListTemplate(Type) 
  Procedure.q DynamicNewList#Type() 
    Protected NewList Local.Type() 
    !mov eax, [esp] 
    !mov edx, [esp+4] 
    !add esp, 8 
    !ret 
  EndProcedure 
EndMacro 

Macro FreeListTemplate(Type) 
  Procedure FreeList#Type(ID.q) 
    If 0 
      NewList Dummy.Type() 
    EndIf 
    BindList#Type(Dummy(), ID.q) 
  EndProcedure 
EndMacro 

Macro UseListType(Type) 
  BindListTemplate(Type) 
  DynamicNewListTemplate(Type) 
  FreeListTemplate(Type) 
EndMacro


Structure     stInfo
  name.s
  md5.s
  size.l
EndStructure


Structure     stName
  name.s  
EndStructure

Global Dim        gListArray.q ( 4 )

UseListType ( stInfo )
UseListType ( stName )
GlobalNewDynamicListPointer ( ptrList1, stInfo )
GlobalNewDynamicListPointer ( ptrList2, stName )

Procedure         InfoPath ( filename.s )
  AddElement ( ptrList1() )
  ptrList1()\name = filename
  ptrList1()\md5 = MD5FileFingerprint ( Filename )
  ptrList1()\size = FileSize ( filename )
EndProcedure

Procedure         JustName ( filename.s )
  AddElement ( ptrList2() )
  ptrList2()\name = filename  
EndProcedure

Procedure         FindFileEx ( command.l, mask.l, root.s, func.l )  
  
  Protected       temp.s
  Static NewList  llmask.s() 
      
  If command = 1    
    
    Protected     i.l
    Protected     filemask.s
            
    ClearList ( llmask() )
    filemask = PeekS ( mask )
    i & 0
    i + 1
    temp = StringField ( filemask, i, ";" )
    
    Repeat
    
      AddElement ( llmask() )
      llmask() = temp
      i + 1
      temp = StringField ( filemask, i, ";" )
    
    Until temp = ""    
    
    command + 1
      
  EndIf
  
  If command = 2
  
    Protected     df.l
    Protected     skip.l
  
    ForEach llmask()
      
      df = ExamineDirectory ( #PB_Any, root, llmask() )
      
      If df > 0
        While NextDirectoryEntry ( df )
          If DirectoryEntryType ( df ) = #PB_DirectoryEntry_File            
            CallFunctionFast ( func, root + "\" + DirectoryEntryName ( df ) )
          EndIf
        Wend    
      EndIf
    
    Next
    
    df = ExamineDirectory ( #PB_Any, root, "" )
    
    If df > 0
      
      skip & 0
    
      While NextDirectoryEntry ( df )
        If DirectoryEntryType ( df ) = #PB_DirectoryEntry_Directory
          skip + 1
          If skip > 2
            temp = DirectoryEntryName ( df )
            FindFileEx ( 2, 0, root + "\" + temp, func )
          EndIf
        EndIf       
      Wend    
    EndIf
  
  EndIf
  
EndProcedure 

OpenConsole()

gListArray ( 0 ) = DynamicNewListstInfo ()
gListArray ( 1 ) = DynamicNewListstName ()

BindListstName ( ptrList2(), gListArray ( 1 ) )
FindFileEx ( 1, @"*.hlp;*.inf;*.ocx", "c:\windows", @JustName() )
ForEach ptrList2()
  PrintN ( ptrList2()\name )
Next


BindListstInfo ( ptrList1(), gListArray ( 0 ) )
FindFileEx ( 1, @"b*.dll;*.ocx", "c:\windows", @InfoPath() )
ForEach ptrList1()
  PrintN ( ptrList1()\name + " [" + ptrList1()\md5 + "] " + Str ( ptrList1()\size ) )
Next

End
FindFileEx uses 4 parameters.
command is always 1 (though I mean to add other params)
mask is address of files you want to look for wild cards included
root is the starting dir
func is function which takes string as parameter (which is full path of filename) and which you can do anything you like

If you have any ideas of how to optimize please let me know and I hope you find it usefull

cheers

KingLestat
[/code]

Posted: Mon Jun 04, 2007 8:21 am
by kinglestat
A slight update and simpler example

Code: Select all

; Dynamic  recursive file finder with stored results
; KingLestat
; June/2007
; PB v4.02

EnableExplicit


Structure     stInfo
  name.s
  md5.s
  size.l
EndStructure

Structure     stName
  name.s  
EndStructure

Global NewList  infolist.stInfo()
Global NewList  namelist.stName()


Procedure         InfoPath ( filename.s )
  AddElement ( infolist() )
  infolist()\name = filename
  infolist()\md5 = MD5FileFingerprint ( Filename )
  infolist()\size = FileSize ( filename )
EndProcedure

Procedure         JustName ( filename.s )
  AddElement ( namelist() )
  namelist()\name = filename  
EndProcedure

Procedure         FindFileEx ( sep.s, mask.l, root.s, func.l )  
  
  Protected       temp.s
  Protected       df.l
  Protected       skip.l
  Static NewList  llmask.s()
        
  If mask    
    
    Protected     i.l
    Protected     filemask.s
            
    ClearList ( llmask() )
    filemask = PeekS ( mask )
    i & 0
    Repeat
    
      temp = StringField ( filemask, i, sep )
      
      If temp > ""
        AddElement ( llmask() )
        llmask() = temp
        i + 1
      Else
        Break
      EndIf
          
    ForEver
      
  EndIf
  
  ForEach llmask()
    
    df = ExamineDirectory ( #PB_Any, root, llmask() )
    
    If df > 0
      While NextDirectoryEntry ( df )
        If DirectoryEntryType ( df ) = #PB_DirectoryEntry_File            
          CallFunctionFast ( func, root + "\" + DirectoryEntryName ( df ) )
        EndIf
      Wend    
    EndIf
  
  Next
  
  df = ExamineDirectory ( #PB_Any, root, "" )
  
  If df > 0
    
    skip & 0
  
    While NextDirectoryEntry ( df )
      If DirectoryEntryType ( df ) = #PB_DirectoryEntry_Directory
        skip + 1
        If skip > 2
          temp = DirectoryEntryName ( df )
          ;Debug temp
          FindFileEx ( sep, 0, root + "\" + temp, func )
        EndIf
      EndIf       
    Wend    
  EndIf
    
EndProcedure

OpenConsole()


FindFileEx ( ";", @"*.hlp;*.inf;*.ocx", "c:\windows", @JustName() )
ForEach namelist()
  PrintN ( namelist()\name )
Next


FindFileEx ( ",", @"b*.dll,*.ocx", "c:\windows", @InfoPath() )
ForEach infolist()
  PrintN ( infolist()\name + " [" + infolist()\md5 + "] " + Str ( infolist()\size ) )
Next

End
First parameter is the separator. And example uses the normal PB lists