Running BAT/CMD file with accented characters in it

可紊 提交于 2019-12-02 19:27:34

Another way of doing this, in Windows, is by using wordpad.exe:

  1. Run wordpad.exe
  2. Write your script as you usually do, with accents
  3. Choose Save as > Other formats
  4. Choose to save it as Text document MS-DOS (*.txt)
  5. Change the file extension from .txt to .bat
Metalcoder

I had the same problem, and this answer solved it. Basically you have to wrap your script with a bunch of commands to change your terminal codepage, and then to restore it.

@echo off
for /f "tokens=2 delims=:." %%x in ('chcp') do set cp=%%x
chcp 1252>nul

:: your stuff here ::

chcp %cp%>nul

Worked like a charm!

Since you have @echo off you can't see what your batch is sending to the command prompt. Reproducing your problem with that off it seems like the ñ character gets misinterpreted since the output I see is:

C:\espa±ol\jre\bin\java -version
The system cannot find the path specified.

I was able to get it to work by echoing the command into the batch file from the command prompt, i.e.

echo C:\español\jre\bin\java.exe -version>>test.bat

This seems to translate the character into whatever the command prompt is looking for, though I've only tested it with English locale set so I don't know if it'll work in all situations for you. Also, if you open the batch in a text editor like notepad it looks wrong (C:\espa¤ol\jre\bin\java.exe)

I'm using Notepad++ and it has an option to change "character sets", OEM-US did the trick. ;)

Pete Reyna

Use Alt + 0164 for ¤ instead of Alt + 164 ñ in a batch file... It will look odd, but your script should run.

Keith

I also had the same problem. I was trying to create a simple XCOPY batch file to copy a spreadsheet from one folder to another. Its name had the "é" character in it, and it refused to copy.

Even trying to use Katalin's and Metalcoder's suggestions didn't work on my neolithic Windows XP machine. Then I suddenly thought: Why not keep things as simple as possible (as I am myself extremely simple-minded when it comes to computers) and just substitute, in the batch file code, "é" with the wildcard character "?".

And guess what? It worked!

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