|
| 1 | +@echo off |
| 2 | +setlocal enabledelayedexpansion |
| 3 | + |
| 4 | +REM Define paths |
| 5 | +set "SCRIPT_DIR=%~dp0" |
| 6 | +set "LOG_FILE=%SCRIPT_DIR%.approved_files.log" |
| 7 | +set "BASE_DIR=%SCRIPT_DIR%.." |
| 8 | + |
| 9 | +REM Check if the log file exists |
| 10 | +if not exist "%LOG_FILE%" ( |
| 11 | + echo Error: Log file .approved_files.log not found in script directory. |
| 12 | + exit /b 1 |
| 13 | +) |
| 14 | + |
| 15 | +REM Find all *.approved.* files recursively from the base directory |
| 16 | +echo Scanning for approval files in base directory: %BASE_DIR%... |
| 17 | +for /r "%BASE_DIR%" %%f in (*.approved.*) do ( |
| 18 | + call :ResolvePath "%%f" resolved_path |
| 19 | + set "ALL_APPROVAL_FILES=!ALL_APPROVAL_FILES!%%f;" |
| 20 | +) |
| 21 | + |
| 22 | +REM Resolve paths in .approved_files.log |
| 23 | +echo Resolving paths from .approved_files.log... |
| 24 | +set "RESOLVED_USED_FILES=" |
| 25 | +for /f "usebackq delims=" %%l in ("%LOG_FILE%") do ( |
| 26 | + call :ResolvePath "%%l" resolved_path |
| 27 | + set "RESOLVED_USED_FILES=!RESOLVED_USED_FILES!!resolved_path!;" |
| 28 | +) |
| 29 | + |
| 30 | +REM Compare files and identify abandoned ones |
| 31 | +echo Identifying abandoned approval files... |
| 32 | +set "ABANDONED_FILES=" |
| 33 | +for %%f in (!ALL_APPROVAL_FILES:;= !) do ( |
| 34 | + call :ResolvePath "%%f" resolved_path |
| 35 | + if "!RESOLVED_USED_FILES:;=!" neq "!RESOLVED_USED_FILES:;=%%resolved_path%%;!" ( |
| 36 | + set "ABANDONED_FILES=!ABANDONED_FILES!!resolved_path!;" |
| 37 | + ) |
| 38 | +) |
| 39 | + |
| 40 | +REM Display abandoned files |
| 41 | +if "!ABANDONED_FILES!"=="" ( |
| 42 | + echo No abandoned approval files found. |
| 43 | + exit /b 0 |
| 44 | +) |
| 45 | + |
| 46 | +echo The following approval files are abandoned: |
| 47 | +for %%f in (!ABANDONED_FILES:;= !) do ( |
| 48 | + echo %%f |
| 49 | +) |
| 50 | + |
| 51 | +REM Prompt for deletion |
| 52 | +set /p "RESPONSE=Would you like to delete these files? (y/n): " |
| 53 | +if /i "!RESPONSE!"=="y" ( |
| 54 | + for %%f in (!ABANDONED_FILES:;= !) do ( |
| 55 | + del /q "%%f" && echo Deleted: %%f |
| 56 | + ) |
| 57 | + echo All abandoned files deleted. |
| 58 | +) else ( |
| 59 | + echo No files were deleted. |
| 60 | +) |
| 61 | + |
| 62 | +exit /b 0 |
| 63 | + |
| 64 | +REM Function to resolve paths |
| 65 | +:ResolvePath |
| 66 | +for %%i in (%1) do set "%2=%%~fi" |
| 67 | +exit /b |
0 commit comments