命令行FOR / F失败 [英] Command Line FOR /F Fails

查看:309
本文介绍了命令行FOR / F失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DOS构建脚本其中之一的Windows Server 2008 R2,但不是另一个上的作品。看到被损坏的机器进入或者在命令行下对症状:

I have a DOS build script which works on one Windows Server 2008 R2 but not another. To see the symptoms on the broken machine entering either of the following at the command line:

for /f %X in ('dir /b *.txt') do @echo %X
for /f "usebackq" %X in (`dir /b *.txt`) do @echo %X

给出:'目录/ b *表的.txt'不被识别为内部或外部的命令。而例如

gives: "'dir /b *.txt' is not recognised as an internal or external command." while e.g.

for %X in (*.txt) do @echo %X

正常工作,所以/ F不被正确地服从。我不相信这是命令扩展本身(CMD开始/ X显示了相同的行为;运行CMD他们/ Y里面的问题,服务器上给正常的/ F在这个时候意外)。我还检查了命令扩展注册表项,并在批处理文件试图SETLOCAL ENABLEEXTENSIONS。

works fine, so the /f is not being obeyed properly. I don't believe this is the Command Extensions themselves (starting cmd /x shows the same behaviour; running them inside cmd /y on the problem server gives the normal "/f was unexpected at this time"). I have also checked the command extensions registry keys and tried "setlocal enableextensions" in the batch files.

我不认为这是相关的,但服务器之间的区别是,没有一个是物理;它的CPU不具备VT扩展;并已安装了McAfee。据我所知,他们在不同的时间,虽然安装了同样的方式。

I don't think it's relevant but differences between the servers are that the failing one is physical; its CPU doesn't have VT extensions; does have McAfee installed. As far as I know they were installed the same way though at different times.

有没有人有什么建议?我坚持!

Does anyone have any suggestions? I am stuck!

推荐答案

检查 COMSPEC 环境变量在机器上它不工作,即做回声%COMSPEC%,看看它包含了(应该是%WINDIR%\\ SYSTEM32 \\ CMD.EXE 或类似)。

Check the COMSPEC environment variable in on the machine where it doesn't work, i.e. do echo %COMSPEC% and see what it contains (it should be %windir%\system32\cmd.exe or comparable).

长篇:

您要详细具体的问题排除了一切其他潜在的可能性(如需要使用 %% X 而不是%X 批处理文件里面,相比于命令行),喜欢与 SETLOCAL ENABLEEXTENSIONS摆弄(或类似的开关,注册表项等)。顺便说一句,该错误信息将不适合。

You're detailed question ruled out all other potential possibilities (like the need to use %%X instead of %X inside batch files, as compared to the command line), like fiddling with setlocal enableextensions (or comparable switches, registry entries, etc.). And by the way, the error message would not fit.

如果您收到错误消息...不被识别为一个内部或外部命令,它是,CMD.EXE不能找到你想执行的命令。由于dir是一个内部命令这绝不应该发生,当然。

If you get the error message "...is not recognised as an internal or external command" it is, that CMD.EXE cannot find the command you're trying to execute. Since "dir" is an internal command "this should never happen", of course.

我能够重现你的错误执行以下操作:

I was able to reproduce your error doing the following:


  1. 开始 CMD.EXE

  2. 输入以下 SET COMSPEC = DoesNotExist

  3. 输入以下 CMD.EXE ,即启动另一个嵌套, CMD.EXE 会话。这一步是必须的,在运行中的 CMD.EXE 会议上,更改为 COMSPEC 似乎被忽视。

  4. 在新的 CMD.EXE 会话中输入您的命令(例如 FOR / F在('DIR / B')%×@echo做%X ),你应该让你看到的错误。请注意,如果你只是输入 DIR 它仍然可以工作,所以你必须有通过该间接的执行,例如,一个循环。滑稽。

  1. Start CMD.EXE
  2. Enter the following SET ComSpec=DoesNotExist
  3. Enter the following CMD.EXE, i.e. start another, nested, CMD.EXE session. This step is required, in a running CMD.EXE session, the change to ComSpec seems to go unnoticed.
  4. In the new CMD.EXE session enter your command (e.g. for /F %x in ('dir /b') do @echo%x), you should get the error you see. Note if you just enter dir it will still work, so you have to have that "indirect" execution via, e.g., a for loop. Funny.

请注意,这是所有做是为了重现您所看到的,理由苛求,导致你的系统中这种行为可能会有所不同的环境或设置条件,然而事实是, COMSPEC 环境变量是指除 CMD.EXE 应该是相同的。

Note that this was all done to reproduce what you are seeing, the reasons exact environmental or setup conditions that lead to this behavior on your system might be different, however the fact that the ComSpec environment variable refers to something other than CMD.EXE should be the same.

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

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