2
votes

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>
3
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
XP SP3. It shouldn't close the cmd prompt in any version of windows, but it does on this machine.barlop
Looks like the problem is not unknown in these quarters: stackoverflow.com/questions/261296/…Andriy M

3 Answers

4
votes

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.

0
votes

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

0
votes

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.