Guest User

.tga/.tpc Bash Version

a guest
May 9th, 2023
1,192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.56 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. shopt -s nocaseglob nocasematch
  4. cd "$(dirname "$0")"/"$(echo override | tr '[:upper:]' '[:lower:]')"
  5.  
  6. readonly VERSION=1.2 SUPPORTED_FILE_TYPES=(tga tpc)
  7. echo "Version ${VERSION}\n\nPlease be sure this file is located in the game's root folder (where the game's .exe is located) and not in the Override folder.\n\nThis script deletes duplicate TGA or TPC files in the Override folder.\n"
  8.  
  9. confirm_delete() {
  10.   read -p "Delete $1? [y/n]: " input_del
  11.   case "${input_del,,}" in [y]*) rm "$1"; echo "Deleted $1";; esac
  12. }
  13.  
  14. delete_duplicates() {
  15.   local files=($(find . -iname "*.$1" -print0 | xargs -0))
  16.   if (( ${#files[@]} )); then
  17.     for file in "${files[@]}"; do
  18.       local companion_file_path="${file%.*}.tpc"
  19.       [[ "$1" == "tpc" ]] && companion_file_path="${file%.*}.tga"
  20.       if [[ -e "$companion_file_path" ]]; then
  21.         if (( dry_run )); then
  22.           echo "Would delete $file (duplicate found: $companion_file_path)"
  23.         else
  24.           echo "Deleting $file (duplicate found: $companion_file_path)"
  25.           confirm_delete "$file"
  26.           (( deleted_files++ ))
  27.         fi
  28.       fi
  29.     done
  30.     (( deleted_files )) || echo "No duplicates found for ${1^^} files."
  31.   else
  32.     echo "No ${1^^} files found."
  33.   fi
  34. }
  35.  
  36. dry_run=0
  37. while getopts "n" opt; do [[ $opt == n ]] && dry_run=1; done
  38.  
  39. for type in "${SUPPORTED_FILE_TYPES[@]}"; do
  40.   read -p "Do you want to delete duplicate ${type^^} files? [y/n]: " input_del
  41.   echo ""
  42.   [[ "$input_del" =~ [yY](es)? ]] && delete_duplicates "$type" "$dry_run"
  43. done
  44.  
  45. echo -e "\nFinished"
Add Comment
Please, Sign In to add comment