Command Line FOR /F Fails

后端 未结 2 895
猫巷女王i
猫巷女王i 2021-01-23 02:49

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 li

2条回答
  •  囚心锁ツ
    2021-01-23 03:33

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

    Long Story:

    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.

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

    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.

提交回复
热议问题