name : add fix_numbering that fixes leading numbers.

This commit is contained in:
Dmitry Voronin 2023-12-10 07:20:48 +03:00
parent db41c5eb82
commit 2bc5285df9
3 changed files with 102 additions and 16 deletions

View file

@ -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++))

View file

@ -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'
}

View file

@ -223,6 +223,7 @@ Command|Description
`name_prefix <OLD> <NEW> [FILES]`|Change file name prefix. All matching files by default.
`name_postfix <OLD> <NEW> [FILES]`|Change file name postfix. All matching files by default.
`name_replace <OLD> <NEW> [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.