-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCreate-FolderstructureReport.ps1
57 lines (47 loc) · 2.13 KB
/
Create-FolderstructureReport.ps1
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
57
# Get the current script's directory path
$rootFolder = $PSScriptRoot
$reportFile = "$rootFolder\folderstructurereport.txt"
# Helper function to display the folder structure
function Show-FolderStructure {
param (
[string]$folderPath, # Path of the folder to display
[int]$indentLevel = 0, # Indentation level for display
[string]$outputFile # File to output the folder structure
)
# Get all subdirectories and files within the folder
$directories = Get-ChildItem -Path $folderPath -Directory
$files = Get-ChildItem -Path $folderPath -File
# Build the lines to show
$prefix = '│ ' * $indentLevel # Vertical line with spaces for indentation
$lastItemPrefix = ' ' * $indentLevel # Space for last item (no vertical line)
# Display files first
$itemCount = $directories.Count + $files.Count
$currentItem = 0
foreach ($file in $files) {
$currentItem++
if ($currentItem -eq $itemCount) {
"$prefix└── $($file.Name)" | Out-File -FilePath $outputFile -Append
} else {
"$prefix├── $($file.Name)" | Out-File -FilePath $outputFile -Append
}
}
# Display directories, maintaining the structure
$currentItem = 0
foreach ($directory in $directories) {
$currentItem++
if ($currentItem -eq $itemCount) {
"$prefix└── $($directory.Name)/" | Out-File -FilePath $outputFile -Append
} else {
"$prefix├── $($directory.Name)/" | Out-File -FilePath $outputFile -Append
}
# Recursively display subdirectories
Show-FolderStructure -folderPath $directory.FullName -indentLevel ($indentLevel + 1) -outputFile $outputFile
}
}
# Initialize or clear the report file
Clear-Content -Path $reportFile -ErrorAction SilentlyContinue
# Output the folder structure
Write-Host "Generating folder structure for: $rootFolder"
"Folder structure for: $rootFolder" | Out-File -FilePath $reportFile -Append
Show-FolderStructure -folderPath $rootFolder -indentLevel 0 -outputFile $reportFile
Write-Host "Folder structure report generated at: $reportFile"