Manage paths with accented characters

大憨熊 提交于 2019-12-03 16:17:52

Batch by default configuration can't print unicode characters but CAN recognize that chars and use it. That means you can't print "ú" but you can acces to a folder called "ú" or touch a file called "ú".

Then if your script fails to acces a file with accented characters maybe the reason is because the encoding of your script is not ANSI, so open the script in notepad and ensure to save it with ANSI encoding (no utf-8 or unicode).

Example:

@echo off

CHCP 850 >NUL

for %%# in (*.txt) do (

    Echo [+] reading File: .\%%~nx# | MORE
    Type "%%~nx#"
) 

Pause>NUL

Output:

[+] reading File: .\áéíóúàèìòù ñ Ñ ç Ç.txt

This is the text content of my file with ISO-Latin characters

If you want to print accented characters in CMD then you need to make the char conversion, this can do the trick:

@echo off
CHCP 850 >NUL
copy con Mychars.txt

In that prompt you will write the characters you want, for example "éíóú", then you will get a "Mychars.txt" textfile with this content:

 ‚¡¢£

Now you can use that chars to print the accented chars:

@echo off
echo ‚¡¢£
Pause>NUL

Output:

éíóú

PS1: Remember to do all the things that I said saving the script in ANSI encoding.

PS2: Notice how I'm using CHCP 850 command, that means the codepage of the CMD, By default CMD uses the 850 codepage but I launch the CMD with a default 1250 codepage to avoid all those problems so in my examples I needed to use codepage 850 to show you.

You can set the default CMD Codepage with a registry key:

REG ADD "HKCU\Software\Microsoft\Command Processor" /V "Autorun" /T "REG_SZ" /D "chcp 1250 >NUL" /F >NUL 2>&1

And you can read about the codepages here: http://msdn.microsoft.com/en-us/library/cc195064.aspx

This doesn't work:

@echo off
start "" "C:\Users\dmb3419\Documents\dev\ééééééééé"

This works:

@echo off
chcp 1252
start "" "C:\Users\dmb3419\Documents\dev\ééééééééé"
Feldmesser

I had a similar problem with French accents in pathnames using a .bat and xcopy.

using:

@echo off
chcp 1252

handled this for me and the accents were correctly recognised in the .bat file. running under MS server2012 R2 cmd.exe

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