for f in "$@"
do
if [[ -f $f ]]; then
fileName=$(basename ${f})
filePath=$(dirname ${f})
iconv -f GB18030 -t UTF-8 $f > ${filePath}/.${fileName}.tmp
mv ${filePath}/.${fileName}.tmp $f
fi
done
for f in "$@"
do
fileName=$(basename ${f})
filePath=$(dirname ${f})
# 两种乱码类型 GBK、UTF-8
# { fileNewName=$(echo $fileName | iconv -f UTF-8-Mac -t latin1 | iconv -f gbk)
# } ||
{ fileNewName=$(echo $fileName | iconv -f UTF-8-Mac -t latin1)
}
# 文件名正常或乱码类型不属上述两种时,新文件名为空,则跳过
if [ -n "$fileNewName" ]; then
# 避免文件重复:如果已存在修复后的文件名,则在新文件名后加上随机字符串。
if [ -e ${filePath}/$fileNewName ]; then
mv "$f" "${filePath}/${fileNewName}-${RANDOM}"
else
mv "$f" "${filePath}/${fileNewName}"
fi
fi
done
%E9
准确的来说这不算乱码,这是经过 URL 编码之后的中文字符串。我们只要把它们解码就可以了。
alias urldecode='python3 -c "import sys, urllib.parse; print(urllib.parse.unquote_plus(sys.argv[1]))"'
for f in "$@"
do
newName=$(urldecode "$f")
mv "$f" "$newName"
done
标签:脚本,中文,UTF,filePath,iconv,fileName,乱码,fileNewName
From: https://www.cnblogs.com/Undefined443/p/18179518