windows 更改 .bat 文件中的代码页(Win7 与 Win Vista)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7737599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 18:15:34  来源:igfitidea点击:

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

windowscommand-linebatch-file

提问by PeeHaa

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

我在尝试更改 .bat 文件中的代码页时遇到一个奇怪的问题。

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

当我在 Windows 7 中执行以下 .bat 文件时,它运行良好。

The codepage gets changed and program.exeget executed.

代码页被更改并被program.exe执行。

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.

但是,当我从 Windows Vista 启动 .bat 文件时,代码页被更改,然后退出批处理文件。

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

所以program.exe永远不会被执行。
但是,当我从命令行手动运行这两个命令时,它确实有效。

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

知道如何从 .bat 文件在 Windows Vista 下工作吗?

采纳答案by PeeHaa

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.
  • 我没有尝试创建批处理文件,而是创建了一个 .txt 文件(与批处理文件具有相同的内容)。
  • 我编写了一个非常简单的 C# 程序,它读取 .txt 文件并执行内容。

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

正如我所说,它很脏,但对我有用。

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

如果这里添加了其他答案,我也会尝试这些。

回答by jeb

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.

对我来说,这对 Win7 有效,这对我来说是新的,在 Vista 和 XP 中,如果代码页更改为 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

在您的情况下(使用德语变音),您可以更好地使用代码页 1252

chcp 1252
echo ??ü?

回答by refaim

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

您是否检查过chcp( chcp 65001 & echo %ERRORLEVEL%) 的返回码?

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

无论如何,尝试chcp 65001 & "D:\program.exe" /opt ?iü & chcp 850

回答by Mod

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.
  1. 将 dir 命令的输出定向到 txt 文件( dir c:\foldername > diroutput.txt )
  2. 在记事本中打开diroutput.txt,找到带有特殊字符的文件名并将其复制到剪贴板(看起来像垃圾,但没关系)
  3. 将文件名粘贴到在记事本中打开的批处理文件中。

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)

如果您在“dos”窗口中输入批处理文件内容(输入 batchfilename.bat ),您将看到文件名是正确的。(它在文件查看器中内置的 totalcommander 中看起来也正确,但在记事本中看起来很垃圾)