Batch file, skip a step if an error occurs

31,577

Solution 1

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

Solution 2

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:
31,577

Related videos on Youtube

Robert Holden
Author by

Robert Holden

Updated on September 18, 2022

Comments

  • Robert Holden
    Robert Holden over 1 year

    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?

    • David
      David about 10 years
      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.
    • Robert Holden
      Robert Holden about 10 years
      Sure, I was in a hurry, didn't realise how sloppy it was. Apologies
  • Robert Holden
    Robert Holden about 10 years
    Thanks LP Chip, this solved the problem. Now the script skips over the part where it can't find a connection.
  • Wally
    Wally about 10 years
    I use the ping/errorlevel method myself for file sharing batching too! I find it helpful also.
  • Robert Holden
    Robert Holden about 10 years
    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
    Robert Holden about 10 years
    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
    Robert Holden about 10 years
    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?
  • LPChip
    LPChip about 10 years
    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.
  • Robert Holden
    Robert Holden about 10 years
    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.
  • LPChip
    LPChip about 10 years
    @RobertHolden Curious that you did not accept my answer while it still seems to be what you're using.
  • Robert Holden
    Robert Holden about 10 years
    I'm using the ping version suggested by David. I checked both, and both work. I'm using the 'ping' version as this logs the connection attempt in my log file, whereas yours does not, making the other version more convenient. Both work, but I'm only allowed to accept one.