Making a Batch not close when a error happens

February 15, 2011 at 17:51:21
Specs: Windows 7, 4GB RAM.
Just like the title says.
How would I go about making it so when soemthing bad happens. (IE: ***** was unexpected at this time) it wont just up and close or stop?
Could i make it say..When a error happens: goto errorhappened or something?
Or maybe just: Echo ERROR *** HAPPENED & pause & continue
(Though I know Continue isnt a command, you get the idea I think)

I heard about error level.
But I guess I dont understand it?
How would I go about making this idea work?


See More: Making a Batch not close when a error happens

Report •


#1
February 15, 2011 at 18:01:13
If it's for testing purposes, just drag the batch file on to cmd.exe.

Errorlevels are the value that is returned. 0 is typically returned when no error occurs, and 1 or higher indicates an error.

Ex.

C:\Users\Matt>echo %errorlevel%
0

C:\Users\Matt>type "123.txt"
The system cannot find the file specified

C:\Users\Matt>echo %errorlevel%
1

C:\Users\Matt>

Can I put a question here?


Report •

#2
February 15, 2011 at 18:37:55
Well, its not 'just' for testing. Different computers have different components and sometimes different commands. Since id like to be able to give my .bat file to a few people when im done with it, id like to be able to see what goes wrong if anything does on their computer on the fly without having to boot up cmd and wait for the error to happen again.

Your explanation of errorlevel is alot nicer then the other ones ive seen lol, thanks. Kinda seems useless in batch files though since batch files crash when a error pops up.
Nice to know though. Thanks for the help! Though I still want help with my original question. lol


Report •

#3
February 15, 2011 at 19:40:39
You could make the script invoke itself with "cmd /k". That way you will just be taken a prompt if something happens.

With this method I suggest always starting a new window because the only way to close it is "EXIT" (with no switches), this will also kill any initial prompts.

@echo off
if not "%~1"=="__reinvoked" (
    start cmd /k "%~f0" __reinvoked %*
    exit /b
)
shift
setlocal

if ==l kdf

pause
exit

Kinda seems useless in batch files though since batch files crash when a error pops up.

What is called an "errorlevel" in batch is just a return code. It can be quite useful, just not in the case of an internal command that causes a crash.


Report •
Related Solutions


Ask Question