Running BAT/CMD file with accented characters in it

僤鯓⒐⒋嵵緔 提交于 2019-12-03 05:09:28

问题


I have a Windows batch file which has an instruction to execute an EXE file in a location whose path contains accented characters. Following are the contents of the batch file.

@echo off
C:\español\jre\bin\java.exe -version

C:\español\jre\bin\java.exe - This path exists and is proper. I can run this command directly on cmd.exe. But when I run the command from a bat/cmd file it fails saying "The system cannot find the path specified"

One way to fix this is by setting code page to 1252 (that works for me). But I'm afraid we'd have to set code pages for any non-English locale and figuring out which code page to use is pretty difficult.

Is there an alternative approach to fix this problem? Maybe a command-line option or something else?


回答1:


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



回答2:


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!




回答3:


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)




回答4:


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




回答5:


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




回答6:


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!



来源:https://stackoverflow.com/questions/7584423/running-bat-cmd-file-with-accented-characters-in-it

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