Forums before death by AOL, social media and spammers... "We can't have nice things"
|    alt.os.linux.mint    |    Looks pretty on the outside, thats it!    |    30,566 messages    |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
|    Message 29,222 of 30,566    |
|    Dr. Noah Bodie to All    |
|    Re: GROK Made a Simple File Renamer    |
|    25 Sep 25 04:16:18    |
   
   XPost: alt.os.linux.ubuntu   
   From: noah@bodie.not   
      
   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   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
(c) 1994, bbs@darkrealms.ca