Created
March 19, 2025 12:51
-
-
Save jranil/2c7f3f639af0e438226a785e3b785c66 to your computer and use it in GitHub Desktop.
PowerShell script that creates a comprehensive project snapshot by generating a single text file containing directory structure and file contents. Useful for code reviews, documentation, and sharing project snapshots without version control.
This file contains hidden or 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
# Get current date in YYYY-MM-DD format | |
$currentDate = Get-Date -Format "yyyy-MM-dd" | |
# Get the current folder name | |
$currentFolderName = Split-Path -Path (Get-Location).Path -Leaf | |
# Define output file path one folder above the current location with date prefix | |
$parentFolder = Split-Path -Path (Get-Location).Path -Parent | |
$outputFile = Join-Path -Path $parentFolder -ChildPath "$currentDate-$currentFolderName`_merged.txt" | |
# Verify output path before proceeding | |
if ([string]::IsNullOrEmpty($outputFile)) { | |
Write-Error "Output file path is null or empty. Exiting script." | |
exit | |
} | |
# Create the output file | |
Set-Content -Path $outputFile -Value "" -Force | |
# Get the full path of the current script file to exclude it | |
$scriptPath = $MyInvocation.MyCommand.Path | |
function Get-DirectoryTree { | |
param ( | |
[string]$Path = ".", | |
[int]$IndentLevel = 0, | |
[string]$OutputFilePath | |
) | |
# Pass the output file path to the function | |
$indent = " " * $IndentLevel | |
$dirInfo = Get-Item -Path $Path | |
Add-Content -Path $OutputFilePath -Value "$indent[DIR] $($dirInfo.Name)" | |
Get-ChildItem -Path $Path -File | ForEach-Object { | |
Add-Content -Path $OutputFilePath -Value "$indent [FILE] $($_.Name)" | |
} | |
Get-ChildItem -Path $Path -Directory | ForEach-Object { | |
Get-DirectoryTree -Path $_.FullName -IndentLevel ($IndentLevel + 1) -OutputFilePath $OutputFilePath | |
} | |
} | |
Add-Content -Path $outputFile -Value "================ DIRECTORY STRUCTURE ================`n" | |
# Pass the output file path explicitly to the function | |
Get-DirectoryTree -OutputFilePath $outputFile | |
Add-Content -Path $outputFile -Value "`n`n================ FILE CONTENTS ================`n" | |
# Get all files except the script itself | |
$files = Get-ChildItem -Path . -File -Recurse | Where-Object { $_.FullName -ne $scriptPath } | |
foreach ($file in $files) { | |
$filePath = $file.FullName | |
$fileName = $file.Name | |
$separator = "--------------------------------------------------------------------------------" | |
$header = "`n$separator`nFILE: $fileName`nPATH: $filePath`n$separator`n" | |
try { | |
$content = Get-Content -Path $filePath -Raw -ErrorAction Stop | |
Add-Content -Path $outputFile -Value $header | |
Add-Content -Path $outputFile -Value $content | |
Add-Content -Path $outputFile -Value "`n$separator END $separator`n`n`n" | |
} | |
catch { | |
Add-Content -Path $outputFile -Value "$header`nError reading file: $_`n$separator END $separator`n`n`n" | |
} | |
} | |
Write-Host "All files have been merged into $outputFile" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Logic
Usage
Save as ps-file-merger.ps1 in project root directory and execute. The merged output file will be generated in the parent directory._
Generated by Claude 3.7 Sonnet