Computer Hope

Microsoft => Microsoft DOS => Topic started by: rjbinney on March 09, 2018, 10:35:02 AM

Title: Keep CMD Window Open After BAT Runs
Post by: rjbinney on March 09, 2018, 10:35:02 AM
Basic question, I know - but I can never get CH's "Search" to find if things have been answered before...

I have a simple .BAT file. I'd like the CMD window to be open and active (i.e., ready for another command, even if it's just "Exit", after it runs).

If I use
Code: [Select]
pauseof course, it keeps the window open until I tap any key, then it closes.

If I use
Code: [Select]
cmd /k before my first command, the window stays open, but is essentially "dead" and won't allow for more commands... I have to manually close the window. Same as if I put just simply
Code: [Select]
cmd as my last line.

My Googling skills are not up to this task. I keep getting the same answers no matter how I ask. I just want a command prompt after the end of my batch file!

Thanks
Title: Re: Keep CMD Window Open After BAT Runs
Post by: Squashman on March 09, 2018, 10:40:00 AM
Why wouldn't you put CMD /K at the end of your batch file?  You said you put it before your first command.
Title: Re: Keep CMD Window Open After BAT Runs
Post by: rjbinney on March 09, 2018, 10:54:29 AM
Sorry, should have mentioned I tried that too. Same results.

I get a blinking underscore, but it's not a command prompt. The only command I type that works is "exit".

What I'd like is for it to be able to then treat the window as a "normal" CMD window once the BAT runs.

Title: Re: Keep CMD Window Open After BAT Runs
Post by: Squashman on March 09, 2018, 09:05:21 PM
When you execute cmd.exe that is a normal console window. 
Title: Re: Keep CMD Window Open After BAT Runs
Post by: BC_Programmer on March 09, 2018, 09:19:19 PM
cmd /k works for me at the end of a test batch file. After the batch file completes I get a standard command prompt:

Code: [Select]
@echo off
echo doing important work. Please standby
ping 127.0.0.1 -n 6 > nul
echo Critical tasks completed.
pause
cmd /k
(The ping obviously is just to make it pretend to do something!)
Title: Re: Keep CMD Window Open After BAT Runs
Post by: rjbinney on March 09, 2018, 11:13:48 PM
When you execute cmd.exe that is a normal console window.
A "normal" console window is some sort of c:\ prompt.... Yes?


cmd /k works for me at the end of a test batch file. After the batch file completes I get a standard command prompt:
When I run your file, I get a c:\ prompt. Hm. Will start from scratch tomorrow and re-report....

#Headscratcher
Title: Re: Keep CMD Window Open After BAT Runs
Post by: Salmon Trout on March 10, 2018, 01:21:21 AM
cmd /k works for me at the end of a test batch file. After the batch file completes I get a standard command prompt:
You can see the window title change when you respond to the Pause command and the batch exits.

Title: Re: Keep CMD Window Open After BAT Runs
Post by: Salmon Trout on March 10, 2018, 01:23:24 AM
A "normal" console window is some sort of c:\ prompt.... Yes?

When I run your file, I get a c:\ prompt. Hm. Will start from scratch tomorrow and re-report....
Isn't that what you are asking for?
Title: Re: Keep CMD Window Open After BAT Runs
Post by: Salmon Trout on March 10, 2018, 01:32:31 AM
I can use BC's script above, with cmd /k at the end, and after I press a key (because of the Pause command) I see the command prompt and can enter a command e.g. Dir, echo %date%, whatever. I can also make a version of that script, but with cmd /k omitted from the end, and call it from the prompt, like this cmd /k batchfile.bat and the same thing happens. This also causes a return of the prompt if the script has an Exit command at the end.

Rjbinney, are you starting your "simple .BAT file" by double clicking it in Windows Explorer, or by typing its name at the command prompt?