I defined the "__New()" method for the "FileContainer" sub class with two parameters: 1) the path of the drive (like C:\) or folder to add and 2) the number of levels of sub folders to scan. The "__New()" method is calling a new custom method named "getFilesInFolder()" that will collect the files and folders under the root path.
Code: Select all
class FileContainer extends FileSystem.FileSystemElement {
__New( path, recurseLevels := 1 ) {
; path -> folder or drive root to scan
; recurseLevels -> number of folder levels to scan (default 1 this path only, -1 to scan all, 0 to scan none)
if ( !fileExist( path ) )
Throw exception( "Path """ . path . """ doesn't exist", "__New", "Exist test returned false" )
if ( !inStr( fileExist( path ), "D" ) ) ; if file is not a folder or a drive
Throw exception( "Error creating File", "__New", "Path does not points to Folder" )
this.name := path
if ( SubStr( path, 0, 1 ) = "\" ) ; remove ending backslash for drive roots (like "C:\")
path := SubStr( path, 1, StrLen( path ) - 1 )
if !( this.getFilesInFolder( path, recurseLevels ) )
Throw exception( "Error getting Items", "__New", "Could not get items from container" )
}
In addition, for sub folders, the method "getFilesInFolder()" is called recursively for the sub folder with the "recurseLevels" decremented to keep track the number of levels scanned. The recursion continues as long as "recurseLevels" is positive. It stops when it gets down to zero. And if "recurseLevels" starts -1 (and lower with recursive calls), scan continues until the end of the directory branch.
Code: Select all
getFilesInFolder( thisPath, recurseLevels ) {
; thisPath -> folder or drive root to scan
; recurseLevels -> number of folder levels to scan including this one
if ( showToolTip )
ToolTip, Getting files from:`n%thisPath%
; if recurseLevels > 0 continue with sub folder
; if recurseLevels < 0 continue until the end of branch
; if recurseLevels = 0 stop recursion
if ( recurseLevels = 0 )
return true
this.Items := Object() ; create an object to contain items (files and folders)
Loop, Files, % thisPath . "\*.*", FD ; do not use "R" here, the class does the recursion below
{
if A_LoopFileAttrib contains H,S ; skip hidden or system files
continue
if A_LoopFileAttrib contains D ; this is a folder, create Directory object and recurse to sub level
objItem := new FileSystem.Directory( A_LoopFileFullPath, recurseLevels - 1 ) ; "- 1" to track the number of levels
else ; this is a file, create File object
objItem := new FileSystem.File( A_LoopFileFullPath )
this.addItem(objItem) ; add Directory or File object to Items container
}
return true
}
Code: Select all
addItem(objItem) {
this.Items.InsertAt(this.Items.Length()+ 1, objItem) ; add Directory or File object to Items container
}
Code: Select all
listFiles( filter := "", recurseLevels := -1 ) {
; filter -> exclude items with filter in their name, default empty (include all items)
; recurseLevels -> number of folder levels to scan (default -1 to scan all, 0 to scan none)
; if recurseLevels > 0 continue with sub folder
; if recurseLevels < 0 continue until the end of branch
; if recurseLevels = 0 stop recursion
if (recurseLevels = 0)
return
thisList := ""
for intKey, objItem in this.Items {
if !StrLen(filter) or InStr(objItem.name, filter)
thisList .= objItem.name . "`n"
if ( objItem.HasKey( "Items" ) ) ; this is a container, recurse
thisList .= objItem.listFiles( filter, recurseLevels - 1 ) ; "- 1" to track the number of levels
}
return thisList
}
Code: Select all
global showTooltip := True
MyFileSystem := new FileSystem ; create my instance of the class
MyNewContainer := new MyFileSystem.Directory( ) ; create a container for the specified folder and get files in this folder only
if ( showTooltip )
ToolTip ; hide last tooltip
; get content from the MyNewContainer object
str := MyNewContainer.listFiles() ; get the list of files and folders contained in the MyNewContainer object
; show content in Gui
Gui, Add, Edit, w800 r25 ReadOnly, % SubStr( str, 1, 30000 ) . ( StrLen( str ) > 30000 ? "`n..." : "" ) ; limit because of 32k limit of Edit control
Gui, Add, Button, default, Close
GuiControl, Focus, Close
Gui, Show
return
ButtonClose:
ExitApp
I will post the full source of the modified class in the next message.