Advertisement
Lee_Dailey

Traverse-FolderTree.ps1

Jun 14th, 2016
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # file named Traverse-FolderTree.ps1
  2. #
  3.  
  4. Function Traverse-FolderTree
  5.     {
  6.     [CmdletBinding()]
  7.     Param
  8.         (
  9.         [Parameter (Mandatory = $True, Position = 1)]
  10.         [string]$FolderName,
  11.  
  12.         [switch]$NotFirstRun
  13.         )
  14.  
  15.     switch ($NotFirstRun)
  16.         {
  17.         $True {Break}
  18.         $False
  19.             {
  20.             # this creates [or resets] a persistent variable in the script scope.
  21.             # when the function finishes the total files & folders can be read outside the function.persistent
  22.             #<# using default Array type
  23.             $script:TFTResults = @{"Files"=@();
  24.                                    "Folders"=@();
  25.                                    "RecurseCount"=[int]0
  26.                                    "TopFolder"=[string]$FolderName}
  27.             #>
  28.  
  29.             <# using ArrayList instead of default Array type
  30.             $TFTResults = @{"Files"=[System.Collections.ArrayList]@();
  31.                             "Folders"=[System.Collections.ArrayList]@();
  32.                             "RecurseCount"=[int]0}
  33.             #>
  34.             }
  35.         }
  36.        
  37.     #<# array version
  38.     $FolderList = Get-ChildItem -LiteralPath $FolderName -Directory | Sort-Object directory
  39.     $TFTResults.Folders += $FolderList
  40.    
  41.     $FileList = Get-ChildItem -LiteralPath $FolderName -File | Sort-Object directory
  42.     $TFTResults.Files += $FileList
  43.     #>
  44.    
  45.     <# arraylist version
  46.     [System.Collections.ArrayList]$FolderList = Get-ChildItem -LiteralPath $FolderName -Directory | Sort-Object directory
  47.     if ($FolderList.Count -gt 0)
  48.         {
  49.         $TFTResults.Folders.AddRange($FolderList)
  50.         }
  51.  
  52.     [System.Collections.ArrayList]$FileList = Get-ChildItem -LiteralPath $FolderName -File | Sort-Object directory
  53.     if ($FileList.Count -gt 0)
  54.         {
  55.         $TFTResults.Files.AddRange($FileList)
  56.         }
  57.     #>
  58.    
  59.  
  60.     # show what is happening - replace the following with your prefered activity indicator
  61.     #
  62.  
  63.     <# details
  64.     Write-Output ("")
  65.     Write-Output ("Current folder name   = $FolderName")
  66.     Write-Output ("Current folder depth  = {0,7:N0}" -f $($FolderName.Split("\").Count))
  67.     Write-Output ("")
  68.     Write-Output ("Folders here          = {0,7:N0}" -f $($FolderList.count))
  69.     Write-Output ("Files here            = {0,7:N0}" -f $($FileList.count))
  70.     Write-Output ("")
  71.     Write-Output ("Recurse count         = {0,7:N0}" -f $($TFTResults.RecurseCount))
  72.     Write-Output ("Total folder count    = {0,7:N0}" -f $($TFTResults.Folders.Count))
  73.     Write-Output ("Total file count      = {0,7:N0}" -f $($TFTResults.Files.Count))
  74.     Write-Output ("**********")
  75.     Write-Output ("")
  76.     #>
  77.  
  78.     #<# progress bar
  79.     # this will bounce around somewhat as it climbs up and down the folder tree
  80.     $Percentage = "{0:N0}" -f (($TFTResults.RecurseCount / $TFTResults.Folders.Count) * 100)
  81.     Write-Progress -Activity "Traversing..." -Status "Percent Complete $Percentage%" -PercentComplete $Percentage
  82.     #>
  83.  
  84.     #
  85.     #
  86.  
  87.  
  88.     foreach ($Folder in $FolderList)
  89.         {
  90.         $TFTResults.RecurseCount += 1
  91.         Traverse-FolderTree $Folder.FullName -NotFirstRun
  92.         }
  93.     }# end of Function Traverse-FolderTree
  94.  
  95.  
  96.  
  97. ##########
  98. #$StartingFolder = "D:\New_Downloads\__By-Artist"
  99. # Total folder count     =     118
  100. # Total file count       =   1,610
  101. # - works with both ARRAY and ARRAYLIST
  102. #
  103. #$StartingFolder = "D:\New_Downloads\__By-Artist\Jackie Gleason"
  104. #$StartingFolder = "D:\New_Downloads\__By-Artist\Jackie Gleason\Jackie Gleason - The Romantic Moods of Jackie Gleason 1 (1996)"
  105.  
  106. #$StartingFolder = "Z:\By_Artist"
  107. # Total folder count  = 8,465
  108. # Total file count    = 75,193
  109. # - fails with ARRAYLIST
  110. # measure-command TFT = 124.141 seconds [with sort-object]
  111. # measure-command TFT = 105.200 seconds [WITHOUT sort-object]
  112. # measure-command GC  =  12.783 seconds
  113. # - GC is faster by about 10x
  114. #
  115. #$StartingFolder = "Z:\By_Artist\Adam_&_the_Ants"
  116. #$StartingFolder = "Z:\By_Artist\Adam_&_the_Ants\Antbox_[Disk_1_of_3]"
  117. #
  118.  
  119. $StartingFolder = $env:ProgramFiles
  120. # Total folder count     =   3,109
  121. # Total file count       =  29,317
  122. # - this one bounces the progress bar around enormously
  123. #
  124. #$StartingFolder = $env:CommonProgramFiles
  125. ##########
  126.  
  127.  
  128.  
  129.  
  130. #Measure-Command {Get-ChildItem -LiteralPath $StartingFolder -Recurse}
  131. #Measure-Command {Traverse-FolderTree $StartingFolder}
  132. Traverse-FolderTree $StartingFolder
  133.  
  134. Write-Output ("")
  135. Write-Output ("Starting folder        = $($TFTResults.TopFolder)")
  136. Write-Output ("Total folder count     = {0,7:N0}" -f $($TFTResults.Folders.Count))
  137. Write-Output ("Total file count       = {0,7:N0}" -f $($TFTResults.Files.Count))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement