name : add fix_numbering that fixes leading numbers.
This commit is contained in:
parent
db41c5eb82
commit
2bc5285df9
|
@ -474,3 +474,83 @@ function name_replace() {
|
|||
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++))
|
||||
fi
|
||||
done
|
||||
|
||||
if [[ ${failed} != 0 ]]; then
|
||||
echo -e "${color_bred}Failed: ${failed}.${color_default}"
|
||||
false
|
||||
fi
|
||||
}
|
||||
|
|
|
@ -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'
|
||||
}
|
||||
|
|
|
@ -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.
|
||||
|
||||
|
|
Reference in a new issue