Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running BAT/CMD file with accented characters in it

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?

like image 396
Kryptic Coder Avatar asked Sep 28 '11 13:09

Kryptic Coder


People also ask

How do you type special characters in CMD?

In Windows, you can type any character you want by holding down the ALT key, typing a sequence of numbers, then releasing the ALT key.

What does @echo off do in Bat?

Example# @echo off prevents the prompt and contents of the batch file from being displayed, so that only the output is visible. The @ makes the output of the echo off command hidden as well.

What is %% in a BAT file?

Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. ( <set> ) Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.


4 Answers

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
like image 108
katalin_2003 Avatar answered Oct 06 '22 08:10

katalin_2003


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!

like image 20
Metalcoder Avatar answered Oct 06 '22 08:10

Metalcoder


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

like image 23
genuinefafa Avatar answered Oct 06 '22 08:10

genuinefafa


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)

like image 25
Jordan Evens Avatar answered Oct 06 '22 10:10

Jordan Evens