Windows 有一種簡單的方法,只需要選取需要的檔案,於第一個檔案重新命名,輸入指定的文字
就會將選取需要的檔案順次序改名,但這種改名方法的結果就不太理想
當然 Windows 上還有其他批次重新命名軟件
Linux 上亦有一種稱為 rename 的指令,一次過將大量檔案重新命名
但要讓檔案順著某個編號開始排列,rename 又好像不太適合
#!/bin/sh
run(){
destination="${1}"
source="${2}"
suf="${3}"
pre="${4}"
len="${5}"
start="${6}"
if ! [ -d "${source}" ]; then
echo "<source> '${source}' is not an exist directory"
elif ! [ -d "${destination}" ]; then
echo "<destination> '${destination}' is not an exist directory"
else
if [ "${#len}" -eq 0 ]; then
len=1
fi
if [ "${#start}" -eq 0 ]; then
start=1
fi
i=0
for f in "${source}/"*; do
if [ -f "${f}" ]; then
i=$((i+1))
fi
done
count="${i}"
length="${#count}"
if [ "${len}" -gt "${length}" ]; then
length="${len}"
fi
i="${start}"
if [ "${i}" -lt 1 ]; then
i=1
fi
for f in "${source}/"*; do
if [ -f "${f}" ]; then
no="${i}"
while [ "${#no}" -lt "${length}" ]; do
no="0${no}"
done
echo "cp '${f}' '${destination}/${pre}${no}${suf}'"
cp "${f}" "${destination}/${pre}${no}${suf}"
i=$((i+1))
fi
done
fi
}
if [ "${#}" -eq 1 ]; then
destination="${1}"
source='.'
suf=''
pre=''
length=0
start=1
can_run=1
elif [ "${#}" -eq 2 ]; then
destination="${1}"
source="${2}"
suf=''
pre=''
length=0
start=1
can_run=1
elif [ "${#}" -eq 3 ]; then
destination="${1}"
source="${2}"
suf="${3}"
pre=''
length=0
start=1
can_run=1
elif [ "${#}" -eq 4 ]; then
destination="${1}"
source="${2}"
suf="${3}"
pre="${4}"
length=0
start=1
can_run=1
elif [ "${#}" -eq 5 ]; then
destination="${1}"
source="${2}"
suf="${3}"
pre="${4}"
length="${5}"
start=1
can_run=1
elif [ "${#}" -eq 6 ]; then
destination="${1}"
source="${2}"
suf="${3}"
pre="${4}"
length="${5}"
start="${6}"
can_run=1
else
echo "${0} <destination> [source=. [suffix= [prefix= [length= [start=1]]]]]"
can_run=0
fi
if [ "${can_run}" -eq 1 ]; then
run "${destination}" "${source}" "${suf}" "${pre}" "${length}" "${start}"
fi
#可能是 plugin 問題script 中最後的 # 是不需要的
使用方法相當簡單
script 只需要任意名稱、如 a.out
利用
./a.out <destination> [source=. [suffix= [prefix= [length= [start=1]]]]]destination 必須項目,為輸出目錄
source 可選,為輸入目錄;若不設定,則使用當前目錄
suffix 可選,為輸出檔案名的後綴;若不設定,為空字串
prefix 可選,為輸出檔案名的前綴;若不設定,為空字串
length 可選,為自動編號的長度;若不設定,則使用批次量的數值長度
start 可選,為自動編號的起首數值,若不設定,則為 1
例如
./a.out ~/comic ~/Downloads .jpg file- 3 1另外,可以設定 PATH 來引入執行指令,不需要每次使用絕對或相對路徑來執行 script
沒有留言 :
張貼留言