I have a bat file like this:
ipconfig
That will print out the IP info to the screen, but before the user can read that info CMD closes itself.
I believe that CMD assumes the script has finished, so it closes.
How do I keep CMD open after the script is finished?
Put pause at the end of your .BAT file.
Depending on how you are running the command, you can put /k after cmd to keep the window open.
cmd /k my_script.bat
Simply adding cmd /k to the end of your batch file will work too. Credit to Luigi D'Amico who posted about this in the comments below.
Just add #pause at the end.
Example:
#echo off
ipconfig
#pause
Or you can also use:
cmd /k ipconfig
When the .bat file is started not from within the command line (e.g. double-clicking).
echo The echoed text
#pause
echo The echoed text
pause
echo The echoed text
cmd /k
echo The echoed text & pause
Adding pause in (Windows 7) to the end did not work for me
but adding the cmd /k in front of my command did work.
Example :
cmd /k gradlew cleanEclipse
start cmd /k did the magic for me. I actually used it for preparing cordova phonegap app it runs the command, shows the result and waits for the user to close it. Below is the simple example
start cmd /k echo Hello, World!
What I did use in my case
start cmd /k cordova prepare
Update
You could even have a title for this by using
start "My Title" echo Hello, World!
If you are starting the script within the command line, then add exit /b to keep CMD opened
In Windows add '& Pause' to the end of your command in the file.
I was also confused as to why we're adding a cmd at the beginning and I was wondering if I had to open the command prompt first.
What you need to do is type the full command along with cmd /k. For example assume your batch file name is "my_command.bat" which runs the command javac my_code.java then the code in your batch file should be:
cmd /k javac my_code.java
So basically there is no need to open command prompt at the current folder and type the above command but you can save this code directly in your batch file and execute it directly.
javac -d C:\xxx\lib\ -classpath C:\xxx\lib\ *.java
cmd cd C:\xxx\yourbat.bat
the second command make your cmd window not be closed.
The important thing is you still able to input new command
As a sidenote this also works when running a command directly from the search bar in windows.
e.g. directly running ipconfig will directly close the cmd window after the command has exited.
Using cmd \k <command> won't - which was what i was trying to do when i found this answer.
It has the added advantage of always recognizing the command you're trying to run. E.g. running echo hello world from the searchbar won't work because that is not a command, however cmd \k echo hello world works just fine.
Related
what I'm essentially trying to do is make a batch file (or shortcut if possible) to change to a directory, echo a message, and then let you use the cmd window as if you had opened it up normally after.
Example:
The batch file/shortcut opens a cmd prompt in C:\test-folder\
Echos "This is your message"
Awaits user input for commands
I've tried the following command:
start cmd /k cd /d C:\test-folder\
echo test
The folder change will work without the echo line, but if I include the echo line, it will not work at all.
Is there any way to do what I'm trying to accomplish here?
the order of the switches matters. See start /? and cmd /? for details.
start "My window" /d "d:\" cmd /k "echo Hello&echo use me"
"My window" belongs to start and gives the new window a title.
/d "c:\test-Folder\" also belongs to start and gives the startdirectory
cmd is the command to start
/k belongs to cmd and has to be cmd's (only or) last Switch
"echo Hello&echo use me" is the commandline to execute.
Your problem is the echo test is run in the parent window that issues the START command. You want the ECHO to occur in the window that START launches.
One option is to append the ECHO command to the end of the START command, remembering to escape the & so that it is included as part of the CMD /K option.
start cmd /k cd /d C:\test-folder\ ^& echo test
Or you could put quotes around the entire /K option (CMD will remove the quotes before executing the string):
start cmd /k "cd /d C:\test-folder\ & echo test"
Or you could use Stephan's suggestion to let START set your active directory, so that your CMD /K option only needs to ECHO the message.
start /d "C:\test-folder" cmd /k echo test
Or you can create a shortcut and edit the properties such that
"Target" = C:\Windows\System32\cmd.exe /k echo test
and
"Start in" = c:\test-folder
If you create a batch file that ends with cmd /k, it will run the commands before cmd /k and then leave you in a command prompt. This is useful if you're performing enough work that makes a one-liner shortcut hard to write.
To get the behaviour you described, you can create a file called "open_test_folder.cmd" with the following:
#echo off
cd C:\test-folder
echo This is your message
cmd /k
This might be very basic but I cannot find the answer in the internet.
I have a cmd/bat file with 3 basics lines to set the working directory as the one of the current folder.Once I run it, I get the CMD window, and I type specific commands (example : "start notepad").
%~d1
cd "%~p1"
call cmd
What should I write within the cmd. or bat. file so the "start notepad" will be already launched as command?
Thank you very much
There are a couple of ways you can achieve this.
You can open notepad directly with the start command and then run cmd, like this:
#echo off
%~d1
cd "%~p1"
start "" notepad
call cmd
You can also include the notepad start command directly in the cmd call, like this:
#echo off
%~d1
cd "%~p1"
call cmd /k start "" notepad
Note that there is a "" after start because start considers the first set of quotes that it encounters to be the window's title.
Try this:
#echo off
Command.Com
When you'll open this batch file it will open up CMD where you can start typing commands like start notepad etc...I think this was what you was looking for?
i want to write some codes in cmd by batch file
{CreateObject("WScript.Shell").Run "%comspec% /c start /wait cmd.exe", 0, True }
this code opens cmd.exe ..
How can I edit this file with my own dos commands ?
i dont want use it in txt file?
my question is to write some command lines to cmd.exe
not to txt file by "echo > & echo >> "
i need to open cmd and start write instructions with a batch file
and how i get some information from the cmd and place it in another script to use it later
Wscript.shell.run uses shell32 to start files. You ask it to specifically start cmd.exe (%comspec%), which you ask that cmd.exe to ask shell32 (the start command) to start cmd.exe. Put the commands you want to run in a batch file (that's a text file).
Wscript.shell.run "c:\folder\batchfile.bat"
or if you prefer
Wscript.shell.run "cmd /c c:\folder\batchfile.bat"
or if only one command
Wscript.shell.run "cmd /k dir"
see
cmd /?
I wanted to start cmd, then start write code which was saved in batch file
The code will be like this:
{ START cmd.exe /k "netsh wlan show profiles" }
This code will open cmd and show you the saved profiles
I need to run script minimized and save it's output. I used following line but saved file is empty:
start /MIN script.bat > file.txt
How to start script minimized and save it's output to file?
You are outputting the results of start to file.txt. The file is empty because start /MIN doesn't produce any output in the current console. What you want is to have the file redirection as part of the started command. Adding quotes fixes this.
Also, when I tried this, it created a new minimized command prompt which ran script.bat and then waited for further commands. I assume this probably wasn't your intent. To make the new console close after script.bat exits, pass the /c parameter to cmd.exe
start /MIN %comspec% /c "script.bat > file.txt"
I want to write a batch file that will do following things in given order:
Open cmd
Run cmd command cd c:\Program files\IIS Express
Run cmd command iisexpress /path:"C:\FormsAdmin.Site" /port:8088 /clr:v2.0
Open Internet Explorer 8 with URL= http://localhost:8088/default.aspx
Note: The cmd window should not be closed after executing the commands.
I tried start cmd.exe /k "cd\ & cd ProgramFiles\IIS Express", but it is not solving my purpose.
So, make an actual batch file: open up notepad, type the commands you want to run, and save as a .bat file. Then double click the .bat file to run it.
Try something like this for a start:
c:\
cd c:\Program files\IIS Express
start iisexpress /path:"C:\FormsAdmin.Site" /port:8088 /clr:v2.0
start http://localhost:8088/default.aspx
pause
I think the correct syntax is:
cmd /k "cd c:\<folder name>"
This fixes some issues with Blorgbeard's answer (but is untested):
#echo off
cd /d "c:\Program files\IIS Express"
start "" iisexpress /path:"C:\FormsAdmin.Site" /port:8088 /clr:v2.0
timeout 10
start http://localhost:8088/default.aspx
pause
cmd /c "command" syntax works well. Also, if you want to include an executable that contains a space in the path, you will need two sets of quotes.
cmd /c ""path to executable""
and if your executable needs a file input with a space in the path a another set
cmd /c ""path to executable" -f "path to file""
#echo off
title Command Executer
color 1b
echo Command Executer by: YourNameHere
echo #################################
: execute
echo Please Type A Command Here:
set /p cmd=Command:
%cmd%
goto execute
start cmd /k "your cmd command1"
start cmd /k "your cmd command2"
It works in Windows server2012 while I use these command in one batch file.
cmd /k cd c:\
is the right answer
I was trying to run a couple of batch files parallely at startup, if a condition was true.
For this I made a parent batch file which should have checked for the condition and invoke the other child batch files if the condition was true.
I tried to achieve it via START but it gave me an empty black command prompt running in the directory of children batch files, instead of running the children batch files themselves
The thing which worked for me was by using a combination of START and CALL
As an example
condition ...
start call "C:\Users\Amd\conn\wsl_setup - conn1.bat"
start call "C:\Users\Amd\conn\wsl_setup - conn2.bat"
start call "C:\Users\Amd\conn\wsl_setup - conn3.bat"
I know DOS and cmd prompt DOES NOT LIKE spaces in folder names. Your code starts with
cd c:\Program files\IIS Express
and it's trying to go to c:\Program in stead of C:\"Program Files"
Change the folder name and *.exe name. Hope this helps