Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # file named Traverse-FolderTree.ps1
- #
- Function Traverse-FolderTree
- {
- [CmdletBinding()]
- Param
- (
- [Parameter (Mandatory = $True, Position = 1)]
- [string]$FolderName,
- [switch]$NotFirstRun
- )
- switch ($NotFirstRun)
- {
- $True {Break}
- $False
- {
- # this creates [or resets] a persistent variable in the script scope.
- # when the function finishes the total files & folders can be read outside the function.persistent
- #<# using default Array type
- $script:TFTResults = @{"Files"=@();
- "Folders"=@();
- "RecurseCount"=[int]0
- "TopFolder"=[string]$FolderName}
- #>
- <# using ArrayList instead of default Array type
- $TFTResults = @{"Files"=[System.Collections.ArrayList]@();
- "Folders"=[System.Collections.ArrayList]@();
- "RecurseCount"=[int]0}
- #>
- }
- }
- #<# array version
- $FolderList = Get-ChildItem -LiteralPath $FolderName -Directory | Sort-Object directory
- $TFTResults.Folders += $FolderList
- $FileList = Get-ChildItem -LiteralPath $FolderName -File | Sort-Object directory
- $TFTResults.Files += $FileList
- #>
- <# arraylist version
- [System.Collections.ArrayList]$FolderList = Get-ChildItem -LiteralPath $FolderName -Directory | Sort-Object directory
- if ($FolderList.Count -gt 0)
- {
- $TFTResults.Folders.AddRange($FolderList)
- }
- [System.Collections.ArrayList]$FileList = Get-ChildItem -LiteralPath $FolderName -File | Sort-Object directory
- if ($FileList.Count -gt 0)
- {
- $TFTResults.Files.AddRange($FileList)
- }
- #>
- # show what is happening - replace the following with your prefered activity indicator
- #
- <# details
- Write-Output ("")
- Write-Output ("Current folder name = $FolderName")
- Write-Output ("Current folder depth = {0,7:N0}" -f $($FolderName.Split("\").Count))
- Write-Output ("")
- Write-Output ("Folders here = {0,7:N0}" -f $($FolderList.count))
- Write-Output ("Files here = {0,7:N0}" -f $($FileList.count))
- Write-Output ("")
- Write-Output ("Recurse count = {0,7:N0}" -f $($TFTResults.RecurseCount))
- Write-Output ("Total folder count = {0,7:N0}" -f $($TFTResults.Folders.Count))
- Write-Output ("Total file count = {0,7:N0}" -f $($TFTResults.Files.Count))
- Write-Output ("**********")
- Write-Output ("")
- #>
- #<# progress bar
- # this will bounce around somewhat as it climbs up and down the folder tree
- $Percentage = "{0:N0}" -f (($TFTResults.RecurseCount / $TFTResults.Folders.Count) * 100)
- Write-Progress -Activity "Traversing..." -Status "Percent Complete $Percentage%" -PercentComplete $Percentage
- #>
- #
- #
- foreach ($Folder in $FolderList)
- {
- $TFTResults.RecurseCount += 1
- Traverse-FolderTree $Folder.FullName -NotFirstRun
- }
- }# end of Function Traverse-FolderTree
- ##########
- #$StartingFolder = "D:\New_Downloads\__By-Artist"
- # Total folder count = 118
- # Total file count = 1,610
- # - works with both ARRAY and ARRAYLIST
- #
- #$StartingFolder = "D:\New_Downloads\__By-Artist\Jackie Gleason"
- #$StartingFolder = "D:\New_Downloads\__By-Artist\Jackie Gleason\Jackie Gleason - The Romantic Moods of Jackie Gleason 1 (1996)"
- #$StartingFolder = "Z:\By_Artist"
- # Total folder count = 8,465
- # Total file count = 75,193
- # - fails with ARRAYLIST
- # measure-command TFT = 124.141 seconds [with sort-object]
- # measure-command TFT = 105.200 seconds [WITHOUT sort-object]
- # measure-command GC = 12.783 seconds
- # - GC is faster by about 10x
- #
- #$StartingFolder = "Z:\By_Artist\Adam_&_the_Ants"
- #$StartingFolder = "Z:\By_Artist\Adam_&_the_Ants\Antbox_[Disk_1_of_3]"
- #
- $StartingFolder = $env:ProgramFiles
- # Total folder count = 3,109
- # Total file count = 29,317
- # - this one bounces the progress bar around enormously
- #
- #$StartingFolder = $env:CommonProgramFiles
- ##########
- #Measure-Command {Get-ChildItem -LiteralPath $StartingFolder -Recurse}
- #Measure-Command {Traverse-FolderTree $StartingFolder}
- Traverse-FolderTree $StartingFolder
- Write-Output ("")
- Write-Output ("Starting folder = $($TFTResults.TopFolder)")
- Write-Output ("Total folder count = {0,7:N0}" -f $($TFTResults.Folders.Count))
- Write-Output ("Total file count = {0,7:N0}" -f $($TFTResults.Files.Count))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement