From 9bc9a61a25708da94fcacf25928c21330dcc8ba5 Mon Sep 17 00:00:00 2001 From: Brandon Rodriguez <brodriguez8774@gmail.com> Date: Mon, 12 Oct 2020 02:20:20 -0400 Subject: [PATCH] Add optional handling for "bad" file name prefixes and extensions --- readme.md | 1 + run.sh | 37 ++++++++++++++++++++++++++++++------- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/readme.md b/readme.md index a96c5ac..17981b6 100644 --- a/readme.md +++ b/readme.md @@ -14,5 +14,6 @@ Organizes files by type. * Second arg (required) - Expected to be the output directory. ### Flags +* `-b` - Appropriately renames files with known "bad" file name prefixes and file extensions. * `-no-exe` - Skips handling of windows exe files. * `-no-sort` - Skips sorting of files into labelled subdirectories. diff --git a/run.sh b/run.sh index 51e36ce..ba6e130 100755 --- a/run.sh +++ b/run.sh @@ -52,9 +52,6 @@ function main () { exit 1 fi - echo "input_dir: ${input_dir}" - echo "output_dir: ${output_dir}" - # Validate passed args. if [[ "${flags[@]}" =~ "no-sort" && "${input_dir}" == "${output_dir}" ]] then @@ -89,17 +86,23 @@ function main () { # Determines full directory location (based on file type) and then calls move_file() function. ## function handle_file () { - local file=${1} - if [[ "${file_extension}" == "" ]] then # First, non-recursive entry into function. - parse_file_name ${file} + parse_file_name ${1} + orig_file_location="${file_name}${file_extension}" to_lower ${file_extension} file_extension=${return_value} fi + # Check for flag. + if [[ "${flags[@]}" =~ "b" ]] + then + # Handle for known "bad" file name values. + handle_bad_file_names + fi + # Handle based on found extension. # Handle images. if [[ "${file_extension}" == ".jpg" || @@ -353,6 +356,26 @@ function handle_file () { } +### + # Handles for known "bad" file name values. + # Includes known "bad" name prefixes and "bad" file extensions. + ## +function handle_bad_file_names () { + + # Handle for discord spoiler prefix. + if [[ "${file_name}" == "SPOILER_"* ]] + then + file_name=${file_name#SPOILER_} + fi + + # Handle for silly jpg file extension. + if [[ "${file_extension}" == ".jpg_large.jpg" ]] + then + file_extension=".jpg" + fi +} + + ### # Handles physically moving file between locations. # Includes handling for if file with identical name already exists in output directory. @@ -379,7 +402,7 @@ function move_file () { done fi - local input_location="${input_dir}/${orig_file_name}${file_extension}" + local input_location="${input_dir}/${orig_file_location}" local output_location="${output_subdir}/${file_name}${file_extension}" echo -e "Moving ${text_purple}${orig_file_name}${file_extension}${text_reset} to ${text_purple}${output_location}${text_reset}" -- GitLab