批量pmaturely关闭$ P $在FOR / F命令 [英] Batch closes prematurely on a for /f command

查看:201
本文介绍了批量pmaturely关闭$ P $在FOR / F命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个批处理文件(在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.EXE

I 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 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.

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) 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

这篇关于批量pmaturely关闭$ P $在FOR / F命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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