2021-01-31

Bash script to copy movie folder name to its contents - movie and srt files

I have a main folder that contains multiple movie folders with different names and each folder has a movie and sometimes a subtitle file (.srt format), and some folders have 2-3 subtitle files, tree structure below:

movie-folder$ tree
.
├── 100.Yen.Love.(2014).BluRay.1080p
│   ├── 100.Yen.Love.2014.1080p.BluRay.x264-.mp4
│   └── 100.Yen.Love.2014.720p.BluRay.x264.-English.srt
├── Afternoon.Affair.Rear.Window.(1972).1080p.BluRay
│   ├── Afternoon.Affair.Rear.Window.1972.1080p.BluRay.x264.AAC-.mp4
│   └── Afternoon.Affair.Rear.Window.1972.1080p.BluRay.x264.AAC-.srt
├── A.Page.Of.Madness.(1926).BluRay.1080p
│   └── A.Page.Of.Madness.1926.1080p.BluRay.x264-.mp4
├── Asaki.Yumemishi.(1974).BluRay.1080p
│   ├── Asaki.Yumemishi.1974.1080p.BluRay.x264-.mp4
│   ├── Asaki.Yumemishi.1974.1080p.BluRay.x264-.srt
│   └── English.srt
  1. I need the movie and srt files to share the same name as the folder they're in while keeping their extensions.
  2. If there are more than 2 srt files I would like to keep both, but the 2nd and if more would need to be labeled the movie name, only adding a number at the end.
  3. The script will only run for the folder being used in and its sub-directories.
  4. When movie folders without an srt file in them and only the movie, do nothing and continue to next folder.

I have found several different Q&A trying to complete the same, but through running and modifying I am not achieving with their scripts. This is why I do not want to include any bad script with the question to allow more of an innovative way to complete this request.

As requested, including script I have tried/modified without success:

for f in *\ *.{srt,ssa,sub} ; do
  if [ -e "$f" ]; then
    if [[ "$f" =~ ([0-9]+)([0-9][0-9]) || "$f" =~ s([0-9]+)e([0-9]+) || "$f" =~ ([0-9]+)x([0-9]+) ]]; then
      echo "Found '$f'"
      
      for movie in *\ *.{avi,mkv,mp4} ; do
        if [ -e "$movie" ]; then

          NEW_NAME=`echo "${movie%.*}".srt`
          if [ "$f" = "${NEW_NAME}" ]; then
            echo "  Already ok"
          elif [ -e "${NEW_NAME}" ]; then
            echo "  A file named '${NEW_NAME}' already exist, skipping"
          else
            mv "$f" "${NEW_NAME}"
            echo "  Renamed '$f' in '${NEW_NAME}'"
          fi
          break;
        fi
      done
    fi
  fi
done

If there's a simpler way to achieve the same, please share.

Thanks for help!



from Recent Questions - Stack Overflow https://ift.tt/39yiQtd
https://ift.tt/eA8V8J

No comments:

Post a Comment