批量pmaturely关闭$ P $在FOR / F命令 [英] Batch closes prematurely on a for /f command
问题描述
我有一个批处理文件(在Windows XP中,使用命令延长激活)与以下行:
FOR / F %% S IN('型version.txt)并设置Version = %%小号
在一些电脑,它工作得很好(由<一所示href=\"http://stackoverflow.com/questions/130116/dos-batch-commands-to-read-first-line-from-text-file\">this SO质疑),但在其他的它杀死CMD (控制台窗口关闭刚)
为什么?
请注意:计算机似乎有类似的配置:XPSP2,用户拥有管理权,没有命令处理器,在HKEY_CURRENT_USER \\软件\\微软\\命令处理器定义...
我得到了第一经验性的答案:
FOR / F %% S IN(version.txt)做...
工作得很好,每一台计算机上。
这似乎 FOR / F
与文件名的作品,不与任何DOS命令,如类型的文件名。
然而,这不是为我的客户的所有计算机真(上一些人来说,类型的文件名正常工作)
如果你想15(?易)点;-),你可以离开了问题的答案:结果
为什么FOR / F有时不与任何其他不是文件名的工作。以及为什么它只是关闭DOS会话?
编辑:(!)3年后, barlop 面临着类似的情况,在这个问题的详细的 FOR / F
关闭马上的命令提示符?一>。他的结论是:
COMSPEC
没有得到执行SET上市时
+ <大骨节病> ENTER 骨节病>。结果
于是,我打开环境变量窗口,看见COMSPEC
下的用户或系统变量中没有列出。我把它添加到系统变量,启动命令提示符,它似乎很好地工作。
块引用>在ss64论坛,通过的螺纹:/在回答barlop的问题/stackoverflow.com/users/297408/andriy-m\">Andriy中号,包含的细节。
在炮轰了
为
循环完成DIR
(或任何命令你已经要求完成)要求COMSPEC
,以重新加载CMD
窗口中进行设置。
块引用>HKEY_LOCAL_MACHINE \\系统\\ CurrentControlSet \\控制\\会话管理\\环境\\ COMSPEC =
%SYSTEMROOT%\\ SYSTEM32 \\ CMD.EXEI have a batch file (in windows XP, with command extension activated) with the following line:
for /f %%s in ('type version.txt') do set VERSION=%%s
On some computer, it works just fine (as illustrated by this SO question), but on other it kills cmd (the console window just closes)
Why ?
Note: the computers seem to have a similar configuration: XpSP2, the user has administrative right, no 'Command processor" defined in HKEY_CURRENT_USER\Software\Microsoft\Command Processor...
解决方案I got a first empiric answer:
for /f %%s in (version.txt) do ...
works just fine, on every computer.
It seems
for /f
works with a filename, not with any dos command like 'type filename'.However, it is not true for all my client's computer (on some, the 'type filename' works fine)
If you want 15 (easy ?) points ;-), you can leave an answer to the question:
why 'for /f' sometime does not work with anything else than a file name. And why it just closes the DOS session ?
Edit: 3 years later(!), barlop faced a similar situation, detailed in the question "
for /f
closes cmd prompt immediately?". His conclusion was:
COMSPEC
did get listed when doingSET
+ENTER.
So, I opened the environment variables window, and sawCOMSPEC
was not listed under user or system variables. I added it to System Variables, started a command prompt, and it seems to work fine.This thread on ss64 forum, mentioned by Andriy M in his answer to barlop's question, contains the details.
The shelling out in the "
for
" loop to complete 'dir
' (or whatever command you've asked to complete) requiresComSpec
to be set in order to reload thecmd
window.
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\ComSpec= %SystemRoot%\system32\cmd.exe
这篇关于批量pmaturely关闭$ P $在FOR / F命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!