2

I don't know if a batchfile is a proper solution to this problem, but I'll describe what I need to do and what I've done so far.

I use a batch file triggering Robocopy and then Blat to send logfiles of a daily file copy task to a target address. Instead of sending the logfiles daily, regardless of success or failure of the copy, I'd like to ONLY send the logfile if a failure is present in the log.

An example of my existing batch is below. Please note, I am not a programmer, and only have a rudimentary understanding of scripting.

_____
REM - Daily Copy Procedure

@Echo Off

robocopy.exe "C:\TEMP\Source" "C:\TEMP\Destination" *.* /r:1 /w:1 /nfl /ndl /mt /tee /np /log:"robocopy_1.log"

Blat.exe robocopy_1.log -to emyemailaddress -serverSMTP mysmtpserver
_____

I'd like to add something between the Robocopy line and the Blat line that will read the logfile and IF it detects a value greater than zero in the FAILED column, THEN proceed to Blat the logfile to my email address, otherwise exit the batch without running Blat.

     Total    Copied   Skipped  Mismatch    FAILED    Extras
Dirs :         1         0         1         0         0         0

(EDIT)

Okay, I solved my problem with a simple solution. I was approaching it in probably the most complicated way possible. Robocopy provides detailed error descriptions in its logfiles whenever something goes awry. All I had to do was get the bat file to parse the logfile for the word "ERROR" and all is good. This solution casts a broad net, I'm not looking for any sort of finesse here, so an IF ELSE command did the trick.

REM @ECHO OFF

REM Run Robocopy with appropriate arguments.

robocopy.exe "C:\TEMP\Source" "C:\TEMP\Destination" *.* /r:1 /w:1 /nfl /ndl /s /mt /tee /np /log:"robocopy_1.log"

REM Search for errors in the robocopy logfile and email if any are found.

FIND /c "ERROR" C:\TEMP\robocopy_1.log

IF %errorlevel% equ 1 (EXIT) ELSE (goto mailfile)

REM Email the logfile if problems are found otherwise the BAT will exit before this step.

:mailfile 
Blat.exe robocopy_1.log -to myemail -serverSMTP mysmtp

1 Answers1

0
for /f "tokens=6" %%i in ('findstr /b "Dirs :" robocopy.log') do set fails=%%i
if %fails%=0 exit

looks for a line starting with "Dirs :" in the logfile and store the sixth token in a variable (standard delimters: space, tab, comma, dot; so first token = Dirs, second token= : etc...)

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • Hello, thanks for your input. I assume a /f argument is needed right after for and before "tokens=6" I incorporated your suggestion but am receiving a "then was unexpected at this time" error. – Eduardo Correa-Lima Vorum Feb 13 '15 at 23:25
  • Eduardo: Arggh... JosefZ: another Arggh.... Thank you both. I shouldn't answer questions too late in the night ^^ – Stephan Feb 14 '15 at 08:31