• Re: GROK Made a Simple File Renamer

    From Dr. Noah Bodie@noah@bodie.not to alt.os.linux.ubuntu,alt.os.linux.mint on Thu Sep 25 04:16:18 2025
    From Newsgroup: alt.os.linux.ubuntu

    An improved version. This alerts you if the file to be renamed matches
    an already existing file since you can't have two files with identical
    names.


    #!/bin/bash
    # Check if zenity is installed
    if ! command -v zenity &> /dev/null; then
    echo "Zenity is required but not installed. Please install it using
    'sudo apt-get install zenity' or equivalent."
    exit 1
    fi

    # Get search string from user
    search_string=$(zenity --entry \
    --title="Search String" \
    --text="Enter characters to search for:" \
    --width=300)

    # Check if user cancelled
    if [ $? -ne 0 ]; then
    exit 0
    fi

    # Check if search string is empty
    if [ -z "$search_string" ]; then
    zenity --error --text="Search string cannot be empty!" --width=200
    exit 1
    fi

    # Get replace string from user
    replace_string=$(zenity --entry \
    --title="Replace String" \
    --text="Enter characters to replace with:" \
    --width=300)

    # Check if user cancelled
    if [ $? -ne 0 ]; then
    exit 0
    fi

    # Count files that will be renamed and check for conflicts
    count=0
    conflicts=()
    for file in *; do
    if [[ "$file" == *"$search_string"* ]]; then new_name="${file//$search_string/$replace_string}"
    if [ "$file" != "$new_name" ]; then
    if [ -e "$new_name" ]; then
    conflicts+=("$new_name")
    else
    ((count++))
    fi
    fi
    fi
    done

    # If there are conflicts, show error and exit
    if [ ${#conflicts[@]} -gt 0 ]; then
    conflict_list=$(printf "%s\n" "${conflicts[@]}")
    zenity --error --text="Cannot rename files. The following files already exist:\n$conflict_list" --width=400
    exit 1
    fi

    # If no files match, inform user and exit
    if [ $count -eq 0 ]; then
    zenity --info --text="No files found containing '$search_string'."
    --width=200
    exit 0
    fi

    # Perform rename operation
    for file in *; do
    if [[ "$file" == *"$search_string"* ]]; then new_name="${file//$search_string/$replace_string}"
    if [ "$file" != "$new_name" ]; then
    mv -n "$file" "$new_name"
    if [ $? -eq 0 ]; then
    echo "Renamed: $file -> $new_name"
    else
    zenity --error --text="Error renaming $file to $new_name" --width=200
    fi
    fi
    fi
    done

    # Show completion message
    zenity --info --text="Renaming complete. $count files processed."
    --width=200
    --- Synchronet 3.21a-Linux NewsLink 1.2