diff --git a/.README.md b/.README.md index 7eb1eb5..be2ed32 100644 --- a/.README.md +++ b/.README.md @@ -557,7 +557,8 @@ Command|Description `name_series [FILES]`|Rename files to Jellyfin's show format: `Episode S01E01.mkv`. `name_manga [FILES]`|Rename files to Kavita's manga format: `Name Vol.1 Ch.01.cbr`. `name_ext [FILES]`|Change file extensions. -`name_prefix [OLD] [NEW] [FILES]`|Change file prefix. +`name_prefix [FILES]`|Change file prefix. +`name_postfix [FILES]`|Change file postfix. ## Ncdu. diff --git a/.config/bash/module/name.sh b/.config/bash/module/name.sh index 880cc64..1683013 100644 --- a/.config/bash/module/name.sh +++ b/.config/bash/module/name.sh @@ -331,8 +331,8 @@ name_ext() fi } -# Change file's prefixes. -# Usage: name_prefix [OLD] [NEW] [FILES] +# Change file prefixes. +# Usage: name_prefix [FILES] name_prefix() { local IFS=$'\n' @@ -379,5 +379,53 @@ name_prefix() fi } +# Change file postfix. +# Usage: name_postfix [FILES] +name_postfix() +{ + local IFS=$'\n' + local old="${1}" # Old postfix. + local new="${2}" # New postfix. + local targets=("${@:3}") # Target files. + local count=0 # Total files processed. + local total=$((${#}-2)) # Total files to process. + local failed=0 + + # All targets by default. + if [[ "${targets}" = "" ]]; then + targets=([^.]*) + total=${#targets[@]} + fi + + # Process. + for target in "${targets[@]}"; do + # Increment counter. + ((count++)) + + # Create new name. + local new_name="${new}${target%$old}" + + # Prepare status. + local status="[${count}/${total}] ${target} -> ${new_name}" + echo -e "${status}" + + # Warning on no change. + [[ "${target}" = "${new_name}" ]] && continue + + # Rename. + mv -- "${target}" "${new_name}" + + if [[ ${?} != 0 ]]; then + echo -e "${color_bred}${status}: Failed.${color_default}" + ((failed++)) + fi + done + + if [[ ${failed} != 0 ]]; then + echo -e "${color_bred}Failed: ${failed}.${color_default}" + false + fi +} + # export for parallel. export -f name name_hash name_hash_check name_ext