Tuesday, August 5, 2008

DirList – Directory enumerator / Excel report maker

This script will enumerate a folder and create an excel spreadsheet based on it.

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_outfile=DirList.exe
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#cs
    DirList
        Alex Belenkiy - ab@nyitsupport.com
        08/05/2008
        
        This script was created with the purpose of enumerating a folder and its contents into an excel file.
        To use just select the starting path and wait for the Excel sheet to show up.
        Make sure the machine this is executed on has Excel installed.
        
        RecursiveFileSearch function was modified from http://www.autoitscript.com/forum/index.php?showtopic=58558&st=0
#ce
 
#include <array.au3>
#include <file.au3>
#include <GUIConstants.au3>
 
;AutoItSetOption("TrayIconHide", 1)
 
$Path = FileSelectFolder("Please select the folder to enumerate", "",4)
if @error then Exit
$excel = ObjCreate("Excel.Application")
$excel.visible = 0
$excel.workbooks.add
$row = 1
$col = 1
with $excel.activesheet
    .cells($row,$col).value = "Listing of " & $Path
    .cells($row,$col).font.bold = True
    .cells($row,$col).font.size = 20
    .cells($row + 3,$col).value = $Path
    .cells($row + 3,$col).font.bold = True
    $row = $row + 4
    $col = $col + 1
EndWith
 
 
$Array = RecursiveFileSearch($Path)
;_ArrayDisplay($Array)
$excel.visible = 1
;$excel.activeworkbook.saveas("Shared.xls")
$excel = 0
 
Func RecursiveFileSearch($RFSstartDir, $RFSpattern = ".", $RFSdepth = 0)
    ;Ensure starting folder has a trailing slash
    If StringRight($RFSstartDir, 1) <> "\" Then $RFSstartDir &= "\"
 
    If $RFSdepth = 0 Then
        ;Get count of all files in subfolders for initial array definition
        $RFSfilecount = DirGetSize($RFSstartDir, 1)
        Global $RFSarray[$RFSfilecount[1] + 1]
    EndIf
   
    $RFSsearch = FileFindFirstFile($RFSstartDir & "*.*")
    If @error Then Return
 
    ;Search through all files and folders in directory
    While 1
        $RFSnext = FileFindNextFile($RFSsearch)
        If @error Then ExitLoop
       
        ;If folder, recurse
        If StringInStr(FileGetAttrib($RFSstartDir & $RFSnext), "D") Then
            with $excel.activesheet
                .cells($row,$col).font.bold = True
                .cells($row,$col).value = $RFSnext
                $row = $row + 1
                $col = $col + 1
            EndWith
            RecursiveFileSearch($RFSstartDir & $RFSnext, $RFSpattern, $RFSdepth + 1)
            $col = $col - 1
        Else
            If StringRegExp($RFSnext, $RFSpattern, 0) Then
                with $excel.activesheet
                    .cells($row,$col).value = $RFSnext
                    $row = $row + 1
                EndWith
            EndIf
        EndIf
    WEnd
    FileClose($RFSsearch)
 
    If $RFSdepth = 0 Then
        Redim $RFSarray[$RFSarray[0] + 1]
        Return $RFSarray
    EndIf
EndFunc   ;==>RecursiveFileSearch

No comments: