Tuesday, April 28, 2009

KiXtart Script: Enumerate Files in Folders and Sub-Folders

This is the third and final script post-of-the-day. This one simply enumerates all files of a given type (i.e. extension) within a specified folder, and all sub-folders beneath it. It recurses the folder you start with and crawl down into all the nooks and crannies like ants finding their way into your kitchen in the Summer time.


;----------------------------------------------------------------
; Filename..: Enum_Files.kix
; Author....: David M. Stein
; Date......: 04/28/09
; Purpose...: recurse all sub-folders and files within each
;----------------------------------------------------------------
Break ON

If Not $StartPath
$=ShowUsage()
Else
If Left($StartPath,1) = "%"
$StartPath = ExpandEnvironmentVars("%ProgramFiles%")
EndIf
EndIf

If Exist($StartPath)
? "computer...: "+@wksta
? "date.......: "+@date
$FileSpec = "exe"
$=Enum_SubFolders($StartPath)
Else
? "error: folder path not found"
EndIf

;----------------------------------------------------------------
; comment:
;----------------------------------------------------------------

Function Enum_SubFolders($Path)
Dim $FileName
$FileName = Dir("$Path\*.*")
While $FileName <> "" And @ERROR = 0
If $FileName <> "." And $FileName <> ".."
If GetFileAttr("$Path\$FileName") & 16
? "$Path\$FileName"
$=Enum_Files("$Path\$FileName", $FileSpec)
$=Enum_SubFolders("$Path\$FileName")
EndIf
EndIf
$FileName = Dir() ; retrieve next file/folder
Loop
EndFunction

;----------------------------------------------------------------
; comment:
;----------------------------------------------------------------

Function Enum_Files($Path, $Ext)
Dim $fName
$fName = Dir("$Path\*.$Ext")
While $fName <> "" And @ERROR = 0
? Chr(9)+"filename...: $fName"
? Chr(9)+Chr(9)+"file size......: "+GetFileSize("$Path\$fName")
? Chr(9)+Chr(9)+"date created...: "+GetFileTime("$Path\$fName", 1)
? Chr(9)+Chr(9)+"company name...: "+
GetFileVersion("$Path\$fName", "CompanyName")
? Chr(9)+Chr(9)+"product name...: "+
GetFileVersion("$Path\$fName", "ProductName")
? Chr(9)+Chr(9)+"product version: "+
GetFileVersion("$Path\$fName", "ProductVersion")
$fName = Dir()
Loop
EndFunction

Function ShowUsage()
?
? "Usage: enum_files.kix $$StartPath=[PATH]"
?
EndFunction

No comments: