17

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.

RGuggisberg
  • 307
  • 1
  • 8
  • 2
    Unfortunately I cannot help. But I have to complement you on providing a well written question that precisely describes your issue, I wish more first time posters took the time to write a well written question like this. Of course I cannot reproduce the behavior on my Win 10 machine. And since you only see the behavior on one specific machine, it may be difficult for anyone to diagnose what is going on. – dbenham Apr 09 '16 at 03:56
  • Thanks @dbenham. I respect your knowledge and the wisdom you dispense on SO under the batch-file tag. So I am a first time poster here on Super User but I know what you mean about new posters. Yes, I agree it is not likely that someone can duplicate... just hoping maybe someone else may have encountered this. It's just a test VM so it is not critical. Just would like to know what it is in case it ever happened on a customer's machine. – RGuggisberg Apr 09 '16 at 12:53
  • 1
    @RGuggisberg - Be sure you merge your accounts so you can edit your own questions. You don't have to add indications you edited your question, the system does that for you, completely unnecessary. – Ramhound Apr 11 '16 at 13:43
  • @Ramhound, Thanks, sounds like good advice. Where do I merge accounts? I don't see anything like that on the Accounts tab and a search of this site was not helpful. – RGuggisberg Apr 11 '16 at 13:53
  • [How can one link/merge/combine/associate two accounts/users? (Anonymous/unregistered/cookie or OpenID/registered)](http://meta.stackexchange.com/questions/18232/how-can-one-link-merge-combine-associate-two-accounts-users-anonymous-unregist) – Ramhound Apr 11 '16 at 13:55
  • Unable to reproduce on any of my machines. – Moshe Katz Apr 12 '16 at 01:00
  • Thanks to all that have tried this. I really don't expect that it can be duplicated on a typical machine. I suspect that there is something wrong or misconfigured on this test machine. I could just build a new one, but it has piqued my interest. Just wondering if anyone has ever encountered similar behavior. – RGuggisberg Apr 12 '16 at 14:48
  • Please paste `wmic process …` command in remaining `cmd` window after last `Press any key to continue . . .` to repeat it again. (Results lost in your update.) – JosefZ Apr 12 '16 at 21:59
  • The CMD window is 'dead'. Still visible, but no menu pops up when I right click in it. I can click on the title bar, select Edit and Paste, but nothing gets pasted. I did find out a bit more by looking at Task Manager while doing this. Will update my question to reflect those findings. – RGuggisberg Apr 13 '16 at 13:20
  • if you close your CMD windows then consolehost should close, can you check the settings of your CMD window? are they the same as the other setups you have? consolehost has to do with windows letting CMD use AERO and THEMES, maybe theres something in this setup that is not working as it should regarding AERO or you have somehow changed default settings for CMD – arana Apr 13 '16 at 16:34
  • @RGuggisberg you should have gotten *two* emails to merge your accounts, you need to click the link in **both** those emails in order to merge your accounts. As it stands your accounts have not been merged. – Mokubai Apr 13 '16 at 18:02
  • @arana Yes, if I close CMD window by 'X'ing out of it the consolehost goes away too. The consolehost is not going away when CMD finishes as explained above. Not sure which 'settings' you are referring to. Properties including Options, Font, Layout, and Color are same as other machines. It's not my machine, so I have no idea what may have been done to it. – RGuggisberg Apr 13 '16 at 18:54
  • Does this happen if you boot in Safe mode? – harrymc Apr 19 '16 at 16:02
  • Don't know about Safe Mode.... it is a VM that I only have access to via LogMeIn – RGuggisberg Apr 19 '16 at 18:14
  • Unable to reproduce.. In order to aid debugging: 1) Does it also occure when clicking other processes instead of Explorer? 2) If you start your batch file from another cmd window, and click that after starting your batch file, does the issue also occure? – Master-Guy Apr 21 '16 at 05:57
  • 1) Yes, the same thing happens when clicking on any other process (not limited to Explorer). Explorer is just were we noticed it. 2.a) If I start batch file by running it from another CMD window it always works correctly (ends normally). 2.b) If I start batch file from another CMD Window using START cmd it ends normally... of course CMD window remains as it should. – RGuggisberg Apr 21 '16 at 14:43
  • 1
    I also had the same issue on one of my VM, as a workaround I have explicitly mentioned `exit` command to close the CMD window. May be this might help. – manjesh23 Apr 22 '16 at 09:37
  • I think - like asked by harrymc - you should check how it goes in safe mode. Make sure text **encoding** is correct. This can result in strange things. And: What virtualization are you using? Are extensions installed on the guest system? – Clijsters Apr 22 '16 at 12:34
  • @manjesh23, exit does not help. I agree Safe Mode would be good to try but I don't have access to the virtual machine host and can only access machine via logmein. Hyper-V environment. Where to check text encoding? – RGuggisberg Apr 22 '16 at 13:27
  • And why access to guest only isn't enough for safe mode? – Clijsters Apr 22 '16 at 13:30
  • Are QuickEdit or Insert options enabled? You may check this in Properties or Defaults of CMD windows menu. – Euri Pinhollow Apr 22 '16 at 13:40
  • Will this help you? http://stackoverflow.com/questions/14697739/how-to-automatically-close-cmd-window-after-batch-file-execution – manjesh23 Apr 22 '16 at 13:58
  • Quick Edit was not enabled. Insert was enabled, but disabling it didn't matter. – RGuggisberg Apr 22 '16 at 14:21
  • 1
    Have you tried the Analyze Wait Chain feature of Resource Monitor to determine on which application the ConHost.exe process is waiting? – Xtremity Apr 22 '16 at 19:38
  • Yes... it says ConHost is running normally. – RGuggisberg Apr 22 '16 at 20:19

2 Answers2

1

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.

clhy
  • 6,283
  • 8
  • 34
  • 66
Argonauts
  • 4,410
  • 19
  • 23
0

Have you tried this?

@echo off
rem some code
pause
cls
exit

Same things, but allows same as pause and exits by itself.

Also, why are you putting the @ in front of everything? Why not just use @echo off before the rest of the commands?

Heptite
  • 19,383
  • 5
  • 58
  • 71
  • Yes.. already tried that. Agree on the @ preface. Typically that is what I do. Does not matter for this. Probably had that in there for some initial testing to see commands. – RGuggisberg May 16 '16 at 18:42