diff --git a/.config/bash/module/name.sh b/.config/bash/module/name.sh index 99f95a0..e815978 100644 --- a/.config/bash/module/name.sh +++ b/.config/bash/module/name.sh @@ -28,7 +28,7 @@ function name() { ext=".${target##*.}" name="${target%.*}" fi - + # Files w/o extension support. [[ "${ext#.}" = "${name}" ]] && ext="" @@ -95,7 +95,7 @@ function name_hash() { # hash the new name. local hash=$(pv "${target}" | sha1sum | cut -d\ -f1) new_name="${hash,,}${extension,,}" - + # prepare status. local status="[${count}/${total}] ${target} -> ${new_name}" @@ -111,7 +111,7 @@ function name_hash() { # rename target. mv -- "${target}" "${new_name}" - + if [[ ${?} != 0 ]]; then echo -e "${color_bred}${status}: Failed.${color_default}" ((failed++)) @@ -209,7 +209,7 @@ function name_series() { # rename target. mv -- "${target}" "${new_name}" - + if [[ ${?} != 0 ]]; then echo -e "${color_bred}${status}: Failed.${color_default}" ((failed++)) @@ -265,7 +265,7 @@ function name_manga() { # rename target. mv -- "${target}" "${new_name}" - + if [[ ${?} != 0 ]]; then echo -e "${color_bred}${status}: Failed.${color_default}" ((failed++)) @@ -300,7 +300,7 @@ function name_ext() { targets=($(ls --classify | grep -v /\$)) total=${#targets[@]} fi - + # process. for target in "${targets[@]}"; do # increment count. @@ -318,7 +318,7 @@ function name_ext() { # rename target. mv -- "${target}" "${new_name}" - + if [[ ${?} != 0 ]]; then echo -e "${color_bred}${status}: Failed.${color_default}" ((failed++)) @@ -348,7 +348,7 @@ function name_prefix() { targets=(${old}*) total=${#targets[@]} fi - + # Process. for target in "${targets[@]}"; do # Increment counter. @@ -363,10 +363,10 @@ function name_prefix() { # 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++)) @@ -396,7 +396,7 @@ function name_postfix() { targets=(*${old}) total=${#targets[@]} fi - + # Process. for target in "${targets[@]}"; do # Increment counter. @@ -411,10 +411,10 @@ function name_postfix() { # 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++)) @@ -444,7 +444,7 @@ function name_replace() { targets=(*${old}*) total=${#targets[@]} fi - + # Process. for target in "${targets[@]}"; do # Increment counter. @@ -459,10 +459,90 @@ function name_replace() { # 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 +} + +# Fix numbering for numbered files. I.e if there are 10 items and some of them start without zero, then append zero to it. 1..10 -> 01..10. +# Usage: name_fix_numbering [FILES] +function name_fix_numbering() { + local IFS=$'\n' + local targets=("${@}") + local count=0 + local total=${#} + local failed=0 + local power=0 + + # All files by default. + if [[ "${targets[*]}" = "" ]]; then + targets=(*) + total=${#targets[@]} + fi + + # Count leading zeroes. + local highest=0 + for target in "${targets[@]}"; do + local digits=($(_parse_ints "${target}")) + local digit="${digits[0]}" + digit=$((10#${digit})) + + [[ "${digit}" -gt "${highest}" ]] && highest="${digit}" + done + + local i=${highest} + while [[ i -gt 0 ]]; do + ((power++)) + i=$((${i}/10)) + done + + # Process. + for target in "${targets[@]}"; do + ((count++)) + + local status="[${count}/${total}] ${target}" + + # Check that starts with a digit. + if [[ ! "${target}" =~ ^[0-9] ]]; then + echo "${status}" + continue + fi + + # Prepare new name. + local digits=($(_parse_ints "${target}")) + local digit="${digits[0]}" + digit=$((10#${digit})) + local new_name=$(printf "%0${power}d" "${digit}")"${target#${digits[0]}}" + + status="${status} -> ${new_name}" + + # Skip if the same. + if [[ "${target}" = "${new_name}" ]]; then + echo "${status}" + continue + fi + + # Check that file does not exist. + if [[ -e "${new_name}" ]]; then + echo -e "${color_bred}${status}: File exists!${color_default}" + return 1 + fi + + echo "${status}" + + mv -- "${target}" "${new_name}" + if [[ ${?} != 0 ]]; then echo -e "${color_bred}${status}: Failed.${color_default}" ((failed++)) diff --git a/.config/bash/module/util.sh b/.config/bash/module/util.sh index 854413d..9a4c963 100644 --- a/.config/bash/module/util.sh +++ b/.config/bash/module/util.sh @@ -2,3 +2,8 @@ function _core_count() { cat /proc/cpuinfo | grep ^processor | wc -l } + +# Parse integers from mixed string. +function _parse_ints() { + echo "${*}" | tr '\n' ' ' | sed -e 's/[^0-9]/ /g' -e 's/^ *//g' -e 's/ *$//g' | tr -s ' ' | sed 's/ /\n/g' +} diff --git a/.doc/bash.md b/.doc/bash.md index 81b37c6..4eadb89 100644 --- a/.doc/bash.md +++ b/.doc/bash.md @@ -223,6 +223,7 @@ Command|Description `name_prefix [FILES]`|Change file name prefix. All matching files by default. `name_postfix [FILES]`|Change file name postfix. All matching files by default. `name_replace [FILES]`|Replace part of the name. All matching files by default. +`name_fix_numbering [FILES]`|Fix numbering for numbered files. I.e if there are 10 items and some of them start without zero, then append zero to it. 1..10 -> 01..10. ## Ncdu.