Freddis Posted July 8 Posted July 8 Hi everyone, I'm learning programming with AUTOIT. I've put four lines of code in it to perform backups with the Windows 10 "Robocopy" command. I intercept an ERROR string in the log file, but sometimes it gives false errors, definitely because I didn't write it well. For this reason, I'd like your help in learning how to intercept the error and the exit code well. I tried writing it with windows script commands and it seems to work fine and also with AUTOIT but I think you need to change the interception method. I'll put the scripts in there. Thanks to anyone who would like to help me.parzial.batparzial.au3
Freddis Posted July 8 Author Posted July 8 ... robocopy "%SOURCE%" "%DEST%\Backup_Archivio" /E /Z /XJ /R:3 /W:5 /COPY:DAT /TEE /V /X /LOG:"%LOGFILE%" echo. echo ===================================== echo Controllo Errori di copia: findstr /C:"ERROR" "%LOGFILE%" echo. if %ERRORLEVEL% EQU 0 set ESITO=OK: Niente da copiare if %ERRORLEVEL% EQU 1 set ESITO=OK: File copiati correttamente if %ERRORLEVEL% EQU 3 set ESITO=OK: File e cartelle copiate if %ERRORLEVEL% EQU 8 set ESITO=OK: Backup ok, 1 file saltato - vedi percorso sopra if %ERRORLEVEL% GEQ 9 set ESITO=ERRORE GRAVE: Backup fallito echo %ESITO% echo Codice uscita: %ERRORLEVEL% echo ===================================== powershell -c "(New-Object Media.SoundPlayer 'C:\Windows\Media\Windows Background.wav').PlaySync()" powershell -c "[System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms'); [System.Windows.Forms.MessageBox]::Show('%ESITO%', 'Backup Archivio')" pause AUTOIT script: ; Controlla errori nel file di log ;Local $sLogContent = FileRead($logFile) If StringInStr($sLogContent, "ERRORE") Then _GIF_DeleteGIF($hGIF) ; Cancella la gif di attesa dalla form MsgBox($MB_ICONERROR, "FREDDIS backup", "Si è verificato un errore durante il backup." & @CRLF & "Codice uscita: " & @CRLF & "Controlla il file log: " & $logfile) Exit Else _GIF_DeleteGIF($hGIF) MsgBox($MB_ICONINFORMATION, "FREDDIS backup", "Backup completato correttamente.") Exit EndIf
argumentum Posted July 8 Posted July 8 expandcollapse popup#include <MsgBoxConstants> #include <Process.constants> ; --- CONFIGURATION --- Local $sSource = "C:\Path\To\Source" ; Change to actual source Local $sDest = "D:\Path\To\Destination" ; Change to actual destination Local $sLogFile = "C:\Path\To\backup_log.txt" ; Change to actual log path ; Recreating their exact Robocopy flags Local $sRobocopyArgs = StringFormat('"%s" "%s\\Backup_Archivio" /E /Z /XJ /R:3 /W:5 /COPY:DAT /TEE /V /X /LOG:"%s"', $sSource, $sDest, $sLogFile) ; --- EXECUTION --- ; Run Robocopy and wait for it to finish to capture the exact exit code Local $iExitCode = RunWait("robocopy " & $sRobocopyArgs, "", @SW_HIDE) ; --- LOG ANALYSIS --- Local $sLogContent = FileRead($sLogFile) Local $bHasErrorWord = StringInStr($sLogContent, "ERROR") Or StringInStr($sLogContent, "ERRORE") ; --- EVALUATE OUTCOME --- Local $sEsito = "" Local $iIcon = $MB_ICONINFORMATION ; Robocopy exit codes: 0-7 are successful variations. 8+ means issues. Select Case $iExitCode = 0 $sEsito = "OK: Niente da copiare (Nessuna modifica)" Case $iExitCode = 1 $sEsito = "OK: File copiati correttamente" Case $iExitCode = 2 $sEsito = "OK: Ci sono file extra nel backup" Case $iExitCode = 3 $sEsito = "OK: File e cartelle copiate" Case $iExitCode = 4 Or $iExitCode = 5 Or $iExitCode = 6 Or $iExitCode = 7 $sEsito = "OK: Backup completato (Mismatches/Modifiche rilevate)" Case $iExitCode = 8 $sEsito = "ATTENZIONE: Backup parziale, alcuni file sono stati saltati!" $iIcon = $MB_ICONWARNING Case $iExitCode >= 9 $sEsito = "ERRORE GRAVE: Backup fallito!" $iIcon = $MB_ICONERROR EndSelect ; Double-check if Robocopy code was okay but the log still contains error strings If $iExitCode < 8 And $bHasErrorWord Then $sEsito &= @CRLF & "(Nota: Trovate stringhe di ERRORE nel file log)" $iIcon = $MB_ICONWARNING EndIf ; --- UI NOTIFICATION --- ; Play a standard Windows notification sound instead of calling PowerShell SoundPlay(@WindowsDir & "\Media\Windows Background.wav", $SOUND_WAIT) ; Clean up their GIF if this is integrated into their existing GUI ; _GIF_DeleteGIF($hGIF) ; Show the final result MsgBox($iIcon, "FREDDIS backup", $sEsito & @CRLF & "Codice uscita Robocopy: " & $iExitCode & @CRLF & "Controlla il file log: " & $sLogFile) not tested but should help Freddis 1 Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting
Freddis Posted July 8 Author Posted July 8 Thanks for the help, much appreciated, now I study it and then implement it of my script 👍 argumentum 1
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now