fixnames.sh

Browser view only. Use the download link if you want to save the file.

Back to Toolbox
#!/bin/bash
source ./launch_random_terminal.sh

# Rename img*.pdf files to Paperwork.pdf or Paperwork1.pdf, Paperwork2.pdf if more than one is found
counter=1
for file in img*.pdf; do
    if [ -e "$file" ]; then
        if [ $counter -eq 1 ]; then
            mv "$file" "Paperwork.pdf"
        else
            mv "$file" "Paperwork${counter}.pdf"
        fi
        counter=$((counter + 1))
    fi
done

# Check for video files (MKV, MP4, or MOV) in both upper and lowercase
ext=""
if ls *.[mM][kK][vV] 1> /dev/null 2>&1; then
    ext="mkv"
elif ls *.[mM][pP]4 1> /dev/null 2>&1; then
    ext="mp4"
elif ls *.[mM][oO][vV] 1> /dev/null 2>&1; then
    ext="mov"
elif ls *.[mM][tT][sS] 1> /dev/null 2>&1; then
    ext="mts"
fi

# If a video set is found, prompt for a namestring and confirm rename for each file
if [ -n "$ext" ]; then
    # Try to get the namestring from the clipboard using xclip
    # (works but annoying, let's just use folder name.  I can copy and paste the answer in for multi-witness myself
    # clipboard_namestring=$(xclip -o -selection clipboard 2>/dev/null)

    # Check if clipboard is valid: under 20 chars and two or three words
    if [ -n "$clipboard_namestring" ] && [ ${#clipboard_namestring} -le 20 ]; then
        # Count the number of words in the clipboard content
        word_count=$(echo "$clipboard_namestring" | wc -w)
        if [ "$word_count" -ge 2 ] && [ "$word_count" -le 3 ]; then
            default_namestring="$clipboard_namestring"
        fi
    fi

    # If clipboard is not valid, extract the name from the containing folder
    if [ -z "$default_namestring" ]; then
        folder_name=$(basename "$PWD")
        # Extract the name part from the folder name assuming format "YYYY-MM-DD Name (Optional)"
        namestring_from_folder=$(echo "$folder_name" | sed -E 's/^[0-9]{4}-[0-9]{2}-[0-9]{2}\s+([^()]+).*/\1/' | xargs)
        default_namestring="$namestring_from_folder"
    fi
    
    # Prompt for namestring with clipboard content pre-filled (if valid)
    read -p "Enter a namestring [default: ${default_namestring}]: " namestring
    # If the user presses enter without typing anything, use the clipboard content
    namestring=${namestring:-$default_namestring}

    # Count the number of video files
    num_videos=$(ls *."${ext,,}" *."${ext^^}" 2>/dev/null | wc -l)
    
    vid_counter=1
    # Loop through both lowercase and uppercase extensions
    for vid_file in *."${ext,,}" *."${ext^^}"; do
        # Skip if no matching files are found
        if [ ! -e "$vid_file" ]; then
            continue
        fi

        # Prompt for confirmation without requiring ENTER
        while true; do
            # Use numbering only if there are multiple files
            if [ "$num_videos" -gt 1 ]; then
                new_filename="${namestring} ${vid_counter}.${ext,,}"
            else
                new_filename="${namestring}.${ext,,}"
            fi

            read -n 1 -p "Rename '$vid_file' to '$new_filename'? (y/n): " confirm
            echo    # move to the next line
            if [[ "$confirm" =~ ^[Yy]$ ]]; then
                mv "$vid_file" "$new_filename"  # Convert extension to lowercase
                break
            elif [[ "$confirm" =~ ^[Nn]$ ]]; then
                break
            else
                echo "Invalid input. Please press 'y' or 'n'."
            fi
        done
        vid_counter=$((vid_counter + 1))
    done
fi

# Kill the terminal window
kill -- "$PPID"