如何使用bat脚本创建带变音符号的文件/文件夹

时间:2015-07-29 14:09:46

标签: batch-file encoding diacritics

我有一个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

  1. bat脚本是ANSI
  2. CHCP 1250> NUL
  3. 我该如何解决这个问题?

2 个答案:

答案 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编号)写入下一个文件:

    {li} ěščřžýáíé-852字符串位于files\diacritic--852.txt文件中, {li> ěščřžýáíé1250 files\diacritic-1250.txt文件中的字符串。

然后,我们使用这些字符串创建<String><Chcp><CPID>.txt名称模式的文件,其中

  • <String> = ěščřžýáíé字符串,其中的变音符号从diacritic-<Chcp>.txt文件中读取;
  • <Chcp> = -8521250:编写diacritic-<Chcp>.txt文件的代码页;
  • <CPID> = -OEMANSI:此文件分别在8521250下编写的代码页名称的文本缩写。< / 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 65001UTF-8)也无法提供帮助......而且根据MSDN: Naming Files, Paths, and Namespaces,Windows NTFS对象名称似乎是UTF-16编码的:

  

在较新的文件系统上,例如NTFSexFATUDFSFAT32,   Windows将长文件名存储在 Unicode ...文件中的磁盘上   system将路径和文件名视为 WCHAR 的不透明序列。

此外:

  

shell和文件系统有不同的要求。 是的   可以使用shell用户的Windows API创建路径   界面无法正确解释