Skip to content

Commit e1e2ddb

Browse files
committed
. e abandoned file scripts
1 parent 2b20957 commit e1e2ddb

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/bin/bash
2+
3+
# Define paths
4+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
5+
LOG_FILE="$SCRIPT_DIR/.approved_files.log"
6+
BASE_DIR="$(realpath "$SCRIPT_DIR/..")"
7+
8+
# Check if the log file exists
9+
if [[ ! -f "$LOG_FILE" ]]; then
10+
echo "Error: Log file .approved_files.log not found in script directory."
11+
exit 1
12+
fi
13+
14+
# Find all *.approved.* files recursively from the base directory
15+
echo "Scanning for approval files in base directory: $BASE_DIR..."
16+
ALL_APPROVAL_FILES=$(find "$BASE_DIR" -type f -name "*.approved.*")
17+
18+
# Resolve paths in .approved_files.log
19+
echo "Resolving paths from .approved_files.log..."
20+
RESOLVED_USED_FILES=()
21+
while IFS= read -r LINE; do
22+
RESOLVED_PATH=$(realpath "$LINE")
23+
RESOLVED_USED_FILES+=("$RESOLVED_PATH")
24+
done < "$LOG_FILE"
25+
26+
# Identify abandoned files
27+
echo "Identifying abandoned approval files..."
28+
ABANDONED_FILES=()
29+
for FILE in $ALL_APPROVAL_FILES; do
30+
RESOLVED_FILE=$(realpath "$FILE")
31+
if ! [[ " ${RESOLVED_USED_FILES[@]} " =~ " $RESOLVED_FILE " ]]; then
32+
ABANDONED_FILES+=("$RESOLVED_FILE")
33+
fi
34+
done
35+
36+
# Display the abandoned files
37+
if [[ ${#ABANDONED_FILES[@]} -eq 0 ]]; then
38+
echo "No abandoned approval files found."
39+
exit 0
40+
fi
41+
42+
echo "The following approval files are abandoned:"
43+
for FILE in "${ABANDONED_FILES[@]}"; do
44+
echo " $FILE"
45+
done
46+
47+
# Ask the user if they want to delete the abandoned files
48+
read -p "Would you like to delete these files? (y/n): " RESPONSE
49+
if [[ "$RESPONSE" == "y" || "$RESPONSE" == "Y" ]]; then
50+
for FILE in "${ABANDONED_FILES[@]}"; do
51+
rm -f "$FILE" && echo "Deleted: $FILE"
52+
done
53+
echo "All abandoned files deleted."
54+
else
55+
echo "No files were deleted."
56+
fi
57+
58+
exit 0

0 commit comments

Comments
 (0)