Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Keep CMD Window Open After BAT Runs  (Read 416 times)

0 Members and 1 Guest are viewing this topic.

rjbinney

    Topic Starter


    Apprentice
  • Disarmingly Good-looking
  • Experience: Familiar
  • OS: Other
Keep CMD Window Open After BAT Runs
« 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
Dan: You're gonna need to get someone to fix my computer.                     Kim: What's wrong with it?                     Dan: It's in several pieces on my floor.

Squashman



    Specialist
  • Thanked: 119
  • Experience: Experienced
  • OS: Other
Re: Keep CMD Window Open After BAT Runs
« Reply #1 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.

rjbinney

    Topic Starter


    Apprentice
  • Disarmingly Good-looking
  • Experience: Familiar
  • OS: Other
Re: Keep CMD Window Open After BAT Runs
« Reply #2 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.

Dan: You're gonna need to get someone to fix my computer.                     Kim: What's wrong with it?                     Dan: It's in several pieces on my floor.

Squashman



    Specialist
  • Thanked: 119
  • Experience: Experienced
  • OS: Other
Re: Keep CMD Window Open After BAT Runs
« Reply #3 on: March 09, 2018, 09:05:21 PM »
When you execute cmd.exe that is a normal console window. 

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1070
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 8
Re: Keep CMD Window Open After BAT Runs
« Reply #4 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!)
I was trying to dereference Null Pointers before it was cool.

rjbinney

    Topic Starter


    Apprentice
  • Disarmingly Good-looking
  • Experience: Familiar
  • OS: Other
Re: Keep CMD Window Open After BAT Runs
« Reply #5 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
Dan: You're gonna need to get someone to fix my computer.                     Kim: What's wrong with it?                     Dan: It's in several pieces on my floor.

Salmon Trout



    Genius

    Thanked: 914
    • Yes
  • Computer: Specs
  • Experience: Experienced
  • OS: Other
Re: Keep CMD Window Open After BAT Runs
« Reply #6 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.

Völlig losgelöst   
von der Erde   
schwebt das Raumschiff   
völlig schwerelos.

Salmon Trout



    Genius

    Thanked: 914
    • Yes
  • Computer: Specs
  • Experience: Experienced
  • OS: Other
Re: Keep CMD Window Open After BAT Runs
« Reply #7 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?
Völlig losgelöst   
von der Erde   
schwebt das Raumschiff   
völlig schwerelos.

Salmon Trout



    Genius

    Thanked: 914
    • Yes
  • Computer: Specs
  • Experience: Experienced
  • OS: Other
Re: Keep CMD Window Open After BAT Runs
« Reply #8 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?
« Last Edit: March 10, 2018, 01:46:36 AM by Salmon Trout »
Völlig losgelöst   
von der Erde   
schwebt das Raumschiff   
völlig schwerelos.