I know when I start a bat file and have pause at the end it will stay open.
Then when pressing the spacebar the bat file ends, and the command prompt will be closed.
Also I know when we use cmd /k it will stay opened, and there is even a command prompt left to enter some bat file code.
Without pause or cmd /k there will be a new window opened and it closes itself.
What I really want to is to have a console window opened, and every time I run a bat file I want the output to be seen on the cmd that is already opened.
I like the way that I can see all the code that was running, without to having close the windows every time, and like it as if all the code will be seen in one opened cmd prompt.
edit: 2
first_file.bat
#echo first
call "C:\ProgramData\Cool\second_file.bat"
#echo thirth
second_file.bat
#echo second
Related
Is it possible to close the Batch File, like "press any key"?
Because I have made a Batch file, that renames a file. And when I Launch the .Bat file, The File changes name to the desired name. And when I "press any key" I made it so the File changes name back to the normal name.
However When i press the (X) Button, to close the batch. It does not follow the last line of code. To rename the file Back. Only works when I "press any key".
What I think you're trying to say is that you want the file to change its name so that, when you press any key or exit out of the program it goes back to the original name, right?
I do not think this is possible with batch as when you terminate the process, (command prompt in our case) it cannot execute a command as it is closing. What you could do is make the command prompt launch another command prompt on start and make that command prompt wait until the 1st one is closed and then rename the file back and close itself. This is not that practical though. If you are realy desperate however, try this code:
#echo off
start "CommandPrompt" cmd.exe /WAIT /K ren OriginalFileName.txt NewFileName.txt & pause>nul & ren NewFileName.txt OriginalFileName.txt
ren NewFileName.txt OriginalFileName.txt
exit
While it does essentialy do what you want it to, it relies on the 2 command windows to be closed in order. It is however probably the least complex way out there. It probably goes without saying that you need to replace OriginalFileName.txt and NewFileName.txt with whatever names you need.
I have a bat file that I would like to open another bat file minimized from the desktop. I tried doing this:
#echo off
start /min "%USERPROFILE%\Desktop\coolkids.bat"
exit
But it just opened up a blank command prompt, even though "coolkids.bat" has commands in it.
I have a windows 10 PC
The first quoted argument of start is taken to be the caption of the new window. Try the following instead, and check start /? for the full syntax.
start /min "" "%USERPROFILE%\Desktop\coolkids.bat"
I know that the question was not very clear, so I will try to make it clearer.
I am looking for the batch script command to open a cmd window that runs like a batch program.
I know the command exists as I have seen it used before and have used it before, however, as of late I have not been
able to find it or remember it. The command looks something like this
#echo off
start cmd.exe ("#echo off && echo second window opened && pause")
pause
It would open a second cmd window that read.
second window opened
press any key to continue...
And when you pressed a key the second window would close, just like a batch file cmd window would. As you probably can tell I am relatively new to batch scripts and am still a little iffy on how it works.
Not bad memory. Almost done
start "title" cmd /c "echo in other window & echo. & pause"
Type cmd /? and start /? to get all the needed information for this commands usage
I'm trying to write batch script to create a folder if it does not already exist.
Following up the online examples, below is my script.
The problem is; first pause works, then probably due to syntax error the window closes even before reaches to the second pause, so I can't really tell which part of my script is wrong.
Could anyone show me how to prevent closing window so that I can see what's on the window?
#echo off
:copy theme images over
:designer
echo copying theme images over...
pause
if not exist "%K2DIR%\K2 SmartForms Runtime\Styles\Themes\Sharepoint 2013\rich_text"
(
md "%K2DIR%\K2 SmartForms Runtime\Styles\Themes\Sharepoint 2013\rich_text333"
)
pause
You could put this line at the beginning of the batch file:
if not defined in_subprocess (cmd /k set in_subprocess=y ^& %0 %*) & exit )
What this line does is, the first time you run it, it re-launches itself in a subprocess that doesn't exit after it finishes running the batch file.
You need to pass the /K switch to CMD, or just open a Command Window and run the batch from the command line.
Press start and type cmd and press enter, you will launch a command prompt.
Just drag and drop what you need to run (your python script, .exe ...) into the cmd windows, and press enter.
(You might some time to run the cmd as admin: find the cmd in the start menu, right-click on it, choose run as admin).
I recorded the screen (bandicam) for when I couldn't quite read the error message, and then I could replay it; I suppose this is mainly helpful if you already have software on your computer.
using pause at end of batch paused the cmd screen, tanks!
How to prevent batch window from closing when error occurs?
I had the problem when using robocopy. My solution was:
if not %errorlevel% lss 8 pause
For Robocopy every exit code below 8 is a success:
https://ss64.com/nt/robocopy-exit.html
With batch, if you get an error, the most you see of it is a flash of text and then the program ends. Is there anyway to have it slow down? or to have it stop before closing when it hits an error?
Thanks
If you execute your Batch file from the command-line in a MS-DOS window and an error happens, you can just review the text in the window to see what happened.
On the other hand, if you execute the Batch file via a double click in the explorer you see nothing if the Batch file have an error. Is this your case? If so, the easiest solution is to test the Batch file in a MS-DOS window until it works ok.
However, if you still need a method to stop closing the DOS window when the Batch file ends, you can do that this way:
Right click on your Batch file and select Create shorcut, a Shorcut is created.
Right click on the Shortcut and select Properties
In Target, after the "C:\Path\filename.bat" string add: & PAUSE
Select OK
This way, when you execute the Shortcut via a double click, the DOS window will execute a PAUSE after the Batch file ends for any reason.
Redirect the output with > to capture it in a file.
You might need: command > file 2>&1
try this :
if NOT ["%errorlevel%"]==["0"] (
pause
exit /b %errorlevel%
)
Run the script from a present CMD.exe and add "exit /b 1" to the scripts end of file. Remove any simple "exit".
Open a new cmd window and execute your command there. The newly opened window will not be closed when an error occurs.
start cmd /k [command]
This works for me with basic commands. Not sure if it's useful for anything more advanced.
To stop a batch script before it ends, put the pause command on a new line, which will make the script wait for user input (like an enter key) before continuing (or closing).
for a second
PING -n 2 127.0.0.1 > NUL 2>&1
or for 10secs
timeout /t 10 /nobreak
This works for me. Similar to #Sri7's answer but you need the brackets and quotes:
if NOT ERRORLEVEL 0 (
pause
)