今天,在使用.bat脚本,将hello.png从"D:\mypic\备份"目录,拷贝到"D:\mypic\备份"时;发现中文乱码,弹出如下对话框:
原来的命令是:
copy D:\mypic\one\hello.png D:\mypic\备份\hello.png /y
在黑框框DOS里,却是乱码,开始寻找解决方法。
原来的.bat
bat默认是ASCII编码,现在需要把它从改成UTF-8编码。假设你的.bat文件如下:
//copyPng.bat
rem @echo offrem src file
set frFm01=D:\mypic\one\hello.png
rem dst file
set toFm01=D:\mypic\备份\hello.png
rem coye file from src to dst
copy %frFm01% %toFm01% /yrem judge dst file is existed or not
set n1=0
if exist %toFm01% (set /a n1+=1)REM output the status
if %n1% equ 1 (echo "copy Successfully!") else (echo "copy failed")
这里以copyPng.bat为例,进行说明。使用Notepad++软件打开copyPng.bat文件,在Notepad++的状态栏,将编码改成UTF-8,如图(2)所示:
然后再copyPng.bat的第一行,加上chcp 65001,如图(3)所示:
修改后的.bat
//copyPng.bat
chcp 65001
rem @echo offrem src file
set frFm01=D:\mypic\one\hello.png
rem dst file
set toFm01=D:\mypic\备份\hello.png
rem coye file from src to dst
copy %frFm01% %toFm01% /yrem judge dst file is existed or not
set n1=0
if exist %toFm01% (set /a n1+=1)REM output the status
if %n1% equ 1 (echo "copy Successfully!") else (echo "copy failed")
效果如下:
解决.bat中文乱码的方法:
- 需要设置.bat文件的编码格式为UTF-8,
- 且.bat文件的第一条语句要设为:chcp 65001