This page as PDF

AutoIT – small script to list all files in a directory

This is an image of SciTE. The source code in ...
Image via Wikipedia

I’d like to show you a simple piece of code for AutoIt. First, you HAVE to know AutoIT, because it’s very simple to develop with, and it’s very useful. The aim of AutoIt is to generate small executable files that help you automatizing tasks, like renaming, processing XSLT on an XML file, etc.

When you install AutoIt, you install the libraries needed to compile the scripts you will develop, and Scite, the integrated text editor based on Scintilla with autocompletion, syntax coloring, compiling options etc. After installing you may want to start a small tutorial to familiarize with the development environment. And you’re lucky, because the AutoIt documentation is very good.

From now, I will assume you have some basic programmation skills, and that you’re familiarized with the environment. So let’s code.

First, we will get the files listed under the folder we explore. (SORRY, NO SYNTAX BRUSH FOR AUTOIT OR AUTOHOTKEY FOR NOW, I’LL HAVE TO CREATE ONE)

#include <Array.au3>
Func getFileList($folderName)
 $fileArray = _FileListToArray($folderName)
 _ArrayDisplay($fileArray)
EndFunc

Yes, it is THAT simple. If you call the function with for example

getFileList(@ScriptDir)

you will get a pop-up that will list all files AND folders under that path (@ScriptDir is just the directory where is your script, you can try with @MyDocumentsDir etc.). So now, what we need is diferentiate folders from files, and then call recursively the function for each folder.
To do that, there’s several solutions. You can for example ask for the size of the file, and if the script throws you an error, you know it’s a folder. But, the best solution might be to retrieve information from the path thanks to the FileGetAttrib function and check if it contains a ‘D’

#include <Array.au3>
#include <File.au3>
Func getFileList($folderName)
 Local $fileArray, $i, $fullFilePath, $fileAttributes

 $fileArray = _FileListToArray($folderName)

 For $i=1 To $fileArray[0]
 $fullFilePath = $folderName & "\" & $fileArray[$i] ;retrieve the full path
 $fileAttributes = FileGetAttrib($fullFilePath)
 If StringInStr($fileAttributes,"D") Then ;folder, have to explore
 ConsoleWrite("FOLDER - " & $fullFilePath & @CRLF)
 getFileList($fullFilePath) ;recursive call
 Else ;file
 ConsoleWrite("FILE - " & $fullFilePath & @CRLF)
 EndIf
 Next
EndFunc

Finally, we come with this piece of code. The files are listed, stored in an array, and returned. Maybe not very useful, but it was a good introduction to AutoIt, isn’t it? This code is not perfect, we should for example verify if the directory we explore is not empty, otherwise the ‘For’ loop would stop with an error.

#include <Array.au3>
#include <File.au3>

Func getFileList($folderName)
 Local $fileArray, $i, $fullFilePath, $fileAttributes
 Local $filePathArray[1]=[0]
$fileArray = _FileListToArray($folderName)

 For $i=1 To $fileArray[0]
  $fullFilePath = $folderName & "\" & $fileArray[$i] ;retrieve the full path
  $fileAttributes = FileGetAttrib($fullFilePath)
  If StringInStr($fileAttributes,"D") Then ;folder, have to explore
   Dim $tempArray = getFileList($fullFilePath) ;recursive call
   _ArrayConcatenate($filePathArray,$tempArray,1) ;add returned results to already found files
  Else ;file
   _ArrayAdd($filePathArray, $fullFilePath) ;add result to already found files
  EndIf
 Next
 $filePathArray[0] = UBound($filePathArray) - 1 ;adjust the size of the array
 Return $filePathArray
EndFunc

Next time we will write that result and some statistics (lines of code, last modified…), with some file type control (image/text etc.), in a text file. It can give you an overview of some statistics of your project quickly.

    • Solid Snake
    • May 13th, 2010

    This is good one.
    But this line “$i=1 To $fileArray[0]” gives error. I think you better use condition in this way
    “$i=1 To Ubount($fileArray) -1″
    Can you put the out put into a tree structure. I tried but its not comming.
    Please help

  1. February 1st, 2010