for /f closes cmd window immediately

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

for /f closes cmd window immediately

Post by MigrationUser »

17 Oct 2008 14:16
joiners

Has anyone encountered a problem when running a bat file that the cmd window closes immediately when the shelling out process occurs. This is a single machine problem in my environment...all others are fine...same bat files being used.

for /f "tokens=1" %i in ('hostname') do set HOST=%i

I believe its related to windows and the process used to shell out to run the command and return the arguments to the for loop for further processing. For some reason I have one particular client which shows this behaviour. All others are running the same bat file without issue.

Its a machine specific problem.
I've logged into the same machine with other accounts and the same closure occurs.
The same user logs into another machine and successfully runs the bat file.

Using something other than a command which returns arguements works fine - like echo 123123
I've tried using usebackq with same results
I've replaced cmd.exe with one from another machine..same results
I've compare registry settings under HKLM and HKCU software>microsoft>command processor - all the same

Any ideas would be appreciated.

----------------------------

#2 17 Oct 2008 15:35
joiners


behaves as I explained when opening a single command shell
starting a second shell by running cmd.exe from the first command window results in successful

----------------------------

#3 17 Oct 2008 16:33
bluesxman


NB - Well if you're running "for /f "tokens=1" %i in ('hostname') do set HOST=%i" from within a batch file, it won't work at all -- you'd need to replace all instances of "%" with "%%".

What's the hostname of the box? Perhaps it's got some characters in that are offending the "for" command.

What happens if you open a CMD window and run the script from within that? Hopefully you're see a helpful error message...

cmd | *sh | ruby | chef

----------------------------

#4 17 Oct 2008 16:56
joiners


The command I've posted is what I running interactively on the command line after identifying the problem line in the batch file. The bat file has %%i where required..and works for all other clients for years now.

The hostname of the machine in question is simple alphanumeric chars..nothing strange. There are no errors before the cmd window closes.

However, as I mentioed in my additional reply, starting a second cmd session within the first, strangely results in a successful evaluation of hostname and returns the hostname properly the remainder of the do loop. The second cmd session is not closed. I used processexplorer to watch these processes and confirm.

its strange. I suspected registry, or cmd.exe problems...none of that is the problem after checking.

----------------------------

#5 17 Oct 2008 20:31
avery_larry
joiners wrote:

The command I've posted is what I running interactively on the command line after identifying the problem line in the batch file. The bat file has %%i where required..and works for all other clients for years now.

The hostname of the machine in question is simple alphanumeric chars..nothing strange. There are no errors before the cmd window closes.

However, as I mentioed in my additional reply, starting a second cmd session within the first, strangely results in a successful evaluation of hostname and returns the hostname properly the remainder of the do loop. The second cmd session is not closed. I used processexplorer to watch these processes and confirm.

its strange. I suspected registry, or cmd.exe problems...none of that is the problem after checking.
command.exe instead of cmd.exe?
environment problems with autorun etc.
What happens with this:

Code: Select all

for /f "tokens=1" %i in ('hostname') do echo set HOST=%i
something with command extensions?

Maybe try

Code: Select all

start for /f "tokens=1" %i in ('hostname') do set HOST=%i
to see if there's an error message.

----------------------------

#6 17 Oct 2008 20:50
joiners


yes that works...as does starting another cmd shell within the first cmd. I have this open with microsoft development support also. It appears to be a buffer overflow from a debugging session with technical support. They will look into how cmd.exe inits the buffer space on a new session. Using processmonitor, we captured the process activity for cmd.ex and see that its throwning a thread_exit as soon as I submit the request. I'll post the results.

----------------------------

#7 17 Oct 2008 21:05
Simon Sheppard


I'm wondering if there could be two programs on the path called 'hostname'?

perhaps try with the full pathname

'C:\windows\sytem32\hostname.exe'

----------------------------

#8 17 Oct 2008 21:20
joiners


I tried that as well...same results. It does not matter what application goes between quotes...I've used things like dir or echo...with same results.

----------------------------

#9 17 Oct 2008 23:23
joiners


Solved.
The problem is that the env var ComSpec MUST be defined on the machine. 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

Logout of windows account and backin, and all works fine.
thanks for the comments and suggestions. Hopefully this helps others with the same.
Post Reply