Jump to content
Sign in to follow this  

Enumerate Files

Recommended Posts


My first posting to this site, thought I would contribute a UDF that seems pretty useful to me.

This enumerates all files in a directory (and subdirectories if desired). The depth of subfolders is limited to the number of files that can be open at once in AutoIt.

Also included is a function to resize an array on the fly.

Excuse all the comments but I prefer to do too much instead of too little! :ph34r:

; Description:      Enumerates all files in the specified folder and subfolders
;        (if required), along with size of all files
; Syntax:           EnumFiles( $folder, $recurse )
; Parameter(s):     $folder - Path to the folder to get files from
;        $recurse - 0 to exclude subfolders, 1 to include subfolders
; Requirement(s):   None
; Return Value(s):  On Success - Returns array containing files
;         array[0] contains number of files
;         array[array[0] + 1] contains size of all files
;                   On Failure - Returns 0 and sets @ERROR = -1
; Author(s):        Sean Hart (autoit@hartmail.ca)
; Note(s):          None
func EnumFiles ($folder, $recurse)
; Declare variables
    Dim $FileList [100], $curdir, $arraysize, $filesize, $curdir, $x, $search, $file

; Keep track of size of array
    $arraysize = 100

; Keep track of size of each file
    $filesize = 0

; Remember the current directory
    $curdir = @WorkingDir

; Check that the folder exists, otherwise return 0 and set errorlevel
    if not FileExists ($folder & "\") then
  SetError (-1)
  Return 0

; Change the directory to the specified folder
    FileChangeDir ($folder)

; Start searching the current folder for files & directories
    $search = FileFindFirstFile ("*.*")
    $file = FileFindNextFile ($search)

; While files are found, loop
    while @error = 0

    ; if a directory is found, process it as such
  if (StringInStr (FileGetAttrib ($file), "D")) then

  ; If recurse options is used and directory is not a root folder, process it
     if $recurse and ($file <> ".") and ($file <> "..") then

     ; Recursively call this function to return an array of files in the subfolder(s)
    $subfiles = EnumFiles ($folder & "\" & $file, 1)

     ; Check number of files returned, and resize array if required
    if $subfiles[0] + 2 > $arraysize then
     $arraysize = $arraysize + $subfiles[0] + 100
     ResizeArray ($FileList, $arraysize)

     ; Loop through array of files in subfolder and add to current file list
    for $x = 1 to $subfiles[0]

    ; If number of files reaches array size, resize it
     if $FileList[0] + 1 = $arraysize then
      $arraysize = $arraysize + 100
      ResizeArray ($FileList, $arraysize)

    ; Add 1 to number of files and add file to file list
     $FileList[0] = $FileList[0] + 1
     $FileList[$FileList[0]] = $subfiles[$x]

     ; Add size of subfolder to size of current file list
    $filesize = $filesize + $subfiles[$subfiles[0] + 1]

    ; If a file is found (not a directory), process it

  ; If number of files reaches array size, resize it
     if $FileList[0] + 1 = $arraysize then
    $arraysize = $arraysize + 100
    ResizeArray ($FileList, $arraysize)

  ; Add 1 to number of files
     $FileList[0] = $FileList[0] + 1

  ; Add full path to file to the file list
     $FileList[$FileList[0]] = $folder & "\" & $file

  ; Add size of file to the total file size
     $filesize = $filesize + FileGetSize ($file)

    ; Find next file
  $file = FileFindNextFile ($search)

; Close search handle
    FileClose ($search)

; Add size of all files to array
    $FileList[$FileList[0] + 1] = $filesize

; Change back to the original directory
    FileChangeDir ($curdir)

; Return array of files
    Return $FileList

; Description:      Resizes an array to the specified size
; Syntax:           ResizeArray( $array, $size )
; Parameter(s):     $array - Array to resize (array)
;        $size - New size of array (integer)
; Requirement(s):   array(0) must hold current size of array or position of last
;        value.
; Return Value(s):  none
; Author(s):        Sean Hart (autoit@hartmail.ca)
; Note(s):          If new size is smaller than old size, any entries beyond new
;        size will be lost.
func ResizeArray (ByRef $array, $size)
; Declare variables
    Dim $temparray[$size], $x, $numentries

; Find which size is bigger (old or new), figure out how many entries to copy
    if $array[0] > $size then
  $numentries = $size
  $numentries = $array[0]

; Loop through original array copying each entry to temporary array
    for $x = 0 to $numentries
  $temparray[$x] = $array[$x]

; Clear original array
    $array = 0

; Copy entire temporary array back to original array, so it takes on new diminension
    $array = $temparray

; Clear temporary array
    $temparray = 0

Share this post

Link to post
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
Sign in to follow this  


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.