<p>I have a batch file (in windows XP, with command extension activated) with the following line:</p>

<pre><code>for /f %%s in ('type version.txt') do set VERSION=%%s
</code></pre>

<p>On some computer, it works just fine (as illustrated by <a href="https://stackoverflow.com/questions/130116/dos-batch-commands-to-read-first-line-from-text-file">this SO question</a>), but on other <strong>it kills cmd</strong> (the console window just closes)</p>

<p>Why ?</p>

<hr>

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

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 ?
Edit: 3 years later(!),
barlop
faced a similar situation, detailed in the question "
```
for /f
```
closes cmd prompt immediately?
". His conclusion was:
```
COMSPEC
```
did get listed when doing
```
SET
```
+
ENTER
.
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.
This
thread on ss64 forum
, mentioned by
Andriy M
in his answer to barlop's question, contains the details.
The shelling out in the "
```
for
```
" loop to complete '
```
dir
```
' (or whatever command you've asked to complete) requires
```
ComSpec
```
to be set in order to reload the
```
cmd
```
window.
```
```
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\ComSpec=
%SystemRoot%\system32\cmd.exe
```
```