dimanche 5 mars 2017

How to add a check file extension in this script

Given this script

#!/bin/bash

fromdir="/Users/Desktop/Downloads/"
todir="/Users/Desktop/Downloads/"

rename_files() {
    title="${1##*${2} - }"
    exttitle="${2}"
    iters=0
    numfiles=$(ls -l "$1/"*.* | wc -l)
    for filename in "$1/"*.*; do
      new_path="${todir}${exttitle} - ${title}/${title}.${filename##*.rar.}"
      iters=_$(( ++i ))
      # extract $path without dot + extension
      new_path_basename=${new_path%*.*};
      # Extract extension from $path
      ext=${new_path##*.};
      if [ "$numfiles" -eq 1 ]; then
          iters=""
      fi
      case "${filename##*.rar.}" in mp3|mp4)
        echo "moving $filename -> ${new_path_basename}${iters}.rar"
        # Add ${iters} before extension
        mv "${filename}" "${new_path_basename}${iters}.rar"
        unrar e "${new_path_basename}${iters}.rar" "${todir}${exttitle} - ${title}"
      ;;
      esac
    done
}

rename_category() {
  for path in "${fromdir}${1}"*; do
    rename_files "$path" "$1"
  done
}

rename_category CAT1
rename_category CAT2

that do this:

Before

/Users/Desktop/Downloads/CAT1 - TEST/file.part1.rar.mp4
                                    /file.part2.rar.mp4

After

/Users/Desktop/Downloads/CAT1 - TEST/TEST_1.rar
                                    /TEST_2.rar

and

/Users/Desktop/Downloads/CAT1 - TEST/filextracted.avi

How can I add this check if file extension exists

count=`ls -1 *.avi 2>/dev/null | wc -l`
if [ $count != 0 ]
then 
echo TRUE
else
echo FALSE
unrar e "${new_path_basename}${iters}.rar" "${todir}${exttitle} - ${title}"
fi

so that it checks if file is extracted or not and if not it tries again until .avi file exists? I tried to add this right after the first done but it gives me errors

Cannot open /Users/Desktop/Downloads/CAT1 - TEST/TEST./Users/Desktop/Downloads/CAT1 - TEST/TEST_2_2.rar

Aucun commentaire:

Enregistrer un commentaire