# Solved: batch file - show if error



## danniy (Oct 26, 2007)

Hello

I am new in making batch files. I only write VBA excel and access code.

I made a simple file that contains a copy command.


```
copy "\\...........SQ_TEMP\DealExport1_v1.xlsm" "C:\Program Files\Microsoft Office\Office12\XLSTART"
```
As you see this bat just copies the xlsm file from a sharepoint to a local folder. It works fine, but if the xlsm file is in use, or the user has no access to the sharepoint it doesn't work. Thats fine by me, but i just want to see when it is not working.

What i want is a simple code to add to this copy line that will tell the user when the file cannot be copied.
Is that possible?

Thank you.
danniy


----------



## Ent (Apr 11, 2009)

Have a look into the IF command, particularly IF Errorlevel.


----------



## danniy (Oct 26, 2007)

i tried but i got lost in all the numbers. lots of error levels and i have no idea how to write the if in a batch file.

for example in vba it's pretty simple:


```
If "condition" Then
"do something"
Else
"do something else"
End if
```
what i need here is something like:

if error "copy............."
show error message.
End if

something like that. I have no idea what level or how should i put this into code.

i got something like:

copy....
if ErrorLevel 1 echo error

this works but i get to see the error message for 1 sec and window closes


----------



## Ent (Apr 11, 2009)

If you're dealing with batch, you have to get out of the mindset which VBA puts you in.

Try the following line AFTER the copy command 

if errorlevel 1 echo Something didn't work there. Please check that you have access to the file and it is not in use.


----------



## danniy (Oct 26, 2007)

tried that. window closes immediately. cannot see the message.
can i get a textbox or something?


----------



## Ent (Apr 11, 2009)

There are, to the best of my knowledge, no text boxes in batch. If you place the command "pause" at the end of your script it should give you time to see the message.


----------



## danniy (Oct 26, 2007)

ok, did it. thank you so much for your help, and time.

Danniy


----------



## Ent (Apr 11, 2009)

You're welcome. I remember trying batch for the first time after using the Basic family. I still call it a duct tape & chewing gum language; effective and invaluable for what it is but incredibly annoying when you want a bit more discipline in a language. Good luck.

Ent


----------



## danniy (Oct 26, 2007)

well, i approve but im just using it to copy a file from a sharepoint. so no big deal here


----------



## Squashman (Apr 4, 2003)

Ent said:


> You're welcome. I remember trying batch for the first time after using the Basic family. I still call it a duct tape & chewing gum language; effective and invaluable for what it is but incredibly annoying when you want a bit more discipline in a language. Good luck.
> 
> Ent


Then you should try PowerShell!


----------



## Squashman (Apr 4, 2003)

danniy said:


> i tried but i got lost in all the numbers. lots of error levels


If you would have Google searched for "batch errorlevel" the first link Google would have found was this.
http://www.robvanderwoude.com/errorlevel.php

And you would have read this in the 2nd paragraph


> Most programmers agree that an errorlevel 0 means the command executed successfully, and an errorlevel 1 or higher usually spells trouble.


In my experience with batch, in your example you should really check to see if the errorlevel is NOT zero.

The link also provides plenty of examples of IF statements.


----------



## danniy (Oct 26, 2007)

thank you very much.


----------

