Folder Recursion in AutoIt

Trackback or

Having the ability to perform an action on every single file in a folder and all subsequent subfolders is a very powerful tool. This means that someone could potentially perform an action on every single file on an entire hard drive volume with only a few lines of code.

The purpose of this article is to provide a framework for folder recursion using AutoIt. I will also provide you with an example script that you can download and run to see how folder recursion works.

The File FileLister.au3

Error: Could not open FileLister.au3

This is the script that we are going to be looking at. It is a relatively basic script but it is very effective at what it does.

This script’s job is to scan all of the files and folders in a specified directory, display them in a tooltip, log each of the file names in a text file, and count the number of files that exist.

There are three main parts to this script. Each with a specific purpose.

Main Program

This is the overall controlling program. This portion is relatively short. It is only four lines:

Dim $FolderName = "C:\WINDOWS\SYSTEM32"
Dim $FileCount = 0

ScanFolder($FolderName)

MsgBox(0,"Done","Folder Scan Complete. Scanned " & $FileCount & " Files")

The first two lines tells us the folder to scan and initializes the file count. The third line starts the scanning process and the forth line tells the user that the scan has completed and how many files were in the scanned folder.

ScanFolder

This function is the core of the program. It is important that this is a function because we need to recursively call this function.

When the ScanFolder function looks at the contents of a folder, it determines whether each item is a folder or not. If it is not a folder, it performs the specified function. If it is a folder, it runs another copy of the ScanFolder function but with the new folder name. This is accomplished by the following code:

If StringInStr($FileAttributes,"D") Then
ScanFolder($FullFilePath)
Else
LogFile($FullFilePath)
EndIf

LogFile

This is the function that is called if the ScanFolder function determines that the item that it has found is a file. This function can be modified to do all types of things and I encourage you to experiment with different things that you could do with the file. Some ideas are:

  • record file size
  • set file attributes
  • rename files
  • MD5 on files
  • catalog files

Conclusion

That’s all there is to it. It is actually a relatively simple process. I would be very interested to hear how you modified this script and made it perform various functions. Feel free to leave your creation ideas and code in the comments section.

If you found this post useful, why don't you buy me a cup of coffee to show your gratitude?

Trackback link - http://www.dailycupoftech.com/folder-recursion-in-autoit/trackback/
Tim Fehlman

7 Responses to “Folder Recursion in AutoIt”

  1. daddydave Says:

    Here’s how I used and modified your script:

    http://www.msfn.org/board/index.php?showtopic=89021

    It goes through a folder tree and modifies INF files and puts them in an output folder tree.

    Thanks for the sharing your code!

  2. Daily Cup of Tech Says:

    Create a Self-Burning ISO with AutoIt Creating a Basic TrueCrypt Volume on a USB Drive Creating Windows Shares on FreeNAS DCoT Discounts DCoT Webmaster Tools Disable IE7 Installation Via Windows Update Folder Recursion in AutoIt FreeNAS Basic Configuration FreeNAS System and Skill Requirements Have Your Lost USB Drive Ask For Help How To Be A Better Blogger HowTo Backup Your Website Files HowTo Install FreeNAS Infrastructure Automation Primer

  3. Constantine Georgantzas Says:

    My employer wants me to compress 14000 pdf files and has only shown me how to do it one at a time with Adobe Acrobat. I sought help from other software, but could find none. So I have recently turned to AutoIt for assistance. How can someone modify your code so as to compress every pdf file inside a company directory, hidden away in thousands of subfolders?

  4. Constantine Georgantzas Says:

    My job as an intern is to compress 14000 pdf files. I have been shown how to do so only one at a time, which I refuse to do. I searched on-line for software to help me in my task, but then at last I heard of AutoIt. However, I am still learning so can you send me a version of your script with more detailed comments so that I may understand it? Or do you have any suggestions for altering your script so that it may complete my task? All the files are in a company directory, hidden away in thousands of subfiles. I will also need to select certain files of a certain size, that were modified before a certain date, and that have not been compressed before.

  5. Jasen Webster Says:

    I basically used it to move all files that meet a certain search string criteria to another folder. Problem I had was having it run for subsequent folders. Your script made that happen. Thank you!!!

    Here’s my code.
    ;Recursive File Lister

    Dim $FolderName = ‘M:\archive\target’
    Dim $FileCount = 0
    Dim $FileDelete = 0
    Dim $FileRename = 0

    ScanFolder($FolderName)

    MsgBox(0,’Done’,'File Move Completed.’ & @TAB & ‘Moved ‘ & $FileRename & ‘ Files’)

    Func ScanFolder($SourceFolder)
    Local $Search
    Local $File
    Local $FileAttributes
    Local $FullFilePath

    $Search = FileFindFirstFile($SourceFolder & ‘\*.*’)

    While 1
    If $Search = -1 Then
    ExitLoop
    EndIf

    $File = FileFindNextFile($Search)
    If @error Then ExitLoop

    $FullFilePath = $SourceFolder & ‘\’ & $File
    $FileAttributes = FileGetAttrib($FullFilePath)

    If StringInStr($FileAttributes,’D') Then
    ScanFolder($FullFilePath)
    Else
    HandleFile($FullFilePath)
    EndIf

    WEnd

    FileClose($Search)
    EndFunc

    Func HandleFile($FileName)
    Local $NewFile
    Local $Date
    If StringRegExp($FileName, ‘[.]…_[(][0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{2}-[0-9]{2}-[0-9]{2}_DEL_T’,0) Then
    $Date = FileGetTime($FileName, 1, 1)
    $NewFile = StringReplace($FileName, StringRight($FileName,32), ”, 0, 2)
    $NewFile = StringReplace($NewFile, ‘ [#]’, ”, 0, 2)
    $NewFile = StringReplace($NewFile, ‘ [-]’, ”, 0, 2)
    $NewFile = StringReplace($NewFile, ‘ [#][-]’, ”, 0, 2)
    $NewFile = StringReplace($NewFile, $FolderName, ‘M:\My Music’, 0, 2)
    FileWriteLine(@ScriptDir & ‘\FileMove.txt’,$NewFile)
    FileMove($FileName, $NewFile, 9)
    If @error Then FileWriteLine(@ScriptDir & ‘\FileMove.txt’, @error)
    FileSetTime($NewFile, $Date, 1)
    $FileRename += 1
    ToolTip($FileName & @CRLF & $NewFile,0,0,’Moving Files’)
    EndIf
    EndFunc

  6. Pete Says:

    Thanks so much. This script comes in handy all the time. I use it for backing up files, and all kinds of text processing.

  7. paul Says:

    Your script is great and i simply added some tags and an if condition to dump into a playlist for automating a limited application. Thanks.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>