locked
Best Method of Parsing Robocopy Log? RRS feed

  • Question

  • I'm having a hard time trying to parse a robocopy log because the data is on separate lines. Usually, if the data label is on the same line as the label, I can easily loop through all of the lines and pull the data.

    But since Robocopy formats the log differently, I can't just loop through the lines because the data is on separate lines.

    I need to be able to see how many directories or files failed and this is how the log is typically formatted.

                   Total    Copied   Skipped  Mismatch    FAILED    Extras
        Dirs :         1         1         0         0         0         0
       Files :       106       106         0         0         0         0
       Bytes :  481.53 m  481.53 m 0         0         0         0
       Times :   0:00:11   0:00:10                       0:00:00   0:00:00

    http://pastebin.com/MLhry4ww

    I'm thinking regex could probably help in some regards, but regex is pretty cryptic to me.

    Any ideas?

    Thursday, August 30, 2012 10:05 PM

Answers

  • Humm, in your case I don't think RegEx is needed.

    The table on the bottem is a fixed width table, that means you can read the whole line, then using String.SubString(start, length) to read a column, then .Trim() to remove unneeded space to get the actual content.

    If you want to find how many Dirs and Files failed, you can just find lines which .StartsWith("    Dirs") or .StartsWith("   Files") to start parsing them. In this way, it fits neatly in loops.

    • Proposed as answer by Mike Feng Friday, August 31, 2012 7:09 AM
    • Marked as answer by Mike Feng Friday, September 7, 2012 6:30 AM
    Friday, August 31, 2012 2:55 AM
    Answerer

All replies

  • Humm, in your case I don't think RegEx is needed.

    The table on the bottem is a fixed width table, that means you can read the whole line, then using String.SubString(start, length) to read a column, then .Trim() to remove unneeded space to get the actual content.

    If you want to find how many Dirs and Files failed, you can just find lines which .StartsWith("    Dirs") or .StartsWith("   Files") to start parsing them. In this way, it fits neatly in loops.

    • Proposed as answer by Mike Feng Friday, August 31, 2012 7:09 AM
    • Marked as answer by Mike Feng Friday, September 7, 2012 6:30 AM
    Friday, August 31, 2012 2:55 AM
    Answerer
  • Humm, in your case I don't think RegEx is needed.

    The table on the bottem is a fixed width table, that means you can read the whole line, then using String.SubString(start, length) to read a column, then .Trim() to remove unneeded space to get the actual content.

    If you want to find how many Dirs and Files failed, you can just find lines which .StartsWith("    Dirs") or .StartsWith("   Files") to start parsing them. In this way, it fits neatly in loops.

    Interesting. I guess that I just assumed it was on multiple lines because of the way it looks. I'll try what you suggested and see what I get.

    Thanks

    Friday, August 31, 2012 1:51 PM
  • Humm, in your case I don't think RegEx is needed.

    The table on the bottem is a fixed width table, that means you can read the whole line, then using String.SubString(start, length) to read a column, then .Trim() to remove unneeded space to get the actual content.

    If you want to find how many Dirs and Files failed, you can just find lines which .StartsWith("    Dirs") or .StartsWith("   Files") to start parsing them. In this way, it fits neatly in loops.

    Using the code below, I was able to get the "files" and "directories" lines:

    Dim file_lines As String() = File.ReadAllLines("C:\log.txt")
            For Each line As String In file_lines
                If line.Length >= 10 Then
                    Select Case True
                        Case line.Substring(0, 10).ToLower.Contains("files :")
                            Dim new_line As String = line.ToLower.Replace("files :", "")
                            Me.TextBox1.Text = new_line.Trim
                        Case line.Substring(0, 10).ToLower.Contains("dirs :")
                            Dim new_line As String = line.ToLower.Replace("dirs :", "")
                            Me.TextBox2.Text = new_line.Trim
                    End Select
                End If
            Next

    Files:

    106       106         0         0         0         0

    Directories:

    1         1         0         0         0         0

    Which looks good. But how would I parse out each column of data? The order tells me the set of data I'm looking for. In this case, it's column index 4. But I can't figure out how to remove all the data before and after that point.


    Friday, August 31, 2012 2:38 PM
  • First, copy a line from the log and paste it in notepad, then arrange a "ruler" like the following:

             1         2         3         4         5         6         7
    1234567890123456789012345678901234567890123456789012345678901234567890
        Dirs :         1         1         0         0         0         0
    

    Now, you can see the second column is from 11th to 20th characters, the third column is 21st to 30th, etc. So you can use SubString() to read in every 10 characters of string, Trim() them and use Convert.ToInt32() to get the value.


    Monday, September 3, 2012 2:12 AM
    Answerer