27

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
  • It end up being a matter of "how you save the file". Commented Nov 2, 2021 at 5:32

7 Answers 7

28

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
3
  • I have solved this problem sometimes by writing the path through cmd edit. In Windows 8 there's no edit, though.
    – Andrestand
    Commented Oct 28, 2014 at 9:19
  • 1
    Because it converts the character codes from ANSI to DOS.
    – user6017774
    Commented Jan 3, 2017 at 0:34
  • 1
    I have tried many methods using chcp 1252 and Notepad Editor to no avail, but at the end this clear instructions worked fine and quick. Great job! +1 Commented Jul 5, 2018 at 6:56
23

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!

2
  • 1
    Worked for me with the French letters (ç for example). Thx
    – locobastos
    Commented Feb 14, 2020 at 11:41
  • 2
    It works like a charm, but I had to save the .bat file using Windows 10 Notepad with the ANSI encoding.
    – Facínora
    Commented Sep 13, 2021 at 14:49
11

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

4
  • in my case, it was UCS-2 LE BOM
    – DigCamara
    Commented Sep 13, 2018 at 17:59
  • In my case it was Western European->Windows-1252.
    – alesscor
    Commented Sep 27, 2018 at 16:59
  • @genuinefafa it depend on the language of the operating system.
    – neves
    Commented Jan 24, 2019 at 16:35
  • are you sure about that @neves? encoding should not be OS related... AFAIK Commented Jan 26, 2019 at 0:30
6

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

You can use Visual Studio Code and it will let you select the encoding you want to use. Lower right corner, you select the encoding and will display option "save with encoding". Select DOS and will save the accented chars.

3

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

1
  • Had the same problem... different character though (Alt 0162 for ó instead of Alt 0164)
    – DigCamara
    Commented Sep 13, 2018 at 18:06
2

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!

1
  • Worked for me too! Thanks a lot. Super easy solution 2023
    – fedeteka
    Commented Jun 6, 2023 at 13:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.