Created
July 20, 2025 02:54
-
-
Save hereisderek/930c76f5bac56d74f7ccbf632e9df811 to your computer and use it in GitHub Desktop.
script to automatically rename my bills to a more human readable formate, currently support statements pdf from watercare for water, genesis for electricity/gas(NZ).
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
| #!/bin/bash | |
| # Determine which date command to use | |
| if [[ "$(uname)" == "Darwin" ]]; then | |
| if command -v gdate >/dev/null 2>&1; then | |
| DATE_CMD="gdate" | |
| else | |
| echo "Error: macOS detected but 'gdate' (GNU date) not found." | |
| echo "Please install it using: brew install coreutils" | |
| exit 1 | |
| fi | |
| else | |
| DATE_CMD="date" | |
| fi | |
| # Set the base directory (adjust if needed) | |
| BASE_DIR="./" | |
| # Supported bill types (folder names must match these exactly) | |
| BILL_TYPES=("electricity" "gas" "rates" "water") | |
| # Loop through each bill type folder | |
| for bill_type in "${BILL_TYPES[@]}"; do | |
| folder="$BASE_DIR/$bill_type" | |
| if [ ! -d "$folder" ]; then | |
| echo "Skipping missing folder: $folder" | |
| continue | |
| fi | |
| echo "Processing folder: $folder" | |
| for file in "$folder"/*.pdf; do | |
| [ -e "$file" ] || continue # Skip if no PDFs | |
| filename=$(basename "$file") | |
| new_filename="" | |
| # Match patterns based on bill type | |
| case "$bill_type" in | |
| electricity) | |
| if [[ $filename =~ ^[^-]+-([0-9]{10})-([0-9]{4})-([A-Za-z]{3})-([0-9]{2})\.pdf$ ]]; then | |
| # Matches Genesis-1003700174-2023-Dec-20.pdf | |
| account="${BASH_REMATCH[1]}" | |
| year="${BASH_REMATCH[2]}" | |
| month="${BASH_REMATCH[3]}" | |
| day="${BASH_REMATCH[4]}" | |
| date_string="$day $month $year" | |
| date=$($DATE_CMD -d "$date_string" +%Y-%m-%d 2>/dev/null) | |
| # echo "matched, date_string: $date_string, date:$date" | |
| if [[ -n "$date" ]]; then | |
| new_filename="${bill_type}_${date}_${account}.pdf" | |
| fi | |
| elif [[ $filename =~ ^([0-9]{10})_.*_([0-9]{2})([0-9]{2})([0-9]{4})-.*\.pdf$ ]]; then | |
| # Matches 1003700174_346867246_30042025-000129.pdf | |
| account="${BASH_REMATCH[1]}" | |
| day="${BASH_REMATCH[2]}" | |
| month="${BASH_REMATCH[3]}" | |
| year="${BASH_REMATCH[4]}" | |
| date="$year-$month-$day" | |
| new_filename="${bill_type}_${date}_${account}.pdf" | |
| fi | |
| ;; | |
| water) | |
| # Match Watercare_Bill_5488116-02_2023_Sep_12.pdf | |
| if [[ $filename =~ ^Watercare_Bill_([0-9]+-[0-9]+)_([0-9]{4})_([A-Za-z]{3})_([0-9]{2})\.pdf$ ]]; then | |
| account="${BASH_REMATCH[1]}" | |
| year="${BASH_REMATCH[2]}" | |
| month="${BASH_REMATCH[3]}" | |
| day="${BASH_REMATCH[4]}" | |
| date_string="$day $month $year" | |
| date=$($DATE_CMD -d "$date_string" +%Y-%m-%d 2>/dev/null) | |
| # echo "matched, date_string: $date_string, date:$date" | |
| if [[ -n "$date" ]]; then | |
| new_filename="${bill_type}_${date}_${account}.pdf" | |
| fi | |
| fi | |
| ;; | |
| *) | |
| # Extend with gas/rates naming rules here | |
| ;; | |
| esac | |
| if [[ -n "$new_filename" ]]; then | |
| new_path="${folder}/${new_filename}" | |
| echo "Renaming: $filename → $new_filename" | |
| mv "$file" "$new_path" | |
| else | |
| echo "Skipped (unmatched): $filename" | |
| fi | |
| done | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment