Getting better, but dont handle errors well

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
drdumont
Posts: 4
Joined: 09 Sep 2011 16:10

Getting better, but dont handle errors well

#1 Post by drdumont » 11 Sep 2011 16:53

Getting a little better at this. My batch will open the CD drawer, I put in the disk, it closes the door, reads the directory, and sends it to a txt file.
Buuuut -
What if there is an error, such as no cd, unreadable cd, etc? Right now the program merely will report an error to the screen, and goes to the next step,
messing up my file count and a couple of pointers. Is there a way to cause the batch to halt or pause or do to an error handler in case of an error?

Thanks and enjoy the few remaining hours in the weekend!
Yours for Better Television,
-- Doc

nitt
Posts: 218
Joined: 22 Apr 2011 02:43

Re: Getting better, but dont handle errors well

#2 Post by nitt » 11 Sep 2011 17:52

drdumont wrote:Getting a little better at this. My batch will open the CD drawer, I put in the disk, it closes the door, reads the directory, and sends it to a txt file.
Buuuut -
What if there is an error, such as no cd, unreadable cd, etc? Right now the program merely will report an error to the screen, and goes to the next step,
messing up my file count and a couple of pointers. Is there a way to cause the batch to halt or pause or do to an error handler in case of an error?

Thanks and enjoy the few remaining hours in the weekend!
Yours for Better Television,
-- Doc


I can't guarantee this, but try using the OR "||" syntax.

http://www.youtube.com/user/khoraski?feature=mhee#p/u/10/CJ2Uwh4ojCU

It goes kinda like this:

DOCOMMAND || FAILED

First, it tries to execute "DOCOMMAND", and if the command returns any kind of error, then it executes "FAILED".

And example of this your be:

Code: Select all

@echo off
ping google.com > nul || echo Error: Could not connect to the internet!


As you can see, I am using the command "ping" with the parameters "google.com" (the ' > nul' part just disables the output). But if you are not connected to the internet, then it will return an error and execute "echo Error: Could not connect to the internet!".

There is also a:

DOCOMMAND && SUCCESS

This will only execute the second command if the first was successfull. So it's kinda like the opposite of "||".

Code: Select all

@echo off
ping google.com > nul && (echo You are connected to the internet!) || (echo You are not connected to the internet!)
pause


Usually AND "&&" and OR "||" are good for error handling.

Post Reply