Author: Tech Nomad
Twitter: https://x.com/Tech_N0mad
This PowerShell script gathers files directly from the current directory where it is executed, filters out the script file itself and the designated output file (output.txt
), and concatenates their paths and contents into that single output file.
This is a simpler alternative to Git-based consolidation, useful when you only need to package files from a single, flat directory.
- Scans the current directory only (
-Depth 0
). - Processes only files, not directories.
- Automatically excludes the script file itself and the output file (
output.txt
by default). - Generates a single output file (
output.txt
) containing:- A header (
Files:
by default). - A list of relative paths (
./filename.ext
) of all included files. - A separator (
===
). - The full content of each included file, preceded by its relative path and separated by the separator.
- A header (
- Overwrites the output file on each run.
- Uses
Get-Content -Raw
to preserve file content formatting. - Uses UTF8 encoding for the output file.
- Includes basic error handling for reading file content.
- PowerShell: Version 5.1 or later recommended. (Git is not required for this script).
- Save the script: Save the PowerShell script code to a file, for example,
consolidate_current_dir.ps1
. - Place files: Ensure the files you want to consolidate are in the same directory as the script.
- Navigate: Open PowerShell or Windows Terminal and change the directory (
cd
) to where you saved the script and the files. - Run the script: Execute the script using:
(Replace
.\consolidate_current_dir.ps1
consolidate_current_dir.ps1
with the actual name you saved the script as). - Check the output: A file named
output.txt
(by default) will be created or overwritten in the same directory, containing the consolidated file list and contents.
The generated output.txt
file has the following structure:
Files:
./some_file.txt
./another_script.ps1
./config.json
===
./some_file.txt
<Content of some_file.txt>
===
./another_script.ps1
<Content of another_script.ps1>
===
./config.json
<Content of config.json>
===
- Output File Name, Delimiter, Header: Modify the
$OutputFileName
,$Delimiter
, and$Header
variables at the beginning of the script.$OutputFileName = "packaged_files.txt" $Delimiter = "--- FILE BREAK ---" $Header = "Included Files:"
- Filtering: To add more complex filtering (e.g., exclude specific file types), modify the
Where-Object
clause that creates the$FilesToProcess
variable.
- Only processes files in the immediate directory where the script is run. It does not search subdirectories.
- Does not sort the files; the order depends on the output of
Get-ChildItem
. - Filtering is basic (only excludes self and output).
This project is licensed under the MIT License - see the LICENSE.md file for details.