Batch creation of a list of folders : can't echo accented characters

只谈情不闲聊 提交于 2019-12-02 01:47:33

问题


I use this batch instruction to generate a list of every subdirectory NOT containing a .zip file :

@echo off
for /d /r %%f in (*) do (
if not exist %%f\*.zip (
echo %%f >>G:\backup\folders.txt
)
)

The problem is this generates a list in which accents break the returned results, for instance

G:\backup\! d‚j… upload‚\

instead of

G:\backup\! déjà uploadé‚\

I read that batch can only deal with Unicode characters by default. There are tricks to make it eat paths with an accent, but I found NO trick to make it output (echo) results with accents.

Would you know how to do that ? I'd be most grateful!


回答1:


The standard code page is code page 850 in non Unicode console windows in Western European countries on Windows which belongs to group of OEM code pages.

The standard code page is Windows-1252 in non Unicode GUI windows in Western European countries on Windows which belongs to group of ANSI code pages although this code page is not an ANSI standard. But Windows-1252 is similar to standardized code page ISO-8859-1 with the exception of the characters with code value 128 (0x80) to 159 (0x9F).

As most folder and file names are created in Windows GUI applications, characters with a code value greater 127 are often using Windows-1252.

For example the folder name ! déjà uploadé‚ is in bytes (hexadecimal)

21 20 64 E9 6A E0 20 75 70 6C 6F 61 64 E9 82

Those 15 bytes are displayed with a font using code page 1252 as ! déjà uploadé‚ but with using code page 850 as ! d‚j… upload‚'

The solution for this batch file is to use command chcp (change code page) before outputting the folder names with echo and redirecting them into a text file opened later in a Windows GUI editor.

@echo off
chcp 1252>nul
for /d /r %%f in (*) do (
    if not exist %%f\*.zip (
        echo %%f>>G:\backup\folders.txt
    )
)

Note: There is no space left to >nul and >>G:\backup\folders.txt. For an explanation see answer on when running batch file from java , 1 randomly appears before >>.



来源:https://stackoverflow.com/questions/26991230/batch-creation-of-a-list-of-folders-cant-echo-accented-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!