简体   繁体   中英

Changing codepage in .bat file (Win7 vs Win Vista)

I have a strange issue while trying to change the codepage in a .bat file.

When I execute the following .bat file in Windows 7 it executes fine.

The codepage gets changed and program.exe get executed.

The batch file:

chcp 65001

"D:\program.exe" /opt ÄiÜ

pause

However when I start the .bat file from Windows Vista the codepage gets changed and after that the batch file is exited.

So program.exe never gets executed.
However when I run the two commands manually from the commandline it does work.

Any idea how to get this working under Windows Vista from .bat file?

It's new to me that this works with Win7, in Vista and XP it's normal that batch files aren't work if the codepage is changed to 65001.

But you can use a workaraound

(
  chcp 65001
  cmd /c type myFile.txt
  chcp 850
)
echo the batch is still alive

This works, as the complete block is cached while the codepage is changed.

In your case (with german umlauts) you could better use the codepage 1252

chcp 1252
echo ÄÖÜß

A less ugly solution, I use it when I need to use filenames with special characters as parameters in batch files:

  1. Direct the output of dir command to a txt file ( dir c:\\foldername > diroutput.txt )
  2. Open diroutput.txt in notepad, find and copy to clipboard the filename with special characters (it will look like garbage, but it's ok)
  3. Paste the filename to the batchfile opened in notepad.

If you type out the batchfile content in "dos" window ( type batchfilename.bat ) you will see the filename is correct. (It will also look correct in totalcommander built in fileviewer, but it will look garbage in notepad)

Have you checked return code of chcp ( chcp 65001 & echo %ERRORLEVEL% )?

Anyway, try chcp 65001 & "D:\\program.exe" /opt ÄiÜ & chcp 850 .

I've found a (very dirty) solution which works for me.

By the looks of it it just isn't possible what I want to do.

What I've done to make it work is the following:

  • Instead of trying to create a batchfile I create a .txt file (with the same contents as the batchfile).
  • I've written a very simple C# program which reads the .txt file and executes the content.

As I said it's pretty dirty but it works for me.

If other answer are added here I'll try those as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM