2

When I open cmd.exe and type that first line and push ENTER, it works fine. When I input that other line and push ENTER, it closes immediately.

I can change ('a') to ('asdfgh') then same problem. or %q to %f same problem.

The difference is the /f, for some reason the FOR /F with ('......') causes cmd.exe to close.

This happens on one of my systems and not another

C:\>for %q in ('a') do echo %q   <ENTER>

C:\>echo 'a'
'a'

C:\>for /f %q in ('a') do echo %q <ENTER>
barlop
  • 12,887
  • 8
  • 80
  • 109
  • What version of Windows? It works for me in Windows 7 HP SP1. I mean, it doesn't terminate the CMD session but prints the error about `a` being a wrong command. – Andriy M Aug 30 '11 at 05:19
  • XP SP3. It shouldn't close the cmd prompt in any version of windows, but it does on this machine. – barlop Aug 30 '11 at 05:23
  • Looks like the problem is not unknown in these quarters: http://stackoverflow.com/questions/261296/batch-closes-prematurely-on-a-for-f-command – Andriy M Aug 30 '11 at 05:31

3 Answers3

4

This guy seems to have solved a very similar problem successfully:

for /f closes cmd window immediately

The problem in that case turned out to have to do with the COMSPEC environment variable, it being incorrectly set initially, as it seems. The guy applied logging out of the Windows account and back in.

Andriy M
  • 76,112
  • 17
  • 94
  • 154
  • COMSPEC did get listed when doing SET 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. – barlop Aug 30 '11 at 06:11
  • +1 That was really a brilliant find. I wasn't expecting anybody to be able to answer it. – barlop Aug 30 '11 at 08:14
  • That question you mentioned in your comment to my question, has 2200 views, and nobody got it. You should get a medal! – barlop Aug 30 '11 at 08:18
  • Oddly the title for that thread on ss64.org forum , apart from one char, is identical character for character to mine! His was more accurate though for not including a question mark(the one char difference), I think i'll remove my question mark! – barlop Aug 30 '11 at 10:59
  • @barlop: It is fascinating how both you and Google think that 'prompt' and 'window' are the same word. :) I googled for `"for /f" closes command prompt`, and in the results the word `window` was highlighted in bold just as well as `prompt`. Other than that, I agree that the coincidence you've mentioned is really striking. – Andriy M Aug 30 '11 at 11:23
  • oh, actually i said cmd prompt he said cmd window. – barlop Aug 31 '11 at 21:13
0

Add a paranthesis "(", like this:

for %q in ('a') do (

It will prompt you with a "More?" in the following lines until you decide to close your for block with a ")"

Arun
  • 2,493
  • 15
  • 12
  • C:\>for /f %f in ('echo abc') do ( More? echo hello More? echo hello More? ) <-- so i've hit ) then I hit ENTER and the cmd prompt suddenly closes. – barlop Aug 30 '11 at 01:36
0

Looking at the help for 'FOR' ('FOR /?'), you can see that the '/F' option is for parsing file input and has the following options:

FOR /F ["options"] %variable IN (file-set) DO command [command-parameters]
FOR /F ["options"] %variable IN ("string") DO command [command-parameters]
FOR /F ["options"] %variable IN ('command') DO command [command-parameters]

Note that last entry... a single-quoted value is treated as a command to run, and the output of that command is what 'FOR' is parsing.

Perhaps you have an 'a' command/executable on one machine, but not on the other?

You should only be using the '/F' flag if you specifically want the behavior that '/F' supplies.

JaredReisinger
  • 6,955
  • 1
  • 22
  • 21
  • I said I tried changing a to asdf, same problem. In those cases it should say no such program/"'adsf' is not recognized as an internal or external command". It still shouldn't close. This line also causes the cmd prompt to close C:\>for /f %f in ('echo asdf') do set ttt=%f This is a problem on one machine and not another. – barlop Aug 30 '11 at 01:35
  • Fair enough... and it looks like the actual problem (mis-set COMSPEC) was a real doozy. Nonetheless, using--or not using--'/F' changes the semantic meaning of the 'FOR'. You shouldn't expect to be able to interchange them. (Although you *should* be able to expect it not to crash. :-) ) – JaredReisinger Aug 31 '11 at 20:29
  • I know what /F does. My example was to demonstrate the problem, by showing a symptom, with the advantage of it being a simple example. I didn't say comspec was mis-set, that looks like something Andriy M said most probably wrongly since his source didn't say it was mis-set. It was just as I said. It may well have had the correct value when it came up from SET, i'd bet it did, but it needed to be set in the environment variables window / that place in the registry. – barlop Aug 31 '11 at 21:04