-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDirPrinter.ps1
112 lines (89 loc) · 3.87 KB
/
DirPrinter.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# Check if the required number of arguments was provided
if ($args.Count -lt 2)
{
Write-Error "Error: Missing arguments. Usage: DirPrinter.ps1 baseDirectory outputFile [-SkipBinary]"
exit 1
}
# Set the base directory and output file from the arguments
$baseDirectory = $args[0]
$outputFile = $args[1]
# Set the default value for the $skipBinary variable
$skipBinary = $false
# Check if the -SkipBinary option was provided
$skipBinaryOptionIndex = $args | Select-String -SimpleMatch "-SkipBinary" | Select-Object -ExpandProperty LineNumber
if ($skipBinaryOptionIndex)
{
$skipBinary = $true
}
# Get all the ".md" files in the base directory and its subdirectories, excluding the .git subdirectory
$mdFiles = Get-ChildItem -Path $baseDirectory -Recurse -File -Exclude ".git" | Where-Object { $_.Directory -notmatch ".git" } | Where-Object { $_.Extension -eq ".md" }
# Get all the remaining files in the base directory and its subdirectories, excluding the .git subdirectory
$otherFiles = Get-ChildItem -Path $baseDirectory -Recurse -File -Exclude ".git" | Where-Object { $_.Directory -notmatch ".git" } | Where-Object { $_.Extension -ne ".md" }
# Open the output file
$outputFileStream = New-Object System.IO.FileStream($outputFile, [System.IO.FileMode]::OpenOrCreate)
$outputFileStream.SetLength(0)
$output = New-Object System.IO.StreamWriter($outputFileStream)
# Write the header to the output file
$output.WriteLine("Files:")
$mdFiles | ForEach-Object {
$output.WriteLine(" - Filename: $( $_.Name )")
$output.WriteLine(" Filepath: $( $_.FullName )")
}
$otherFiles | ForEach-Object {
$output.WriteLine(" - Filename: $( $_.Name )")
$output.WriteLine(" Filepath: $( $_.FullName )")
}
$output.WriteLine("Total number of files: $( $mdFiles.Count + $otherFiles.Count )")
$output.WriteLine()
# Initialize the progress counter
$progressCounter = 0
# Set the total number of files being processed
$totalFiles = $mdFiles.Count + $otherFiles.Count
# Process the ".md" files first
foreach ($file in $mdFiles)
{
# Increment the progress counter
$progressCounter++
# Calculate the current progress percentage
$progressPercent = [int](($progressCounter / $totalFiles) * 100)
# Display the progress bar
Write-Progress -Activity "Processing files" -Status "Processing file $( $file.Name ) -PercentComplete $progressPercent"
# Read the contents of the file as a single string
$contents = Get-Content -Path $file.FullName -Raw
# Write the file name to the output file
$output.WriteLine()
$output.WriteLine("---")
$output.WriteLine("--- Filename: $( $file.Name)")
$output.WriteLine("--- Filepath: $( $file.FullName)")
$output.WriteLine("---")
$output.WriteLine()
# Write the contents of the file to the output file
$output.WriteLine($contents)
}
# Process the remaining files
foreach ($file in $otherFiles)
{
# Skip binary files if the -SkipBinary option was provided
if ($skipBinary -and $file.Extension -match "^(.jpg|.jpeg|.png|.gif|.bmp|.mp3|.mp4|.mpeg|.wav|.wma|.wmv|.webm|.ogg|.flac|.aac|.ac3|.eot|.otf|.ttf|.woff|.woff2)$")
{
continue
}
# Increment the progress counter
$progressCounter++
# Calculate the current progress percentage
$progressPercent = [int](($progressCounter / $totalFiles) * 100)
# Display the progress bar
Write-Progress -Activity "Processing files" -Status "Processing file $( $file.Name ) -PercentComplete $progressPercent"
# Read the contents of the file as a single string
$contents = Get-Content -Path $file.FullName -Raw
$output.WriteLine()
$output.WriteLine("---")
$output.WriteLine("--- Filename: $( $file.Name)")
$output.WriteLine("--- Filepath: $( $file.FullName)")
$output.WriteLine("---")
$output.WriteLine()
# Write the contents of the file to the output file
$output.WriteLine($contents)
}
# Close the output file
$output.Close()