Skip to content

Instantly share code, notes, and snippets.

@jranil
Created March 19, 2025 12:51
Show Gist options
  • Save jranil/2c7f3f639af0e438226a785e3b785c66 to your computer and use it in GitHub Desktop.
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.
# 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"
@jranil
Copy link
Author

jranil commented Mar 19, 2025

Logic

  • Gets current date and folder name for output file naming
  • Creates output file in parent directory with format YYYY-MM-DD-FolderName_merged.txt
  • Recursively maps directory structure with indentation showing hierarchy
  • Captures content of all files (excluding the script itself)
  • Organizes output with clear section headers and file separators
  • Includes error handling for file read operations

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment