Jump to content

Recursive File System search


Recommended Posts

Alright, this being my first post to the forum, I just want to say that AutoIt is fantastic. Not only do I use it every day, but it really got me into scripting.

Anyway,

I am trying to create a recursive search function for a variety of different purposes; in this case to scan a hard drive and see if any files / folder are compressed.

I have a working VBS script that does it, but I want to be able to do it in AutoIt for convenience.

Here is the working VBS. Basically it starts at C:\, gets a list of files, checks them for compression. Gets a list of folders, checks them for compression and calls itself (so it will go through the whole filing system).

The problem I am having in AutoIt is twofold; one, I cannot diffrentiate between files and folders when doing FileFindFirstFile and the subsequent FileFindNextFile. While normally a good thing, this makes performing an operation on only folders tough.

Second issue. In the VBS I use the collections returned by the object.subfolders to iterate smoothly through all the files and subfolders. This is especially inportant once the recursion begins. In AutoIt, I'm not sure how to write the For Next loop. If I use an Array to store the files / folders, everything gets mucked up when the function starts calling itself.

I hope what I am trying to do makes sense; with luck I am missing something obvious.

Any thoughts?

Thanks in advance for any ideas.

Ravenlark

SearchDir = "C:\"

Call SystemSearch(SearchDir)

Function SystemSearch([ByRef SearchDir])

'******************************************
Dim oWSHShell       
Dim oFSO    
Dim Fol
Dim ThisFolder
Dim Subs
Dim Path
Dim FL
Dim FileList
Dim FilePath
Dim LogFile
'**********************************************
Set oWSHShell = WScript.CreateObject("WScript.Shell")       
Set oFSO = CreateObject("Scripting.FileSystemObject")   

Set LogFile = oFSO.OpenTextFile("C:\vbslog.txt",8,true)

Set ThisFolder = oFSO.GetFolder(SearchDir)
Set Subs = ThisFolder.Subfolders
Set FileList = ThisFolder.Files

    For Each FL in FileList
        If FL.Attributes >= 2048 Then
            FilePath = FL.Path
            LogFile.WriteLine FL.Path
            LogFile.WriteLine "Compressed"
        Else 
            LogFile.WriteLine FL.Path
            LogFile.WriteLine "NOT compressed"
        End If
    Next

LogFile.Close   

For Each Fol in Subs
Set LogFile = oFSO.OpenTextFile("C:\vbslog.txt",8,true)
    Path = Fol.Path
    SearchDir = Path
        If Fol.Attributes >= 2048 Then
            LogFile.WriteLine Fol.Path
            LogFile.WriteLine "Compressed"
        Else
            LogFile.WriteLine Fol.Path
            LogFile.WriteLine "NOT compressed"
        End If
    LogFile.Close   
    Call SystemSearch(SearchDir)
Next
End Function
Edited by Ravenlark

Ravenlark-----------------------------------------------------when you find yourself with the majority, its time to pause and reflect - Mark Twain

Link to comment
Share on other sites

@Ravenlark

Dim $oWSHShell      
Dim $oFSO   
Dim $Fol
Dim $ThisFolder
Dim $Subs
Dim $Path
Dim $FL
Dim $FileList
Dim $FilePath
Dim $LogFile

Dim $SearchDir = "C:\"

_SystemSearch($SearchDir)

Func _SystemSearch(ByRef $SearchDir)
 $oFSO = ObjCreate("Scripting.FileSystemObject")    
 $LogFile = $oFSO.OpenTextFile("C:\vbslog.txt",8,1)

 $ThisFolder = $oFSO.GetFolder($SearchDir)
 $Subs = $ThisFolder.Subfolders
 $FileList = $ThisFolder.Files
    For $FL in $FileList
        If $FL.Attributes >= 2048 Then
            $FilePath = $FL.Path
            $LogFile.WriteLine ($FL.Path)
            $LogFile.WriteLine ("Compressed")
        Else 
            $LogFile.WriteLine ($FL.Path)
            $LogFile.WriteLine ("NOT compressed")
        EndIf
    Next
$LogFile.Close ()

    For $Fol in $Subs
        $LogFile = $oFSO.OpenTextFile("C:\vbslog.txt",8,1)
        $Path = $Fol.Path
        $SearchDir = $Path
            If $Fol.Attributes >= 2048 Then
                $LogFile.WriteLine ($Fol.Path)
                $LogFile.WriteLine ("Compressed")
            Else
                $LogFile.WriteLine ($Fol.Path)
                $LogFile.WriteLine ("NOT compressed")
            EndIf
        $LogFile.Close ()
        _SystemSearch($SearchDir)
    Next
EndFunc

I hope this can help you out.

Enjoy !!

Edited by ptrex
Link to comment
Share on other sites

ptrex,

I had no idea I could use VBS (or presumably any other WSH) code directly in AutoIt!

Well looks like I was missing something obvious. Thanks for the help!

Ravenlark

Ravenlark-----------------------------------------------------when you find yourself with the majority, its time to pause and reflect - Mark Twain

Link to comment
Share on other sites

  • 4 months later...

Try this!

Func FileSearch($search_dir, $s_ext = '*.*', $sep_char = @CRLF, $last_line = False)
 Local $allfiles = ''
 $search_dir = StringReplace($search_dir & '\', '\\', '\')
 Local $search = FileFindFirstFile($search_dir & '*.*')
 While 1
  Local $file = FileFindNextFile($search)
  If $file = '' Then ExitLoop
  Local $full_file = $search_dir & $file
  Local $check_file = StringInStr(FileGetAttrib($full_file), 'D')
  If $check_file <> 0 Then $allfiles &= FileSearch($full_file, $s_ext, $sep_char, True)
  If $check_file = 0 Then
   If $s_ext = '*.*' Then $allfiles &= $full_file & $sep_char
   If $s_ext <> '*.*' And StringTrimLeft($s_ext, 1) = StringRight($full_file, StringLen(StringTrimLeft($s_ext, 1))) Then $allfiles &= $full_file & $sep_char
  EndIf
 WEnd
 FileClose($search)
 If Not $last_line Then $allfiles = StringTrimRight($allfiles, StringLen($sep_char))
 Return $allfiles
EndFunc   ;==>FileSearch
Link to comment
Share on other sites

  • Moderators

Try this!

Func FileSearch($search_dir, $s_ext = '*.*', $sep_char = @CRLF, $last_line = False)
 Local $allfiles = ''
 $search_dir = StringReplace($search_dir & '\', '\\', '\')
 Local $search = FileFindFirstFile($search_dir & '*.*')
 While 1
  Local $file = FileFindNextFile($search)
  If $file = '' Then ExitLoop
  Local $full_file = $search_dir & $file
  Local $check_file = StringInStr(FileGetAttrib($full_file), 'D')
  If $check_file <> 0 Then $allfiles &= FileSearch($full_file, $s_ext, $sep_char, True)
  If $check_file = 0 Then
   If $s_ext = '*.*' Then $allfiles &= $full_file & $sep_char
   If $s_ext <> '*.*' And StringTrimLeft($s_ext, 1) = StringRight($full_file, StringLen(StringTrimLeft($s_ext, 1))) Then $allfiles &= $full_file & $sep_char
  EndIf
 WEnd
 FileClose($search)
 If Not $last_line Then $allfiles = StringTrimRight($allfiles, StringLen($sep_char))
 Return $allfiles
EndFunc   ;==>FileSearch
http://www.autoitscript.com/forum/index.ph...c=33930&hl=

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...