# Solved: .bat file help



## andythepandy (Jul 2, 2006)

Hi, I have a .bat file which runs some commands. Sometimes, if the user has entered incorrect details, the .bat file commands will throw errors. The user will not know about these because the command prompt flashes up and goes in a second. 
Is there a way to catch these errors in the .bat file?

Thanks,
Andy


----------



## Squashman (Apr 4, 2003)

Post the batch file here. You need to put in checks to see if the data they entered is valid if possible.

You can also redirect all standard error output to a file.


----------



## andythepandy (Jul 2, 2006)

Thanks for reply,

The .bat file is written by an excel spreadsheet, the contents is:
msg /server:COMPUTERNAME USERNAME MESSAGE

the COMPUTERNAME, USERNAME and MESSAGE are replaced by values the user enters in the spreadsheet, for example:
msg /server:home_computer administrator "Hi there!"

Once written, the .bat file is executed.

If the user enters, say in the COMPUTERNAME field, a computer that doesn't exist, the msg.exe will throw error but the user wont know.

Thanks,
Andy


----------



## Squashman (Apr 4, 2003)

Why not just put a pause at the end of the batch file so that the person can see the error output. Granted they will have to acknowledge the Press Any Key to continue to prompt but at least they will know it failed.


----------



## andythepandy (Jul 2, 2006)

yeah i tried this but i want to draw all attention away from the command prompt and to be able to catch errors and make my own vba msgbox saying what the problem is.

Andy


----------



## Squashman (Apr 4, 2003)

Why not check that the computername exists by trying to ping it before you do the msg command. If the ping comes back with an error then display an error on the screen.


----------



## andythepandy (Jul 2, 2006)

But how does vba know if theres been an error in the ping?

Andy


----------



## Squashman (Apr 4, 2003)

Actually you could just check the errorlevel in the batch file. If the command doesn't complete the errorlevel will not be equal to Zero. If the error level does not equal zero you could throw up a VB msgbox stating that message did not send. I don't know VB so don't ask me how to do that.


```
H:\>ping yahoo.com

Pinging yahoo.com [206.190.60.37] with 32 bytes of data:

Reply from 206.190.60.37: bytes=32 time=33ms TTL=51
Reply from 206.190.60.37: bytes=32 time=33ms TTL=51
Reply from 206.190.60.37: bytes=32 time=32ms TTL=51
Reply from 206.190.60.37: bytes=32 time=33ms TTL=51

Ping statistics for 206.190.60.37:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 32ms, Maximum = 33ms, Average = 32ms

H:\>echo %errorlevel%
0

H:\>ping computername
Ping request could not find host computername. Please check the name and try again.

H:\>echo %errorlevel%
1

H:\>
```


----------



## Squashman (Apr 4, 2003)

andythepandy said:


> But how does vba know if theres been an error in the ping?
> 
> Andy


In the batch file do an if statement to check the errorlevel and if it does not equal zero call your VBscript to display an error message.


----------



## andythepandy (Jul 2, 2006)

thanks for your help, i am redirecting the output of the msg command to a txt file then creating error from that.

Thanks for the help,
Andy


----------



## Squashman (Apr 4, 2003)

Why don't you just write this whole thing in VB. I am sure it can be done. This way you don't have to screw around with Excel, Batch file and VB.


----------



## andythepandy (Jul 2, 2006)

That would be the best thing to do, but i'm just one of those people who has to make things as complicated as possible!!

Thanks,
Andy


----------



## colcar07 (Jan 7, 2009)

hey


----------

