Last active
March 19, 2024 22:20
-
-
Save CodeByAidan/568d9737f674a2c3e4ab77eac77aaeb7 to your computer and use it in GitHub Desktop.
(.bat, .py, .sh, .ps1) - This script generates a file that contains the contents of all files in a directory and its subdirectories. Great tool for using with ChatGPT - so you can export all your files quickly and paste it over! Powershell version is the most customized for now.
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
:: GenerateFileContents.bat | |
:: ~~~~~~~~~~~~~~~~~~~~~~~~ | |
:: Description: | |
:: This script generates a file that contains the contents of all files in a directory and its subdirectories. | |
:: | |
:: Parameters: | |
:: 1. Directory path to search for files in (i.e. "D:\Other\Java Projects\luhnalgorithm\src\main\java\com") | |
:: 2. Output file path (i.e. "D:\Other\Java Projects\luhnalgorithm\filecontents.txt") | |
:: 3. File extensions to look for (i.e. "java" or "java txt" or "java txt csv") | |
:: | |
:: Usage: | |
:: .\GenerateFileContents.bat "D:\Other\Java Projects\luhnalgorithm\src\main\java\com" "D:\Other\Java Projects\luhnalgorithm\filecontents.txt" "java txt csv" | |
:: | |
:: Author: CodeByAidan (https://github.com/CodeByAidan) | |
:: Date: 02-26-2024 | |
:: ~~~~~~~~~~~~~~~~~~~~~~~~ | |
@echo off | |
setlocal enabledelayedexpansion | |
setlocal enableextensions | |
rem Directory path is the first argument | |
set "directory=%~1" | |
rem Output file path is the second argument | |
set "outputFile=%~2" | |
rem File extensions to look for are the third argument | |
set "extensions=%~3" | |
rem Delete the output file if it already exists | |
if exist "%outputFile%" del "%outputFile%" | |
rem Perform the main loop | |
call :mainLoop | |
rem Remove the {REMOVE-ME} lines | |
powershell -Command "(Get-Content '%outputFile%') -replace '{REMOVE-ME}', '' | Set-Content '%outputFile%'" | |
rem Inform user about the completion | |
echo File contents have been written to "%outputFile%" | |
rem Exit the script | |
exit /b | |
rem This function iterates through the files in the directory and subdirectories | |
:mainLoop | |
for %%E in (%extensions%) do ( | |
for /r "%directory%" %%F in (*.%%E) do ( | |
rem Call the function that writes to the output file | |
call :writeToFile "%%F" | |
) | |
) | |
exit /b | |
rem This function writes the file's content to the output file | |
:writeToFile | |
set "fullPath=%~1" | |
set "relativePath=!fullPath:%directory%\=!" | |
rem Output file title | |
>>"%outputFile%" echo !relativePath! | |
>>"%outputFile%" echo ====== | |
rem Output file contents | |
type "%fullPath%" >> "%outputFile%" | |
rem Add newlines for separation | |
>>"%outputFile%" (echo {REMOVE-ME}) | |
>>"%outputFile%" (echo {REMOVE-ME}) | |
exit /b |
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
<# | |
.SYNOPSIS | |
This script recursively searches a directory and its subdirectories for | |
files with specified extensions and compiles their contents into a single | |
output file, excluding specified files. | |
.DESCRIPTION | |
This script generates a file that contains the contents of all files in | |
a directory and its subdirectories, excluding specified files. It searches | |
the specified directory and its subdirectories for files with the specified | |
extensions and compiles their contents into a single output file. Optionally, | |
specific files can be excluded from the output. Additionally, it can include | |
a directory tree at the end of the file. | |
.PARAMETER Directory | |
Directory path to search for files in. | |
.PARAMETER OutputFile | |
Output file path. | |
.PARAMETER Extensions | |
File extensions to look for. | |
.PARAMETER ExcludeFiles | |
Files or patterns to exclude from the output. Wildcards (*) are supported. | |
The current directory can be specified using ".\". | |
.PARAMETER IncludeTree | |
Switch to include a directory tree at the end of the output file. | |
.EXAMPLE | |
.\GenerateFileContents.ps1 -Directory "$pwd\src" -OutputFile "$pwd\filecontents.txt" -Extensions "java", "txt" -ExcludeFiles ".\filecontents.txt", "*.tmp", "LOL.txt", ".\Hello.txt" -IncludeTree True | |
.\GenerateFileContents.ps1 -Directory "$pwd" -OutputFile "$pwd\filecontents.txt" -Extensions "java", "txt" -ExcludeFiles ".\filecontents.txt", "*.tmp", "LOL.txt", ".\Hello.txt" | |
.\GenerateFileContents.ps1 -Directory "$pwd" -OutputFile "$pwd\filecontents.txt" -Extensions "java", "txt" -ExcludeFiles ".\filecontents.txt", "*.tmp", "LOL.txt" | |
.\GenerateFileContents.ps1 -Directory "$pwd" -OutputFile "$pwd\filecontents.txt" -Extensions "java", "txt" -ExcludeFiles ".\filecontents.txt" | |
.\GenerateFileContents.ps1 -Directory "$pwd" -OutputFile "$pwd\filecontents.txt" -Extensions "java", "txt" | |
.NOTES | |
Author: CodeByAidan (https://github.com/CodeByAidan) | |
Date: 02-26-2024 | |
#> | |
param ( | |
[string]$Directory, | |
[string]$OutputFile, | |
[string[]]$Extensions, | |
[string[]]$ExcludeFiles, | |
[switch]$IncludeTree | |
) | |
# Function to resolve files specified in the .\filename.txt format | |
function ResolveRelativePath($File) { | |
$currentDirectory = (Get-Item -Path ".\").FullName | |
return Join-Path -Path $currentDirectory -ChildPath $File | |
} | |
# Resolve files specified in the .\filename.txt format | |
$ResolvedExcludeFiles = @() | |
foreach ($ExcludeFile in $ExcludeFiles) { | |
if ($ExcludeFile -like ".\*") { | |
$ResolvedExcludeFiles += ResolveRelativePath -File $ExcludeFile.Substring(2) | |
} elseif ($ExcludeFile -like "*.*") { | |
$ResolvedExcludeFiles += ResolveRelativePath -File $ExcludeFile | |
} else { | |
$ResolvedExcludeFiles += $ExcludeFile | |
} | |
} | |
# Remove the output file if it already exists | |
if (Test-Path $OutputFile) { | |
Remove-Item $OutputFile | |
} | |
# Process files based on the specified extensions and excluding the specified files | |
foreach ($Extension in $Extensions) { | |
Get-ChildItem -Path $Directory -Recurse -Filter "*.$Extension" | ForEach-Object { | |
if ($_.FullName -ne $OutputFile -and $_.FullName -notin $ResolvedExcludeFiles) { | |
Add-Content -Path $OutputFile -Value $_.FullName | |
Add-Content -Path $OutputFile -Value "=======" | |
Get-Content $_.FullName | Add-Content -Path $OutputFile | |
Add-Content -Path $OutputFile -Value "{REMOVE-ME}" | |
Add-Content -Path $OutputFile -Value "{REMOVE-ME}" | |
} | |
} | |
} | |
# Remove the {REMOVE-ME} lines from the output file | |
(Get-Content $OutputFile) -replace "{REMOVE-ME}", "" | Set-Content $OutputFile | |
# Include tree if the switch is provided | |
if ($IncludeTree) { | |
Add-Content -Path $OutputFile -Value "`nTree Directory`n============" | |
$treeOutput = tree . /F | Select-Object -Skip 2 | ForEach-Object { $_.TrimEnd() } | |
$treeOutput | Out-File -FilePath $OutputFile -Append -Encoding utf8 | |
} | |
Write-Host "File contents have been written to $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
r""" | |
GenerateFileContents.bat | |
~~~~~~~~~~~~~~~~~~~~~~~~ | |
Description: | |
This script generates a file that contains the contents | |
of all files in a directory and its subdirectories. | |
Parameters: | |
1. Directory path to search for files in (i.e. | |
"D:\Other\Java Projects\luhnalgorithm\src\main\java\com") | |
2. Output file path (i.e. | |
"D:\Other\Java Projects\luhnalgorithm\filecontents.txt") | |
3. File extensions to look for (i.e. "java" or "java txt" | |
or "java txt csv") | |
Usage: | |
python GenerateFileContents.py <directory> <output_file> <extensions> | |
ex. python GenerateFileContents.py "D:\Other\Java Projects\luhnalgorithm\src\main\java\com" "D:\Other\Java Projects\luhnalgorithm\filecontents.txt" "java txt csv" | |
Author: CodeByAidan (https://github.com/CodeByAidan) | |
Date: 02-26-2024 | |
~~~~~~~~~~~~~~~~~~~~~~~~ | |
""" | |
from typing import List, TextIO | |
import os | |
def main(directory: str, output_file: str, extensions: List[str]) -> None: | |
""" | |
Deletes the output file if it already exists, performs the | |
main loop to write file contents to the output file, and | |
informs the user about the completion. | |
Args: | |
directory (str): The directory to search for files. | |
output_file (str): The path of the output file. | |
extensions (List[str]): The list of file extensions to search for. | |
Returns: | |
None | |
""" | |
# Delete the output file if it already exists | |
if os.path.exists(output_file): | |
os.remove(output_file) | |
# Perform the main loop | |
with open(output_file, "a", encoding="utf-8") as f: | |
for root, _, files in os.walk(directory): | |
for file in files: | |
if file.endswith(tuple(extensions)): | |
write_to_file(os.path.join(root, file), f) | |
# Inform user about the completion | |
print(f"File contents have been written to '{output_file}'") | |
def write_to_file(file_path: str, output_file: TextIO) -> None: | |
""" | |
Writes the contents of a file to the output file. | |
Args: | |
file_path (str): The path of the file to be written. | |
output_file: The output file object to write to. | |
Returns: | |
None | |
""" | |
# Output file title | |
output_file.write(file_path + "\n") | |
output_file.write("=====\n") | |
# Output file contents | |
with open(file_path, "r", encoding="utf-8") as f: | |
output_file.write(f.read()) | |
# Add newlines for separation | |
output_file.write("\n\n") | |
if __name__ == "__main__": | |
import sys | |
if len(sys.argv) != 4: | |
print( | |
"Usage: python generate_file_contents.py <directory> <output_file> <extensions>" | |
) | |
sys.exit(1) | |
main(sys.argv[1], sys.argv[2], sys.argv[3].split()) |
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
#!/bin/bash | |
# Description: | |
# This script generates a file that contains the contents of all files in a directory and its subdirectories. | |
# | |
# Parameters: | |
# 1. Directory path to search for files in (i.e. "/mnt/d/Other/Java Projects/luhnalgorithm/src/main/java/com") | |
# 2. Output file path (i.e. "/mnt/d/Other/Java Projects/luhnalgorithm/filecontents.txt") | |
# 3. File extensions to look for (i.e. "java txt csv") | |
# | |
# Usage: | |
# ./GenerateFileContents.sh "/mnt/d/Other/Java Projects/luhnalgorithm/src/main/java/com" "/mnt/d/Other/Java Projects/luhnalgorithm/filecontents.txt" "java txt csv" | |
# | |
# Author: CodeByAidan (https://github.com/CodeByAidan) | |
# Date: 02-26-2024 | |
# Directory path is the first argument | |
directory="$1" | |
# Output file path is the second argument | |
outputFile="$2" | |
# File extensions to look for are the third argument | |
extensions="$3" | |
# Delete the output file if it already exists | |
[ -f "$outputFile" ] && rm "$outputFile" | |
# Perform the main loop | |
for ext in $extensions; do | |
find "$directory" -type f -name "*.$ext" | while read -r file; do | |
# Write the file's content to the output file | |
relativePath="${file#$directory/}" | |
echo "$relativePath" >> "$outputFile" | |
echo "=======" >> "$outputFile" | |
cat "$file" >> "$outputFile" | |
echo "" >> "$outputFile" | |
echo "" >> "$outputFile" | |
done | |
done | |
# Inform the user about the completion | |
echo "File contents have been written to $outputFile" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
DEMO:
https://gist.github.com/assets/67598470/59296e33-14b2-407b-a7e1-9c3f8f04dd74