Created
April 1, 2024 05:51
-
-
Save nodew/bf4f88a4c2422d0194075e7a190c0606 to your computer and use it in GitHub Desktop.
Merge and separate files with PowerShell scripts.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param( | |
[Parameter(Mandatory = $true)] | |
[string]$rootPath = "", | |
[Parameter(Mandatory = $true)] | |
[string]$outputFile = "", | |
[Parameter(Mandatory = $false)] | |
[string]$filter = "*" | |
) | |
Write-Host $rootPath | |
$rootPath = Resolve-Path -Path $rootPath | |
# Clear the content of the output file if it already exists | |
if (Test-Path $outputFile) { | |
Clear-Content $outputFile | |
} | |
Write-Host $rootPath | |
# Get all files under the root path, including subdirectories | |
$files = Get-ChildItem -Path $rootPath -Recurse -Filter $filter | |
foreach ($file in $files) { | |
# Construct the header with the file path relative to the root path | |
$relativePath = $file.FullName.Substring($rootPath.Length) | |
$header = "--- " + $relativePath.TrimStart('\') + " ---`n" | |
# Output the header to the output file | |
Add-Content -Path $outputFile -Value $header | |
# Read the content of the current file and append it to the output file | |
$content = Get-Content -Path $file.FullName | |
Add-Content -Path $outputFile -Value $content | |
# Add an extra newline after the content for separation, if desired | |
Add-Content -Path $outputFile -Value "`n" | |
} | |
# Notify the user that the script has finished | |
Write-Host "All files have been concatenated into $outputFile" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param( | |
[Parameter(Mandatory=$true)] | |
[string]$mergedFile, # The path to the merged file | |
[Parameter(Mandatory=$true)] | |
[string]$outputFolder # The path to the output folder where files will be separated | |
) | |
# Check if the merged file exists | |
if (-not (Test-Path -Path $mergedFile)) { | |
Write-Error "Merged file does not exist." | |
return | |
} | |
# Check if the output folder exists, if not, create it | |
if (-not (Test-Path -Path $outputFolder)) { | |
Write-Host "Output folder does not exist. Creating it..." | |
New-Item -ItemType Directory -Path $outputFolder | |
} | |
# Read the merged file content | |
$mergedContent = Get-Content -Path $mergedFile | |
# Initialize variables to keep track of the current file content and path | |
$currentFilePath = $null | |
$currentFileContent = @() | |
foreach ($line in $mergedContent) { | |
if ($line -match "^---\s+(.+)\s+---$") { | |
# If there's an existing file path, it means we've hit a new file header | |
# So, we need to save the current file content before starting a new one | |
if ($currentFilePath) { | |
$fullPath = Join-Path -Path $outputFolder -ChildPath $currentFilePath | |
$dirPath = Split-Path -Path $fullPath | |
if (-not (Test-Path -Path $dirPath)) { | |
New-Item -ItemType Directory -Path $dirPath | |
} | |
Set-Content -Path $fullPath -Value $currentFileContent | |
} | |
# Start a new file with the new path | |
$currentFilePath = $matches[1] | |
$currentFileContent = @() | |
} else { | |
# Add the current line to the file content, excluding the separator line | |
if ($line -ne "") { | |
$currentFileContent += $line | |
} | |
} | |
} | |
# Save the last file after the loop ends | |
if ($currentFilePath) { | |
$fullPath = Join-Path -Path $outputFolder -ChildPath $currentFilePath | |
$dirPath = Split-Path -Path $fullPath | |
if (-not (Test-Path -Path $dirPath)) { | |
New-Item -ItemType Directory -Path $dirPath | |
} | |
Set-Content -Path $fullPath -Value $currentFileContent | |
} | |
Write-Host "Files have been separated into the output folder: $outputFolder" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment