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
;Recursive File Lister Dim $FolderName = "C:\WINDOWS\SYSTEM32" Dim $FileCount = 0 ScanFolder($FolderName) MsgBox(0,"Done","Folder Scan Complete. Scanned " & $FileCount & " 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 LogFile($FullFilePath) EndIf WEnd FileClose($Search) EndFunc Func LogFile($FileName) FileWriteLine(@ScriptDir & "\FileList.txt",$FileName) $FileCount += 1 ToolTip($FileName,0,0) EndFunc
Download this code: 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.
This is the overall controlling program. This portion is relatively short. It is only four lines:
Dim $FolderName = "C:\WINDOWS\SYSTEM32"
Dim $FileCount = 0
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.
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
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
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.
|Trackback link - http://www.dailycupoftech.com/folder-recursion-in-autoit/trackback/|