如何修复Windows批处理文件FOR命令重现“活动代码页:65001"; [英] How to fix Windows batch file FOR command returing "Active code page: 65001"

查看:705
本文介绍了如何修复Windows批处理文件FOR命令重现“活动代码页:65001";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我正确理解此批处理脚本

If I understand correctly this batch script

for /f "usebackq tokens=*" %%i in (`time /t`) do (
  echo "%%i"
)
echo "done"

应该只打印时间,然后完成"

should just print the time and then "done"

虽然可以在我的计算机上打印Windows 10 Pro x64

Instead though on my machine, Windows 10 Pro x64 it prints

>test.bat
>for /F "usebackq tokens=*" %i in (`time /t`) do (echo "%i" )
>(echo "Active code page: 65001" )
"Active code page: 65001"
>(echo "17:48" )
"17:48"
>echo "done"
"done"

为什么这样做以及如何解决它,所以它只是花费时间,而不是此活动代码页:65001"的结果.这破坏了构建脚本,因为在FOR命令中使用什么命令都无关紧要,第一行始终是活动代码页:65001"

Why is it doing this and how do I fix it so it just gets the time and not this "Active code page: 65001" result. This is breaking build scripts as it doesn't matter what command goes in the FOR command the first line is always "Active code page: 65001"

为清楚起见,我正在寻找操作系统设置修复程序,而不是批处理文件修复程序.我要运行的真正批处理文件来自开源项目中的构建脚本,由于这个问题,它们失败了.

To be clear I'm looking for an OS setting fix, not a fix to the batch file. The real batch files I'm trying to run are from build scripts in open source projects and they are failing because of this issue.

推荐答案

当我们运行命令并获得正确的输出时,但它之前是另一个命令的输出,则此意外命令会以某种方式在我们的预期命令之前运行.

When we run a command and we get the correct output, but it is preceded by the output of another command, somehow this unexpected command is run before our intended command.

通常可疑的是使用与我们正在调用的命令相同的名称创建的批处理文件,但在这种情况下,作为内部命令time,情况并非如此,内部命令的优先级高于外部命令(默认行为).

The usual suspect is a batch file created with the same name that the command we are calling, but in this case, being time an internal command, this can not be the case, internal commands have precedence over external commands (default behaviour).

如果time是内部命令,那么如何调用另一个命令?

If time is an internal command, how can another command being called?

for /f需要处理命令的输出时,将创建一个单独的cmd实例来执行该命令.在此单独实例中生成的输出由for /f命令的do子句中的代码处理.

When for /f needs to process the output of a command a separate cmd instance is created to execute that command. The output generated in this separate instance is processed by the code in the do clause of the for /f command.

此单独实例的创建可能是意外输出的来源.

The creation of this separate instance can be the source of the unexpected output.

阅读cmd /?所示的帮助信息,我们可以看到有两个注册表项

Reading the help information shown by cmd /? we can see that there are two registry keys

HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\AutoRun
HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun

可以配置为在每次创建新的cmd实例时运行命令(除非启动cmd实例时使用/D开关).

that can be configured to run commands every time a new cmd instance is created (unless the /D switch is used when starting the cmd instance).

这是最可能找到获得Active code page: 65001输出原因的地方.

This is the most probable place where to find why you get the Active code page: 65001 output.

这篇关于如何修复Windows批处理文件FOR命令重现“活动代码页:65001";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆