-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-ChildItemsProgress.ps1
More file actions
56 lines (43 loc) · 2.01 KB
/
Get-ChildItemsProgress.ps1
File metadata and controls
56 lines (43 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<#
.SYNOPSIS
Recursively retrieves non-directory items within a specified folder and its subdirectories.
.DESCRIPTION
This script recursively explores the specified folder and its subdirectories,
outputting non-directory items and calculating progress based on the total number of subdirectories.
.PARAMETER FolderPath
Specifies the folder path to start the recursive operation.
.EXAMPLE
Get-ChildItemsProgress -FolderPath "C:\Path\To\Your\Folder"
Runs the script on the specified folder and its subdirectories.
#>
function Get-ChildItemsRec {
param (
[Parameter(Mandatory = $true)][string]$FolderPath,
[bool]$noProgress = $false
)
function Get-ItemsRecursively {
param (
[Parameter(Mandatory = $true)][string]$Path,
[Parameter(Mandatory = $true)][double]$SubProgressStart,
[Parameter(Mandatory = $true)][double]$SubProgressEnd
)
$items = Get-ChildItem -Path $Path -Force
$subdirectoryCount = ($items | Where-Object { $_.PSIsContainer }).Count
$subdirectorySlice = ($SubProgressEnd - $SubProgressStart) / $subdirectoryCount
$subProgress = $SubProgressStart
foreach ($item in $items) {
if (-not $item.PSIsContainer) {
Write-Output $item
}
if ($item.PSIsContainer) {
if (-not $noProgress) {
$status = "$item............................................................................................................"
Write-Progress -Activity ("{0:N2}%" -f $subProgress) -Status $status -PercentComplete $subProgress
}
Get-ItemsRecursively -Path $item.FullName -SubProgressStart $subProgress -SubProgressEnd ($subProgress + $subdirectorySlice)
$subProgress += $subdirectorySlice
}
}
}
Get-ItemsRecursively -Path $FolderPath -SubProgressStart 0 -SubProgressEnd 100
}