Windows – CMD window will not close if folder is closed during execution of bat file

batch filewindows server 2012

I have a Server 2012 R2 Standard 64 Bit virtual machine that is acting strangely. Bat files work fine and the CMD window closes normally; but if the folder containing the bat file is closed during execution of the bat file it will finish but the CMD window does not close. I have simplified this to the point of having the simplest possible bat file. It contains 1 line.
pause

  1. Open the folder containing the bat file with Windows Explorer
  2. Start the bat file by double clicking on it
  3. Close the Windows Explorer window
  4. Press any key in CMD window
  5. Cursor moves to next line as bat file is finished
  6. CMD window remains

This only happens on one machine. Does anybody know why this might be? It is a test VM and maybe should just be rebuilt, but it has me curious. Thanks

I replaced my simple bat file with one containing the following:

@assoc .bat
@for /F "tokens=1,* delims==" %%G in ('assoc .bat') do @ftype %%H
@reg query "HKLM\Software\Microsoft\Command Processor" /v AutoRun 2>NUL
@reg query "HKCU\Software\Microsoft\Command Processor" /v AutoRun 2>NUL
@wmic process where "caption = 'cmd.exe'" get CommandLine, ParentProcessId,     ProcessID
@echo please close Windows Explorer before continuing
@pause
@wmic process where "caption = 'cmd.exe'" get CommandLine, ParentProcessId,     ProcessID
@pause

Results are shown below:

.bat=batfile
batfile="%1" %*



CommandLine                                      ParentProcessId  ProcessId
C:\Windows\system32\cmd.exe /c ""D:\test.bat" "  1608             8460

please close Windows Explorer before continuing
Press any key to continue . . .
CommandLine                                      ParentProcessId  ProcessId
C:\Windows\system32\cmd.exe /c ""D:\test.bat" "  1608             8460

Press any key to continue . . .

CommandLine

04/13/2016
I did find out a bit more by looking at Task Manager while doing this. When I start the bat file 2 lines appear in the Task Manager – Details window… 1 for cmd.exe and 1 for conhost.exe. When the bat finishes cmd.exe goes away, but conhost.exe remains. One other thing I noticed is that you don't really have to exit the Windows Explorer folder as I previously stated… all you have to do is click anywhere in the Explorer window. If I don't click anywhere in the Explorer window the CMD window for the bat file closes normally and both tasks disappear from the details window as we would expect.

Best Answer

This is unlikely but you seem to have ruled out all the likely things. Check the following two registry entries to see if they contain anything:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor

or

HKEY_CURRENT_USER\SOFTWARE\Microsoft\Command Processor

Anything in there will get called each time you run a batch file, and if it has cmd /k that would cause this behavior.