- notageek.it di Mirko Iodice - http://www.notageek.it -

Analyze Robocopy Log

AnalyzeRobocopyLog.vbs [1] è uno script WSH che analizza e riassume in pochi secondi i lunghissimi log generati da robocopy [2] riportando e mettendo in evidenza gli errori riscontrati e le altre informazioni utili al controllo della corretta esecuzione del processo di backup.

Come input è necessario fornire il percorso completo della cartella contente i file di log da sottoporre ad analisi.

Lo script va eseguito per mezzo del prompt dei comandi utilizzando il motore WSH “cscript.exe”, la sintassi è la seguente:

cscript.exe AnalyzeRobocopyLog.vbs "cartella_sorgente"

Esempio:

cscript.exe AnalyzeRobocopyLog.vbs "c:\logs"

Per reindirizzarne l'output in un file di testo è sufficiente eseguirlo in questo modo:

cscript.exe AnalyzeRobocopyLog.vbs "c:\logs" > output.txt
' ***************************************************************************************
' ***
' *** Scriptname: AnalyzeRobocopyLog.vbs
' ***
' *** Purpose: Scans robocopy log files for summaries and errors. Generates consolidated list
' ***          and prints to screen. Screen output can be redirected to a file if necessary.
' ***
' *** Requirements: Log files have to be in a single folder. 
' ***
' *** Usage: Run the following command for usage: "cscript AnalyzeRobocopyLog.vbs /?"
' ***
' *** Author: Dirk Pelzer
' ***
' *** Version: 2.0
' ***
' *** History: 23.09.04 (dp) Initial version (1.0)
' ***          06.10.04 (dp) Added code to read arguments from the command line (2.0)
' ***
' *** (c) 2004 Dirk Pelzer. All rights reserved. Use at your own risk.
' ***
' ***************************************************************************************

Option Explicit

'*************************************************
' Variables
'*************************************************

Dim intRet
Dim aryFileList
Dim strFolder
Dim strReadBuffer
Dim aryReadBuffer
Dim strFilename
Dim i, j
Dim strSummary
Dim oArgs

'*************************************************
' Constants
'*************************************************

Const DOUBLESPACE = "  "
Const DEFAULTSOURCEDIR = "C:\Logs"

'*************************************************
' Indicate script start
'*************************************************

wscript.echo "***************************"
wscript.echo "* Anylyzing Robocopy logs *"
wscript.echo "***************************"
wscript.echo

'*************************************************
' Read args from command line
'*************************************************

Set oArgs = WScript.Arguments

If oArgs.Count = 0 Then 
   wscript.echo "No source folder specified. Using default " & DEFAULTSOURCEDIR & "." & vbCRLF
   strFolder = DEFAULTSOURCEDIR
Else
   strFolder = oArgs(0)
End If

Set oArgs = Nothing

'*************************************************
' Figure out if user requested help
'*************************************************

Select case uCase(strFolder)
   Case "?"    Usage
   Case "/?"   Usage
   Case "HELP" Usage
   Case "-?"   Usage
   Case "H"    Usage
End Select

'*************************************************
' Retrieve list of files from a given folder
'*************************************************

intRet = EnumerateFiles(strFolder, aryFileList)

Select Case intRet
   Case 1 
      wscript.echo "Error: Specified folder does not exist."
      wscript.echo "       Please verify that " & strFolder & " really exists" & vbCRLF & _
                   "       and you have sufficient permissions." & vbCRLF
      wscript.echo "Script aborted."
      wscript.quit(1)
   Case 2
      wscript.echo "Error: The specified folder was empty."
      wscript.echo "       Please verify that " & strFolder & " contains" & vbCRLF & _
                   "       robocopy logs and you have sufficient permissions." & vbCRLF
      wscript.echo "Script aborted."
      wscript.quit(1)
End Select

'*************************************************
' Loop through log files and parse for keywords
'*************************************************

strSummary = ""

For i = 0 to uBound(aryFileList)
   strFileName = strFolder & "\" & aryFileList(i)
   'wscript.echo "strFileName = " & strFileName
   intRet = ReadFile(strFileName, strReadBuffer)
   'wscript.echo strReadBuffer

   strSummary = strSummary & vbCRLF
   strSummary = strSummary &  " --- START ---" & vbCRLF 
   strSummary = strSummary & DOUBLESPACE & aryFileList(i) &  vbCRLF

   If intRet = 0 Then
      'Valid buffer. Split up individual lines
      aryReadBuffer = split(strReadBuffer, vbCRLF)
      For j = 0 to ubound(aryReadBuffer)
         ' Parse for known keywords
         If instr(aryReadBuffer(j), "Started :")  > 0 Then strSummary = strSummary & aryReadBuffer(j) & vbCRLF
         If instr(aryReadBuffer(j), "Source :")  > 0 Then strSummary = strSummary & aryReadBuffer(j) & vbCRLF
         If instr(aryReadBuffer(j), "Dest :")  > 0 Then strSummary = strSummary & aryReadBuffer(j) & vbCRLF         
         If instr(aryReadBuffer(j), "Options :")  > 0 Then strSummary = strSummary & aryReadBuffer(j) & vbCRLF
         If instr(aryReadBuffer(j), "Total    Copied   Skipped")  > 0 Then strSummary = strSummary & aryReadBuffer(j) & vbCRLF
         If instr(aryReadBuffer(j), "Dirs :")  > 0 Then strSummary = strSummary & aryReadBuffer(j) & vbCRLF
         If instr(aryReadBuffer(j), "Files :")  > 0 Then strSummary = strSummary & aryReadBuffer(j) & vbCRLF
         If instr(aryReadBuffer(j), "Bytes :")  > 0 Then strSummary = strSummary & aryReadBuffer(j) & vbCRLF
         If instr(aryReadBuffer(j), "Times :")  > 0 Then strSummary = strSummary & aryReadBuffer(j) & vbCRLF
         If instr(aryReadBuffer(j), "Ended :")  > 0 Then strSummary = strSummary & aryReadBuffer(j) & vbCRLF
         If instr(aryReadBuffer(j), "Speed :")  > 0 Then strSummary = strSummary & aryReadBuffer(j) & vbCRLF
         If instr(aryReadBuffer(j), " ERROR ")  > 0 Then strSummary = strSummary & aryReadBuffer(j) & vbCRLF
      Next 
   End If

   strSummary = strSummary &  " --- END ---" & vbCRLF 
   strSummary = strSummary & vbCRLF
Next


wscript.echo "       RESULT"
wscript.echo "===================="
wscript.echo
wscript.echo strSummary
wscript.echo
wscript.echo "Script completed successfully"



'*************************************************
' ReadFile
'*************************************************

Function ReadFile(strFileName, strReadBuffer)
' Returns the content of a specified file in strReadBuffer

   Const ForReading = 1, ForWriting = 2
   Dim fso, f

   Set fso = CreateObject("Scripting.FileSystemObject")

   If NOT (fso.FileExists(strFileName)) Then
      ReadFile = 1 'Specified file does nor exist
      Set fso = Nothing
      Exit Function
   End If

   Set f = fso.OpenTextFile(strFileName, ForReading)

   strReadBuffer =   f.ReadAll

   if len(strReadBuffer)> 0 Then
      ReadFile = 0 'File read successfully
   Else
      ReadFile = 2 'File was empty
   End if

   Set f = Nothing
   Set fso = Nothing

End Function

'*************************************************
' EnumerateFiles
'*************************************************

Function EnumerateFiles(strFolder, aryFileList)
' Returns an array of all files in a specified folder 
' Return code = 0: success
' Return code = 1: specified folder does not exist
' Return code = 2: no files found

   Dim strFileList
   Dim fso, f, f1, fc, s

   strFileList = ""
   Set fso = CreateObject("Scripting.FileSystemObject")

   If NOT (fso.FolderExists(strFolder)) Then
      EnumerateFiles = 1 'Specified folder does not exist
      Set fso = Nothing
      Exit Function
   End If

   Set f = fso.GetFolder(strFolder)
   Set fc = f.Files
   For Each f1 in fc
      strFileList = strFileList & f1.name & ";"
      'wscript.echo "strFileList = " & strFileList
   Next

   If right (strFileList, 1) = ";" Then strFileList = left(strFileList, len(strFileList)-1)
   aryFileList = split (strFileList, ";")

   If uBound(aryFileList) >= 0 then 
      EnumerateFiles = 0 'Files found
   Else
      EnumerateFiles = 2 'No files found
   End If
  
   Set fc = nothing
   Set f = nothing
   Set fso = nothing

End Function

'*************************************************
' Usage
'*************************************************

Sub Usage
   wscript.echo
   wscript.echo "Usage:"
   wscript.echo "~~~~~~" & vbCRLF
   wscript.echo "cscript " & WScript.ScriptName & " [sourcepath]" & vbCRLF
   wscript.echo "   where [sourcepath] is the full path to the log files to be analyzed." & vbCRLF
   wscript.echo "If no [sourcepath] is specified, the default path " & DEFAULTSOURCEDIR & " is used." & vbCRLF
   wscript.echo "Example: cscript " & WScript.ScriptName & " c:\Temp"
   wscript.quit (0)
End Sub