REPORTS - rename all files
#!/bin/bash
# usage: ./rename_files_combined.sh renames_DOC.csv /path/to/main/folder renames_RBA.csv /path/to/006_RBA
#
# For each row in renames_DOC.csv (idx,amount,date):
# 1. Search the main folder for a file ending in ---{amount}.pdf, picking whichever
# candidate's filename-date is closest to the CSV date - but only accept it if
# that difference is within MAX_DIFF_DAYS. This avoids wrongly grabbing an
# unrelated invoice that happens to share the same amount weeks/months apart.
# 2. If no acceptable amount match is found, look up this idx in renames_RBA.csv
# (format: ref,filename) to get the exact original filename, then find that
# exact file anywhere under the RBA folder.
CSV="$1"
ROOT="$2"
RBA_CSV="$3"
RBA_ROOT="$4"
DEST="./renamed"
MAX_DIFF_DAYS=10
MAX_DIFF_SECONDS=$(( MAX_DIFF_DAYS * 86400 ))
mkdir -p "$DEST"
target_epoch() {
date -d "$1" +%s 2>/dev/null
}
# strip potential \r line endings up front
sed -i 's/\r$//' "$CSV" "$RBA_CSV" 2>/dev/null
cat "$CSV" | while IFS=',' read -r idx amount date; do
target_ts=$(target_epoch "$date")
best_match=""
best_diff=999999999
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" ] && [ "$best_diff" -le "$MAX_DIFF_SECONDS" ]; 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
if [ -n "$best_match" ]; then
echo "SKIPPED (amount match too far: $((best_diff / 86400)) days) idx=$idx -> trying RBA fallback"
fi
# fallback: look up this idx's exact filename in the RBA csv (format: ref,filename)
rba_filename=$(grep "^${idx}," "$RBA_CSV" | head -n 1 | cut -d',' -f2-)
if [ -z "$rba_filename" ]; then
echo "NOT FOUND: idx=$idx - no amount match within ${MAX_DIFF_DAYS} days, and not present in $RBA_CSV"
continue
fi
fallback_match=$(find "$RBA_ROOT" -type f -iname "$rba_filename" | head -n 1)
if [ -z "$fallback_match" ]; then
echo "NOT FOUND: idx=$idx expected_file=$rba_filename - not found under $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
lol tis beau
#!/bin/bash # usage: ./rename_files_combined.sh renames_DOC.csv /path/to/main/folder renames_RBA.csv /path/to/006_RBA # # For each row in renames_DOC.csv (idx,amount,date): # 1. Search the main folder for a file ending in ---{amount}.pdf, picking whichever # candidate's filename-date is closest to the CSV date - but only accept it if # that difference is within MAX_DIFF_DAYS. This avoids wrongly grabbing an # unrelated invoice that happens to share the same amount weeks/months apart. # 2. If no acceptable amount match is found, look up this idx in renames_RBA.csv # (format: ref,filename) to get the exact original filename, then find that # exact file anywhere under the RBA folder. CSV="$1" ROOT="$2" RBA_CSV="$3" RBA_ROOT="$4" DEST="./renamed" MAX_DIFF_DAYS=10 MAX_DIFF_SECONDS=$(( MAX_DIFF_DAYS * 86400 )) mkdir -p "$DEST" target_epoch() { date -d "$1" +%s 2>/dev/null } # strip potential \r line endings up front sed -i 's/\r$//' "$CSV" "$RBA_CSV" 2>/dev/null cat "$CSV" | while IFS=',' read -r idx amount date; do target_ts=$(target_epoch "$date") best_match="" best_diff=999999999 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" ] && [ "$best_diff" -le "$MAX_DIFF_SECONDS" ]; 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 if [ -n "$best_match" ]; then echo "SKIPPED (amount match too far: $((best_diff / 86400)) days) idx=$idx -> trying RBA fallback" fi # fallback: look up this idx's exact filename in the RBA csv (format: ref,filename) rba_filename=$(grep "^${idx}," "$RBA_CSV" | head -n 1 | cut -d',' -f2-) if [ -z "$rba_filename" ]; then echo "NOT FOUND: idx=$idx - no amount match within ${MAX_DIFF_DAYS} days, and not present in $RBA_CSV" continue fi fallback_match=$(find "$RBA_ROOT" -type f -iname "$rba_filename" | head -n 1) if [ -z "$fallback_match" ]; then echo "NOT FOUND: idx=$idx expected_file=$rba_filename - not found under $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
No comments to display
No comments to display