REPORTS - rename documentation (not done ❌)
csv format
01-1-1,2.000,00,05.06.2026.
01-1-2,18,00,02.03.2026.
chmod +x rename_files_DOC.sh
./rename_files_DOC.sh renames_DOC.csv "/path/to/your/local/Drive/folder"
#!/bin/bash
# usage: ./rename_files_v5.sh renames.csv /path/to/drive/folder renames_RBA.csv /path/to/006_RBA
CSV="$1"
ROOT="$2"
RBA_CSV="$3"
RBA_ROOT="$4"
DEST="./renamed"
mkdir -p "$DEST"
target_epoch() {
date -d "$1" +%s 2>/dev/null
}
cat "$CSV" | while IFS=',' read -r idx amount date; do
target_ts=$(target_epoch "$date")
best_match=""
best_diff=999999999
# try amount match in main folder first
while IFS= read -r file; do
base=$(basename "$file")
filedate=$(echo "$base" | grep -oE '^[0-9]{4}-[0-9]{2}-[0-9]{2}')
file_ts=$(target_epoch "$filedate")
if [ -z "$file_ts" ]; then
continue
fi
diff=$(( target_ts > file_ts ? target_ts - file_ts : file_ts - target_ts ))
if [ "$diff" -lt "$best_diff" ]; then
best_diff=$diff
best_match="$file"
fi
done < <(find "$ROOT" -type f -iname "*---${amount}.pdf")
if [ -n "$best_match" ]; then
base=$(basename "$best_match")
newname="${idx}-${base}"
cp -v "$best_match" "$DEST/$newname"
echo "MATCHED (amount) idx=$idx -> $base (date diff: $((best_diff / 86400)) days)"
continue
fi
# fallback: look up this idx's date in the RBA csv, then search RBA folder
rba_date=$(grep "^${idx}," "$RBA_CSV" | head -n 1 | cut -d',' -f3)
if [ -z "$rba_date" ]; then
echo "NOT FOUND: idx=$idx not matched in $ROOT and not present in $RBA_CSV"
continue
fi
yyyymmdd=$(echo "$rba_date" | tr -d '-')
fallback_match=$(find "$RBA_ROOT" -type f -iname "*_${yyyymmdd}.pdf" | head -n 1)
if [ -z "$fallback_match" ]; then
echo "NOT FOUND: idx=$idx rba_date=$rba_date - no file in $RBA_ROOT"
continue
fi
base=$(basename "$fallback_match")
newname="${idx}-${base}"
cp -v "$fallback_match" "$DEST/$newname"
echo "MATCHED (fallback RBA csv) idx=$idx -> $base"
done
NOTE: upper one NOT VERY RELIABLE
lower one great but w/o bank reports
#!/bin/bash
# usage: ./rename_files_v3.sh renames.csv /path/to/your/drive/folder
CSV="$1"
ROOT="$2"
DEST="./renamed"
mkdir -p "$DEST"
target_epoch() {
date -d "$1" +%s 2>/dev/null
}
cat "$CSV" | while IFS=',' read -r idx amount date; do
target_ts=$(target_epoch "$date")
best_match=""
best_diff=999999999
# find all files with matching amount, any date
while IFS= read -r file; do
base=$(basename "$file")
filedate=$(echo "$base" | grep -oE '^[0-9]{4}-[0-9]{2}-[0-9]{2}')
file_ts=$(target_epoch "$filedate")
if [ -z "$file_ts" ]; then
continue
fi
diff=$(( target_ts > file_ts ? target_ts - file_ts : file_ts - target_ts ))
if [ "$diff" -lt "$best_diff" ]; then
best_diff=$diff
best_match="$file"
fi
done < <(find "$ROOT" -type f -iname "*---${amount}.pdf")
if [ -z "$best_match" ]; then
echo "NOT FOUND: amount=$amount (index $idx, target date $date)"
continue
fi
base=$(basename "$best_match")
newname="${idx}-${base}"
cp -v "$best_match" "$DEST/$newname"
echo "MATCHED idx=$idx -> $base (date diff: $((best_diff / 86400)) days)"
done
No comments to display
No comments to display