Last active
January 26, 2024 00:53
-
-
Save SupaMic/e52e9b02dda8ecbeedf2ea2f635918ef to your computer and use it in GitHub Desktop.
Windows BAT file for deleting the files in a Git repo based on patterns in the .gitignore. This file is designed to be run in a parent directory with multiple Repo folders within it. Can run bat file with -y flag to Automatically confirm deletions but I'd recommend doing the Yes/No confirm for each file as the patterns can be unpredictable and n…
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
@echo off | |
setlocal enabledelayedexpansion | |
:: Check for '-y' argument | |
set "AUTO_CONFIRM=0" | |
if "%~1"=="-y" set "AUTO_CONFIRM=1" | |
:: Set the parent directory to the directory where the batch file is located | |
set "PARENT_DIR=%~dp0" | |
echo Parent Directory: %PARENT_DIR% | |
:: Loop through each subdirectory in the parent directory | |
for /d %%d in ("%PARENT_DIR%*") do ( | |
echo Entering directory: %%d | |
pushd %%d | |
if errorlevel 1 ( | |
echo Failed to enter directory: %%d | |
goto nextdir | |
) | |
set "ROOT_DIR=%%d" | |
echo Processing directory: !ROOT_DIR! | |
:: Check if .gitignore exists in the subdirectory | |
if exist ".gitignore" ( | |
echo Found .gitignore in !ROOT_DIR! | |
:: Initialize PATTERNS | |
set "PATTERNS=" | |
:: Read .gitignore and build a list of patterns to delete | |
for /f "tokens=*" %%a in ('type ".gitignore"') do ( | |
set "pattern=%%a" | |
:: Replace forward slashes with backslashes and remove leading/trailing backslashes | |
set "pattern=!pattern:/=\!" | |
if "!pattern:~0,1!"=="\" set "pattern=!pattern:~1!" | |
if "!pattern:~-1!"=="\" set "pattern=!pattern:~0,-1!" | |
:: Skip lines starting with '#' or '!' | |
if not "!pattern:~0,1!"=="#" and not "!pattern:~0,1!"=="!" ( | |
set "PATTERNS=!PATTERNS! !pattern!" | |
) | |
) | |
:: Display the patterns for verification | |
echo Patterns to delete in !ROOT_DIR!: !PATTERNS! | |
:: Delete matching items | |
for %%p in (!PATTERNS!) do ( | |
echo Checking pattern: %%p | |
for /r %%f in (%%p) do ( | |
if exist "%%f" ( | |
echo Found: %%f | |
if "!AUTO_CONFIRM!"=="1" ( | |
echo Auto-confirming deletion of: %%f | |
if exist "%%f\*" ( | |
rmdir /s /q "%%f" | |
) else ( | |
del /q "%%f" | |
) | |
) else ( | |
set /p "confirm=Delete this item? (Y/N/Q for Quit): " | |
if /i "!confirm!"=="Y" ( | |
if exist "%%f\*" ( | |
echo Deleting directory: %%f | |
rmdir /s /q "%%f" | |
) else ( | |
echo Deleting file: %%f | |
del /q "%%f" | |
) | |
) else if /i "!confirm!"=="Q" ( | |
echo Exiting script. | |
exit /b | |
) else ( | |
echo Skipped: %%f | |
) | |
) | |
) | |
) | |
) | |
) else ( | |
echo .gitignore not found in !ROOT_DIR! | |
) | |
:nextdir | |
popd | |
echo Leaving directory: %%d | |
) | |
echo Done. | |
endlocal |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Windows BAT file for deleting the files in a Git repo based on patterns in the .gitignore. This file is designed to be run in a parent directory with multiple Repo folders within it. You can run bat file with -y flag to Automatically confirm deletions but I'd recommend doing the Yes/No confirm for each file as the patterns can be unpredictable and nothing goes to the Recycle bin, all permanent deletes. If your .gitignore has many complex paths with wildcards, might do a quick comment out of those before running.