How to prevent batch window from closing when error occurs? - batch-file

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

Related

How to create a batch script that restarts a executable after it is closed?

I want to create a batch file that can detect an executable is running or not and if not, it should start it immediately after the executable is closed.
I tried wait command but it doesn't seems to work as I wanted, like when I type:
#echo off
:Restart
start "SY" /wait "C:\Program Files (x86)\SY.exe"
goto Restart
It starts my executable program and not let it close, but when the executable is minimized and restored, then it closes very easily when close button is clicked.
Then it shows a message in the command window: ^CTerminate batch job (Y/N)?
But nothing like that happens when I try the same batch code for notepad executable:
#echo off
:Restart
start "Edit Text" /wait "%windir%\Notepad.exe"
goto Restart
The notepad window never close, even by minimizing and restoring then clicking close, it does not close.
So I want the results for my executable to be the same as the results for the notepad executable. Any help would be greatly appreciated.... Thanks in advance!!!!
you can check if your .EXE is running with takslist. If it isn't running (||) then just start it. Put a loop around and you're done:
:loop
tasklist |findstr /ibc:"SY.EXE" || "C:\Program Files (x86)\SY.exe"
timeout 1
goto :loop

Running files in the current window with batch

This is pretty hard to explain so I'll do my best.
When I use the start command in Batch, It'll create a new window etc.
I'm trying to make my own console batch script that isn't as annoying as cmd.exe.
Problem is running commands like help, echo and so on in the current window.
My current code is
:console
set COMMAND=n
set /p COMMAND=%~dp0console.exe^> %=%
if %COMMAND%==exit exit
start %COMMAND%
rem Wait 1000
goto console
I'm using it with Batch to EXE Converter so rem Wait 1000 is valid.
The big problem with the script is that if a user types help or echo and any other commands you can run normally in cmd.exe, it'll open a new cmd.exe window and run the script. I do not want this to happen, is there a way to solve the problem?
Things I've tried:
Instead of using start %COMMAND% I just used %COMMAND%. This
resulted in an error.
(Will add on this list when things that don't work are suggested)
There are two options:
call %command%
Which will wait until the command is over
start /b %command%
Which will start the command in the current window

How to open a .bat file from another in a certain situation

So I am using the following code:
:begin
SET /P runscript= [Question Here]
if %runscript%==:100 goto run blahblah.bat
if %runscript%==EXIT goto :A
pause
I am trying to make there be an option to open another .bat file in a different window, but when I answer :100, command prompt just shuts down. I am trying to be as clear as possible as to what I am trying to do, but this is just a snip of a very big project I am working on.
Try start cmd /k to run the bat in new window. For goto, notice no colon before A. Lastly, your set command as written will include the space. I closed it here.
:begin
SET /P runscript=[Question Here]
if %runscript%==:100 start cmd /k blahblah.bat
if %runscript%==EXIT goto A
pause
if %runscript%==:100 goto run blahblah.bat
will attempt to find the label run on entry of :100
If you want to transfer execution to the batch blahblah.bat then remove the goto run

What is the batch script to open another cmd window to run another line of script

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

Slowing a batch file down to read errors

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
)

Resources