Running BAT/CMD file with accented characters in it

后端 未结 7 905
面向向阳花
面向向阳花 2021-01-31 05:51

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.<

相关标签:
7条回答
  • 2021-01-31 06:09

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

    0 讨论(0)
  • 2021-01-31 06:12

    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!

    0 讨论(0)
  • 2021-01-31 06:16

    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
    0 讨论(0)
  • 2021-01-31 06:16

    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)

    0 讨论(0)
  • 2021-01-31 06:20

    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!

    0 讨论(0)
  • 2021-01-31 06:22

    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.

    0 讨论(0)
提交回复
热议问题