gpt4 book ai didi

batch-file - 批处理在 for/f 命令上过早关闭

转载 作者:行者123 更新时间:2023-12-02 07:40:09 26 4
gpt4 key购买 nike

我有一个批处理文件(在 Windows XP 中,激活了命令扩展),其中包含以下行:

for /f %%s in ('type version.txt') do set VERSION=%%s

在某些计算机上,它工作得很好(如this SO question所示),但在其他计算机上它会杀死cmd(控制台窗口刚刚关闭)

为什么?

<小时/>

注意:计算机似乎具有类似的配置:XpSP2,用户具有管理权限,HKEY_CURRENT_USER\Software\Microsoft\Command Processor 中没有定义“命令处理器”...

最佳答案

我得到了第一个经验答案:

for /f %%s in (version.txt) do ...

在每台计算机上都可以正常工作。

似乎 for/f 适用于文件名,而不适用于任何 dos 命令,如“type filename”。

但是,对于我所有客户的计算机而言,情况并非如此(在某些计算机上,“类型文件名”工作正常)

如果你想要 15 分(简单?);-),你可以留下问题的答案:
为什么“for/f”有时不能与文件名以外的任何内容一起使用。为什么它只是关闭 DOS session ?

<小时/>

编辑:3年后(!),barlop面临类似的情况,详细信息请参阅问题“for /f closes cmd prompt immediately?”。他的结论是:

COMSPEC did get listed when doing SET+ENTER.
So, I opened the environment variables window, and saw COMSPEC was not listed under user or system variables. I added it to System Variables, started a command prompt, and it seems to work fine.

这个thread on ss64 forum ,由 Andriy M 提及在他对巴洛普问题的回答中包含了详细信息。

The shelling out in the "for" loop to complete 'dir' (or whatever command you've asked to complete) requires ComSpec to be set in order to reload the cmd window.

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\ComSpec=
%SystemRoot%\system32\cmd.exe

关于batch-file - 批处理在 for/f 命令上过早关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/261296/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com