12

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

8 Answers 8

15

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.

6
  • Thats a good point. By running it via the MS-DOS window then will be like Calling the Bat file. Thanks! Oct 24, 2011 at 7:15
  • @GarethJones: Excuse me. If you think my answer is useful, may I ask for your upvote? Thanks! BTW, did you see my answer in your "5 seconds timer in set /p" question? I think my answer may enrich a lot your Batch game!
    – Aacini
    Oct 25, 2011 at 4:37
  • I tried to, but cause the wireless down my end of the house sucks so much, it bounced back with an error. Also i did see your answer, thanks, it was a great help, as i didn't know about external programs like that were available for batch Oct 25, 2011 at 5:34
  • This was very useful, batch file worked when ran from the command prompt but not when clicked (due to relative path) Nov 24, 2014 at 17:25
  • This is not a fully useful answer. Some batch scripts launch another cmd.exe, which means that none of this is relevant. May 27, 2015 at 19:49
4

Redirect the output with > to capture it in a file.

You might need: command > file 2>&1

2
  • 4
    Why don't you try it and see?
    – bmargulies
    Oct 22, 2011 at 21:42
  • For the benefit of others who find this post: The second suggestion will include error messages in the output file on Windows 7 (and I assume older). If you leave off the 2>&1 part, errors go to the console, which can also be useful.
    – Ben Wyatt
    Oct 25, 2012 at 20:33
4

try this :

if NOT ["%errorlevel%"]==["0"] (
    pause
    exit /b %errorlevel%
)
1
  • 1
    This works! I think this should be the accepted answer.
    – Len
    May 19, 2017 at 1:15
3

Run the script from a present CMD.exe and add "exit /b 1" to the scripts end of file. Remove any simple "exit".

1
  • exit /b 1 means "exit this batch script with error code 1". It does not mean "exit this batch script only if an error code has been set", which is what the question is asking for.
    – Len
    May 19, 2017 at 1:14
3

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.

1
  • you can also pipe commands like this: start cmd /k "command 1 & command 2 & command 3"
    – Koen
    Feb 22, 2017 at 9:07
2

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).

1
  • 1
    That wont work because if the batch file hits an error, it will crash, and so wont hit the end of the script, nor any pauses. Oct 22, 2011 at 21:23
0

for a second

PING -n 2 127.0.0.1 > NUL 2>&1

or for 10secs

timeout /t 10 /nobreak
0

This works for me. Similar to @Sri7's answer but you need the brackets and quotes:

if NOT ERRORLEVEL 0 (
   pause
)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.