0

I have a batch file that attempts to copy a file from my local machine to hundreds of network machines using RoboCopy. These machines may not be powered on or connected to the network. If this is the case, the error that is returned is "The network path was not found".

I am ok with this error, but I want to do something if this error is found. I am just now learning about batch files and I am not sure how to implement this in an IF statement.

FOR /F %%X IN (SISComputerList.txt) DO (
  robocopy.exe "Source Directory" "%%X" (FileToCopy) /R:0
  ECHO %ERRORLEVEL% 
  IF %ERRORLEVEL$ LEQ 2 (
     Echo %%X >> Logfile.txt
     Echo Fail
  ) ELSE (
      ECHO PASS
  )
)
@echo Completed 

In this code, even if there is a network error, it goes into the "Else" block with ErrorLevel 1.

Dustin Johnson
  • 405
  • 1
  • 4
  • 5
  • 1
    The all time favorite of not knowing when and how to use [delayed expansion](https://ss64.com/nt/delayedexpansion.html). In this case there is a better method as explained by help of command __IF__ output on running in a command prompt window `if /?`. Replace `IF %ERRORLEVEL$ LEQ 2` with the typing mistake `$` instead of `%` by `if not errorlevel 3` and code works. See [Single line with multiple commands using Windows batch file](https://stackoverflow.com/a/25344009/3074564) for details. – Mofi Sep 27 '18 at 17:50
  • 2
    Another option would be to do an `IF EXIST` before the `robocopy` command. `IF EXIST "%%X" (robocopy .....) else (echo folder does not exist)` – Squashman Sep 27 '18 at 19:14
  • With robocopy return codes, there's no one cut-off point for success/failure that is less than 8. How to categorize the 0..7 range is dependent on your pre and post copy expectations. Is it an error for all or any of the files to already exist on the target? What if one or more of them exist, but they are different? Obviously, the copy options you chose, also affects which of the error codes you're likely to encounter and how you would interpret them. – jwdonahue Sep 27 '18 at 20:02

0 Answers0