tags:

views:

1791

answers:

7

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

+2  A: 

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 ?

VonC
Maybe offering 15 points does not cut the mustard? :-) Honestly, I failed to reproduce the behavior you mentioned. Can you post some more details?
Tomalak
Thank you, Tomalak, for having taken the time to check that issue out. The thing is, I have not much details beside the one laready mentionned: on one client's PC, on a DOS session, I type a "for /f %s in ('type afilename.txt') do echo %s" and... "poof" no more DOS windows...
VonC
I will pursue this, but I wanted to check with the SO community if that problem had already be seen before. Apparently not. I will post any conclusion I may find.
VonC
I do not have any precise answer from our own support team, so I "accept" my own answer for now (no reputation gain whatsoever)
VonC
A: 

Is it that the file doesn't have an extension and thus the cmd treats it like a directory that doesn't exist?

Keng
Actually, the file does exist and has an extension (filename was just a foobar name) : "for /f %s in ('type afilename.txt') do echo %s" triggers the immediate closing of the DOS windows
VonC
A: 

If command extensions are disabled, the (set) parameter to the for command must be a file.

If command extensions are enabled, the (set) parameter to the for command may be a file, a string, or a command.

Command extensions are disabled on the client's computer where the 'type filename' set fails.

For infomation on enabling or disabling command extensions, type "cmd /?"

Craig Lebakken
He mentioned command extensions being activated (consistently, I guess).
Tomalak
Yes, but from Craig's remark, I will check again that. To be sure, Craig, could you precise the criteria validating the fact command extension is or is not enable ?
VonC
Darned... I just checked and re-check the registry, command extensions are indeed enabled, and no other special values stand out. The windows event does not display anything out of the ordinary. cmd /E:ON still closes itself on a for /f in ('command') command-line... frustrating ;)
VonC
If I am not mistaken you there is the possibility CMD.EXE is called with /E:OFF so that command extensions are disabled. I suggest you run these in the DOS shell: VERIFY errors 2>nul SETLOCAL ENABLEEXTENSIONS IF ERRORLEVEL 1 echo Unable to enable extensions
Philibert Perusse
A: 

Wait...could it be that you have the apostrophes around the file name? MS is saying that filenamesets get double-quotes and literal strings get apostrophes.

for /F ["usebackqParsingKeywords"] {%% | %}variable in ("filenameset") do command [CommandLineOptions]

for /F ["usebackqParsingKeywords"] {%% | %}variable in ('LiteralString') do command [CommandLineOptions]

for /F ["usebackqParsingKeywords"] {%% | %}variable in (`command`) do command [CommandLineOptions]

Unless of course you're doing this.

You can use the for /F parsing logic on an immediate string, by wrapping the filenameset between the parentheses in single quotation marks (that is, 'filenameset'). Filenameset is treated as a single line of input from a file, and then it is parsed.

Keng
Thank you Keng for your suggestion. I will check that out tomorrow at work.
VonC
Argh... nope: the last form (`command`) will close immediately the DOS windows
VonC
A: 

Regarding the command extensions, there is the possibility CMD.EXE is called with /E:OFF so that command extensions are disabled (even though enabled in the registry).

If using command extensions shell options in a script, it is HIGHLY suggested that you do the following trick at the beginning of your scripts.

-- Information pasted from http://www.ss64.com/nt/setlocal.html

SETLOCAL will set an ERRORLEVEL if given an argument. It will be zero if one of the two valid arguments is given and one otherwise.

You can use this in a batch file to determine if command extensions are available, using the following technique:

VERIFY errors 2>nul
SETLOCAL ENABLEEXTENSIONS
IF ERRORLEVEL 1 echo Unable to enable extensions

This works because "VERIFY errors" sets ERRORLEVEL to 1 and then the SETLOCAL will fail to reset the ERRORLEVEL value if extensions are not available (e.g. if the script is running under command.com)

If Command Extensions are permanently disabled then SETLOCAL ENABLEEXTENSIONS will not restore them.

Philibert Perusse
Thank you for this detailed answer... still no luck. That DOS windows still closes itself, even though the ENABLEEXTENSIONS is activated successfully...
VonC
A: 

Hi I have a problem. In my usb flas i have two folder but i can't see in explorer but i can see them in dos. Can somebody help me?

Hi Nikola. I believe someone can help you. Post this as a new question on http://superuser.com, and read *first* about how those sites run ( http://meta.stackoverflow.com/questions/tagged/faq+superuser and http://meta.stackoverflow.com/questions/7931/how-does-stackoverflow-work-the-official-faq )
VonC
A: 

I'm having the same problem with one of our office machines (an XP). Any command in for /F will kill the dos window, like the following one:

for /F "tokens=1*" %A in ('dir') do echo 123

Same thing if you replace dir with any other command. This happens with Command extension enabled.

By the way, when the command is run with command extension is disabled, dos prints an error message (/f is unexpected) and does not exit.

I'm wondering how common this problem is.

Wayne
That does describe the problem indeed. I didn't see it reproduced *that much*, so I hope it is not too common;)
VonC
Just found out something interesting. Given the window-killing 'for' command example above, it works perfectly fine in a nested command window. That is, if I run cmd.exe inside the command window, and run the for command from the inner one, it does what it is supposed to do. Can you verify if this trick works with your machines?
Wayne