我有一个bat文件,它读取文件的行,然后根据给定的参数尝试创建文件或文件夹。
问题在于,当它变成chaâлîş的字符时,它不起作用。
这是我的代码:
IF "%1"=="" GOTO Final
IF "%1"=="file" GOTO File
IF "%1"=="folder" GOTO Folder
:File
for /f %%i in (files.txt) do echo. > %%i.rtf
GOTO Final
:Folder
for /f "tokens=*" %%a in (folders.txt) do (
mkdir "%%a"
)
GOTO Final
:Final
到目前为止,我使用此链接尝试了什么:Manage paths with accented characters
我该如何解决这个问题?
答案 0 :(得分:1)
将CHCP XXX
放入批处理中,其中XXX是与文本文件(files.txt和folders.txt)的编码匹配的代码页。请注意,您可以使用等效于UTF-8的CHCP 65001
,并且应该可以毫无问题地处理大多数diactrics。
答案 1 :(得分:0)
避免在文件和文件夹名称中使用重音字符。否则,mojibake在Windows命令行中得到保证。
md files 2>NUL
pushd files
md unASCII 2>NUL
chcp 852 >nul
echo ěščřžýáíé-852>diacritic--852.txt
chcp 1250 >nul
echo ěščřžýáíé1250>diacritic-1250.txt
chcp 1250 >nul
findstr /R "^" "diacritic-*.txt"
for %G in (diacritic*.txt) do @for /F %g in (%G) do @echo %G:%g
for %G in (diacritic*.txt) do @for /F %g in (%G) do @echo(%~nG:%g>"unASCII\%gANSI.txt"
chcp 852 >nul
findstr /R "^" "diacritic-*.txt"
for %G in (diacritic*.txt) do @for /F %g in (%G) do @echo %G:%g
for %G in (diacritic*.txt) do @for /F %g in (%G) do @echo(%~nG:%g>"unASCII\%g-OEM.txt"
popd
请注意,上面的CLI
命令列表不是.bat
代码段。然而,复制&在命令行窗口中粘贴它会显示下一个输出,显示当创建文件时文件中的代码页实际显示 必须互相插入。否则,可见晶体 mojibake ,参见例如findstr /R "^" "diacritic-*.txt"
:
==>md files 2>NUL
==>pushd files
==>md unASCII 2>NUL
==>chcp 852 >nul
==>echo ěščřžýáíé-852>diacritic--852.txt
==>chcp 1250 >nul
==>echo ěščřžýáíé1250>diacritic-1250.txt
==>
==>chcp 1250 >nul
==>findstr /R "^" "diacritic-*.txt"
diacritic--852.txt:Řçźý§ě ˇ‚-852
diacritic-1250.txt:ěščřžýáíé1250
==>for %G in (diacritic*.txt) do @for /F %g in (%G) do @echo %G:%g
diacritic--852.txt:Řçźý§ě ˇ‚-852
diacritic-1250.txt:ěščřžýáíé1250
==>for %G in (diacritic*.txt) do @for /F %g in (%G) do @echo(%~nG:%g>"unASCII\%gANSI.txt"
==>chcp 852 >nul
==>findstr /R "^" "diacritic-*.txt"
diacritic--852.txt:ěščřžýáíé-852
diacritic-1250.txt:ýÜŔ°×řßÝÚ1250
==>for %G in (diacritic*.txt) do @for /F %g in (%G) do @echo %G:%g
diacritic--852.txt:ěščřžýáíé-852
diacritic-1250.txt:ýÜŔ°×řßÝÚ1250
==>for %G in (diacritic*.txt) do @for /F %g in (%G) do @echo(%~nG:%g>"unASCII\%g-OEM.txt"
==>popd
我们已将ěščřžýáíé
字符串(后跟CHCP编号)写入下一个文件:
ěščřžýáíé-852
字符串位于files\diacritic--852.txt
文件中,
{li> ěščřžýáíé1250
files\diacritic-1250.txt
文件中的字符串。
然后,我们使用这些字符串创建<String><Chcp><CPID>.txt
名称模式的文件,其中
<String>
= ěščřžýáíé
字符串,其中的变音符号从diacritic-<Chcp>.txt
文件中读取; <Chcp>
= -852
或1250
:编写diacritic-<Chcp>.txt
文件的代码页; <CPID>
= -OEM
或ANSI
:此文件分别在852
和1250
下编写的代码页名称的文本缩写。< / LI>
让我们再次在命令行窗口中使用最后四个文件:Copy&Paste
跟随代码段:
chcp 437 >nul
dir /B /S "files\unASCII\*.txt"
for %G in (files\unASCII\ěščřžýáíé*.txt) do @echo %G
findstr /S /R "^" "files\unASCII\ěščřžýáíé*.txt"
chcp 1250 >nul
for %G in (files\unASCII\ěščřžýáíé*.txt) do type "%G"
chcp 852 >nul
for %G in (files\unASCII\ěščřžýáíé*.txt) do type "%G"
输出:我们可以一次又一次地看到 mojibake :
==>chcp 437 >nul
==>dir /B /S "files\unASCII\*.txt"
d:\bat\files\unASCII\ýÜŔ°×řßÝÚ1250-OEM.txt
d:\bat\files\unASCII\ěščřžýáíé-852-OEM.txt
d:\bat\files\unASCII\ěščřžýáíé1250ANSI.txt
d:\bat\files\unASCII\Řçźý§ě ˇ‚-852ANSI.txt
==>for %G in (files\unASCII\ěščřžýáíé*.txt) do @echo %G
files\unASCII\ěščřžýáíé-852-OEM.txt
files\unASCII\ěščřžýáíé1250ANSI.txt
==>findstr /S /R "^" "files\unASCII\ěščřžýáíé*.txt"
==>
==>chcp 1250 >nul
==>for %G in (files\unASCII\ěščřžýáíé*.txt) do type "%G"
==>type "files\unASCII\ěščřžýáíé-852-OEM.txt"
diacritic--852:Řçźý§ě ˇ‚-852
==>type "files\unASCII\ěščřžýáíé1250ANSI.txt"
diacritic-1250:ěščřžýáíé1250
==>chcp 852 >nul
==>for %G in (files\unASCII\ěščřžýáíé*.txt) do type "%G"
==>type "files\unASCII\ěščřžýáíé-852-OEM.txt"
diacritic--852:ěščřžýáíé-852
==>type "files\unASCII\ěščřžýáíé1250ANSI.txt"
diacritic-1250:ýÜŔ°×řßÝÚ1250
OOPS ,为什么findstr
没有输出?我们来使用
chcp 1250 >nul
findstr /S /R "^" "files\unASCII\*.txt"
chcp 852 >nul
findstr /S /R "^" "files\unASCII\*.txt"
输出显示findstr
不仅会在文件内容 中导致 mojibake ,而且会在文件中名称< / strong> 以及:
==>chcp 1250 >nul
==>findstr /S /R "^" "files\unASCII\*.txt"
FINDSTR: Cannot open files\unASCII\ŤsR›zr ˇ‚1250-OEM.txt
FINDSTR: Cannot open files\unASCII\escrzŤ˙'-852-OEM.txt
FINDSTR: Cannot open files\unASCII\escrzŤ˙'1250ANSI.txt
FINDSTR: Cannot open files\unASCII\RÎzŤäe?'-852ANSI.txt
==>chcp 852 >nul
==>findstr /S /R "^" "files\unASCII\*.txt"
FINDSTR: Cannot open files\unASCII\ŹsRŤzráíé1250-OEM.txt
FINDSTR: Cannot open files\unASCII\escrzŹ ş'-852-OEM.txt
FINDSTR: Cannot open files\unASCII\escrzŹ ş'1250ANSI.txt
FINDSTR: Cannot open files\unASCII\R╬zŹńeś?'-852ANSI.txt
仅供参考:CHCP 65001
(UTF-8
)也无法提供帮助......而且根据MSDN: Naming Files, Paths, and Namespaces,Windows NTFS对象名称似乎是UTF-16
编码的:
在较新的文件系统上,例如
NTFS
,exFAT
,UDFS
和FAT32
, Windows将长文件名存储在 Unicode ...文件中的磁盘上 system将路径和文件名视为 WCHAR 的不透明序列。
此外:
shell和文件系统有不同的要求。 是的 可以使用shell用户的Windows API创建路径 界面无法正确解释。