name : add name_prefix & bugfixes.

This commit is contained in:
Dmitry Voronin 2023-11-08 16:47:12 +03:00
parent 2c55a5e61e
commit 9188526612
2 changed files with 124 additions and 54 deletions

View file

@ -542,9 +542,10 @@ Command|Description
`name [FILES]`|Replace all non-alphanumeric characters in file with underscores.
`name_hash [FILES]`|Replace all file names with hashes, keep extension.
`name_hash_check [FILES]`|Check all file hashes.
`name_series <SEASON>`|Rename files to Jellyfin's show format: `Episode S01E01.mkv`.
`name_manga <SEASON>`|Rename files to Kavita's manga format: `Name Vol.1 Ch.01.cbr`.
`name_series <SEASON> [FILES]`|Rename files to Jellyfin's show format: `Episode S01E01.mkv`.
`name_manga <SEASON> [FILES]`|Rename files to Kavita's manga format: `Name Vol.1 Ch.01.cbr`.
`name_ext <EXTENSION> [FILES]`|Change file extensions.
`name_prefix [OLD] [NEW] [FILES]`|Change file prefix.
## Ncdu.

View file

@ -68,9 +68,8 @@ name_hash()
# process.
for target in "${targets[@]}"; do
# process only targets.
[[ -f "${target}" ]] || continue
# process only files.
if [[ -f "${target}" ]]; then
# increment count.
((count++))
@ -100,6 +99,13 @@ name_hash()
# show change.
echo -e "${status}"
else
# Increment count.
((count++))
# Report skip.
echo -e "${color_yellow}[${count}/${total}] ${target}: skipping directory.${color_default}"
fi
done
}
@ -115,12 +121,12 @@ name_hash_check()
# all targets by default.
if [[ "${targets}" = "" ]]; then
targets=([^.]*)
total=${#target[@]}
total=${#targets[@]}
fi
# process.
for target in "${targets[@]}"; do
# process only targets.
# process only files.
if [[ -f "${target}" ]]; then
# increment count.
((count++))
@ -139,6 +145,12 @@ name_hash_check()
echo -e "${color_red}${status}: failed.${color_default}"
((failed++))
fi
else
# Increment count.
((count++))
# Report skip.
echo -e "${color_yellow}[${count}/${total}] ${target}: skipping directory.${color_default}"
fi
done
@ -153,13 +165,14 @@ name_hash_check()
}
# rename files for Jellyfin series, i.e. Episode S01E01.mkv
# usage: name_series <SEASON>
# usage: name_series <SEASON> [FILES]
name_series()
{
local season="${1}" # season number.
local targets="${@:2}" # target files.
local count=0 # processed counter.
local total=${#} # total to process.
local season="${1}" # Season number.
local targets=("${@:2}") # Target files.
local count=0 # Processed counter.
local episode=0 # Current episode count.
local total=${#} # Total to process.
# error when no season number specified.
if [[ "${season}" = "" ]]; then
@ -179,9 +192,10 @@ name_series()
if [[ -f "${target}" ]]; then
# increment episode number.
((count++))
((episode++))
# extract new name.
local new_name="Episode S${season}E$(printf %02d ${count}).${target##*.}"
local new_name="Episode S${season}E$(printf %02d ${episode}).${target##*.}"
# prepare status.
local status="[${count}/${total}] ${target} -> ${new_name}"
@ -189,19 +203,26 @@ name_series()
# rename target.
mv -- "${target}" "${new_name}" &> /dev/null
echo -e "${status}"
else
# Increment count.
((count++))
# Report skip.
echo -e "${color_yellow}[${count}/${total}] ${target}: skipping directory.${color_default}"
fi
done
}
# rename files for Kavita manga format.
# usage: name_manga <SEASON>
# usage: name_manga <SEASON> [FILES]
name_manga()
{
local season="${1}" # season number.
local targets="${@:2}" # target files.
local count=0 # processed counter.
local total=${#} # total to process.
local manga="${PWD##*/}" # manga name.
local season="${1}" # Season number.
local targets=("${@:2}") # Target files.
local count=0 # Processed counter.
local episode=0 # Current episode count.
local total=${#} # Total to process.
local manga="${PWD##*/}" # Manga name.
# error when no season number specified.
if [[ "${season}" = "" ]]; then
@ -221,9 +242,10 @@ name_manga()
if [[ -f "${target}" ]]; then
# increment episode number.
((count++))
((episode++))
# extract new name.
local new_name="${manga} Vol.${season} Ch.${count}.${target##*.}"
local new_name="${manga} Vol.${season} Ch.${episode}.${target##*.}"
# prepare status.
local status="[${count}/${total}] ${target} -> ${new_name}"
@ -231,6 +253,12 @@ name_manga()
# rename target.
mv -- "${target}" "${new_name}" &> /dev/null
echo -e "${status}"
else
# Increment count.
((count++))
# Report skip.
echo -e "${color_yellow}[${count}/${total}] ${target}: skipping directory.${color_default}"
fi
done
}
@ -240,16 +268,13 @@ name_manga()
name_ext()
{
local extension="${1}" # new extension.
local targets="${@:2}" # target file(s).
local targets=("${@:2}") # target file(s).
local count=0 # processed counter.
local total=${#} # total to process.
# remove extension name from total files.
((total--))
local total=$((${#}-1)) # total to process.
# error when no new extension specified.
if [[ "${extension}" = "" ]]; then
echo "usage: name_ext <EXTENSION> [targets]"
echo "usage: name_ext <EXTENSION> [FILES]"
return 1
fi
@ -269,14 +294,58 @@ name_ext()
# extract new name.
local new_name="${target%.*}"."${extension}"
# Prepare status.
local status="[${count}/${total}] ${target} -> ${new_name}"
# rename target.
mv -- "${target}" "${new_name}" &> /dev/null
# show change.
echo "[${count}/${total}] ${target} -> ${new_name}"
echo "${status}"
else
# Increment count.
((count++))
# Report skip.
echo -e "${color_yellow}[${count}/${total}] ${target}: skipping directory.${color_default}"
fi
done
}
# Change file's prefixes.
# Usage: name_prefix [OLD] [NEW] [FILES]
name_prefix()
{
local old="${1}" # Old prefix.
local new="${2}" # New prefix.
local targets=("${@:3}") # Target files.
local count=0 # Total files processed.
local total=$((${#}-2)) # Total files to process.
# 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}"
# Rename.
mv -- "${target}" "${new_name}" &> /dev/null
# Show change.
echo "${status}"
done
}
# export for parallel.
export -f name name_hash name_hash_check name_ext