If it can be achieved, remember to like and share, thank you~
The method mentioned in this article is how to restore the correct Chinese name of the file name when the file name is garbled. It is not intended to avoid the occurrence of garbled characters once and for all. This is due to the garbled names of downloaded files, which are often caused by the combined influence of three factors: the system, the browser, and the website. To avoid the appearance of garbled characters, you can only adjust and configure your personal system or browser for specific websites based on specific circumstances.
PS: If you are a developer, you can let the background staff handle the garbled problem of downloaded file names and contents.
Because I use a MAC, I only explain the MAC system.
macOS system
The macOS system comes with powerful automation support, such as Automator and AppleScript. Here, I used Automator to write a file service, using the following method:
1. Right-click the file that needs to be processed and enter Create Service
As shown below:
2. This service does not require a network and supports batch processing of multiple files or folders. There is only the operation of "Run Shell Script", and the Shell code is:
PS: Download here first python is divided into two versions, choose according to your own needs
Python version
# 调用 Python 内置模块进行解码
alias urldecode='python -c "import sys, urllib as ul; print ul.unquote_plus(sys.argv[1])"'
# 遍历选择的文件列表,进行重命名
for f in "$@"
do
newName=$(urldecode "$f")
mv "$f" "$newName"
done
Python 3.0+ version
import sys
import urllib.parse
import os
import shutil
def urldecode(url):
return urllib.parse.unquote_plus(url)
for f in sys.argv[1:]:
newName = urldecode(f)
shutil.move(f, newName)
Copy and paste the corresponding script above into the image below:
3. Finally save, name the service as UrlDecode
Right-click again to open the file you need to parse, as shown in the figure:
Find the service you created. If it cannot be found, please restart the computer.
At this point, you have solved most of the garbled file names. Hahahaha, I would like to add it to your favorites.
2. 自å type is garbled
In other cases, the file names downloaded by macOS 10.13.4 Chinese system Safari and Chrome browser are normal. In the English system, the file names downloaded by both browsers are garbled.
1. Follow the above steps to create a new service named å to fix the garbled file name.
There are two Python versions
Pyhton:
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
Python 3.0+ version
import os
import shutil
import random
import sys
for f in sys.argv[1:]:
fileName = os.path.basename(f)
filePath = os.path.dirname(f)
# 两种乱码类型 GBK、UTF-8
fileNewName = fileName.encode('latin1').decode('gbk') if fileName.encode('utf-8').decode('utf-8', 'ignore') != fileName else fileName.encode('utf-8').decode('utf-8', 'ignore')
# 文件名正常或乱码类型不属于上述两种时,新文件名为空,则跳过
if fileNewName:
# 避免文件重复:如果已存在修复后的文件名,则在新文件名后加上随机字符串。
if os.path.exists(os.path.join(filePath, fileNewName)):
newFileName = f"{fileNewName}-{random.randint(0, 99999)}"
else:
newFileName = fileNewName
shutil.move(f, os.path.join(filePath, newFileName))
2. Just remember to save the file service and you’re done.
3. Conclusion
You can see that the above code is used to solve two types of garbled characters. Among them, GBK garbled characters are what we often encounter, and UTF-8 type is relatively rare, so we added them conveniently.
The appearance of garbled characters is always annoying. I hope the methods introduced in this article can help you solve the problems you encounter.