Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm trying to write a script that automates copying some files from two external locations using Robocopy. I want the script to skip a step if one of the locations is unavailable. I'm using the following script:

@echo off

NET USE \\192.168.10.90\IPC$

ROBOCOPY \\192.168.10.90\lgrdata\ \ICOS\Analyser_backups\LGR_Flux\ /NP /TEE /E /dcopy:T /Z /LOG:C:\logfiles\LGR\robocopyjob_log.txt

NET USE \\192.168.10.90\IPC$ /D



NET USE \\192.168.10.91\IPC$

ROBOCOPY \\192.168.10.91\lgrdata\ \ICOS\Analyser_backups\LGR_Profile\ /NP /TEE /E /dcopy:T /Z /LOG+:C:\logfiles\LGR\robocopyjob_log.txt

NET USE \\192.168.10.91\IPC$ /D


robocopy C:\ICOS\Analyser_backups\ Z:\Analyser_backups\ /NP /TEE /E /dcopy:T /Z /LOG+:C:\logfiles\LGR\robocopyjob_log.txt


for /f "tokens=2-8 delims=.:/ " %%a in ("%date% %time: =0%") do rename "C:\logfiles\LGR\robocopyjob_log.txt" rclog_%%c-%%a-%%b_%%d%%e.txt

This works fine, when both network locations are available. I want the script to continue if either location is not present. I get error 53 when one instrument is not present, and then there is a 30 second wait, and then the connection is retried. This continues forever.

I tried using IF ERRORLEVEL 53 GOTO

If I put this after the NET USE command, it seems to be ignored. If I place it at the beginning of the script, the script closes without doing anything.

Any ideas how I can skip the step if the instrument is not present? Perhaps there is some way of using PING to identify if it is there first?

share|improve this question
    
Please could you tidy up your script so it is more obvious where you want the help, so the answer can be better. As in please take out the rem lines that are not relevant to your question. I had to guess at what you wanted to skip over. –  David Apr 15 at 11:57
    
Sure, I was in a hurry, didn't realise how sloppy it was. Apologies –  Robert Holden Apr 15 at 15:15

2 Answers 2

up vote 1 down vote accepted

Try something like this:

ping -n 1 192.168.10.91 > NUL
IF ERRORLEVEL 0 (goto :copyhost1) ELSE goto :skipcopyhost1
:copyhost1
NET USE \\192.168.10.91\IPC$
ROBOCOPY \192.168.10.91\lgrdata\ \ICOS\Analyser_backups\LGR_Profile\ /NP /TEE /E /dcopy:T /Z /LOG+:C:\logfiles\LGR\robocopyjob_log.txt
:skipcopyhost1
share|improve this answer
    
I use the ping/errorlevel method myself for file sharing batching too! I find it helpful also. –  Wally Apr 15 at 15:30
    
Hi David, Thanks for your help. This method works too, and I actually prefer it as the ping command and the error message are logged in the log file. With the method suggested by LP chip, nothing about the step that is skipped is logged. I just copied your script above and it works perfectly. Thanks again. –  Robert Holden Apr 15 at 16:09
    
At the end of the script I copy files to an external disk. Interestingly here, the script doesn't get stuck if the disc is missing, it just recognises it is missing, logs it in the log file, and continues the script to the end. I guess that's because it is a local drive, rather than an external network location. –  Robert Holden Apr 16 at 7:51

Replace your IF %ERRORLEVEL%==53 GOTO with the following:

IF NOT %ERRORLEVEL%==0 GOTO

The reason is, that error 53 is the windows errorcode. ERRORLEVEL might give a different number. By saying not 0, you basically say: any error other than OK.

To see the actual errorlevel, use

echo %ERRORLEVEL%

in your script. If you do so, you'll find out that it could give ERRORLEVEL 2: Network path was not found.

Note, you can test this also in a commandprompt. So first execute the command, then type echo %ERRORLEVEL%

share|improve this answer
    
Thanks LP Chip, this solved the problem. Now the script skips over the part where it can't find a connection. –  Robert Holden Apr 15 at 15:26
    
A quick follow up.. I guess this 'IF NOT %ERRORLEVEL%==0 GOTO' line will work with all errors, allowing me to skip steps. Do I just need to put it immediately after a statement that I think might cause a problem? Is there a more general way that I can get error messages to be ignored? It might be useful to be able to write something at the top of the script that says in all cases if there is an error, go to the next line. Is that possible? –  Robert Holden Apr 16 at 8:55
    
Not with batchfiles. Batchfiles are per line executed as if you typed them in the command prompt manually. It works like this, you execute a program, and at the end of the program execution, the environmental variable ERRORLEVEL is set with either 0 (success) or the error code. Note that a warning is still an error that is not 0, so it depends on how specific you want to do your errorhandling. If you want to use an errorhandling on a global scale, you'd need to use for example VBScript where you can have: on error goto ... or on error resume next, which will be applied to all future errors. –  LPChip Apr 16 at 10:36
    
Thanks LP Chip, that's good to know. Your script still solved my problem, and it's good enough for what I want to do in the future. I'll read up on VBScript at some point, thanks for the tip. –  Robert Holden Apr 16 at 11:34
    
@RobertHolden Curious that you did not accept my answer while it still seems to be what you're using. –  LPChip Apr 16 at 12:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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