Convert robocopy log into csv file.

Michael 1 Reputation point
2021-05-13T14:24:11.73+00:00

Hi can someone help me get this robocopy log to export into a csv file? The columns can be size, date, source, and destination. Here is what the log outputs after running robocopy:

------------------------------------------------------------------------------  

               Total    Copied   Skipped  Mismatch    FAILED    Extras  
    Dirs :         4         0         4         0         0         0  
   Files :        17         0        17         0         0         0  
   Bytes :    27.0 k         0    27.0 k         0         0         0  
   Times :   0:00:00   0:00:00                       0:00:00   0:00:00  
   Ended : Thursday, May 13, 2021 10:12:44 AM  

This is the script im using to export the log.

$path = "C:\Powershell\robocopylog.txt"
If(!(test-path $path))
{
New-Item -ItemType file -Force -Path $path
}
Clear-Content "C:\Powershell\robocopylog.txt" -Force
$EmailFrom = "test@tiedtlaw email .com"
$EmailTo = "test@tiedtlaw email .com"
$EmailBody = "Robocopy completed successfully. See attached log file for details"
$EmailSubject = "Robocopy Summary"

$files = @("SCRIPT TEST")

for($i = 0; $i -lt $files.Count; $i++){
robocopy "C:\$($files[$i])" "C:\NEW TEST\folder\folder\$($files[$i])" /Z /e /xx /W:5 /NFL /NDL /NJH /nc /np /unilog+:$Logfile
}

Send-MailMessage -To $EmailTo -from $EmailFrom -Subject $Line -Body $Line -attachment $Logfile -smtpserver 192.161.245.121 -Port 25

Windows for business | Windows Server | User experience | PowerShell
{count} votes

4 answers

Sort by: Most helpful
  1. KristianSmith-MSFT 446 Reputation points Microsoft Employee Moderator
    2021-05-13T20:55:48.22+00:00

    Hello Michael-9141,

    Thanks for reaching out regarding your robocopy log question. As this question seems to not pertain to Open Specification documentation issues, I will remove it from the list of associated tags. Please refer to the Open Specifications link below for more details:

    https://learn.microsoft.com/en-us/openspecs/main/ms-openspeclp/3589baea-5b22-48f2-9d43-f5bea4960ddb

    Thanks again for reaching out.

    Regards,
    Kristian Smith
    Support Escalation Engineer
    Windows Open Spec Protocols

    0 comments No comments

  2. Anonymous
    2021-05-14T05:27:29.537+00:00

    Hi,

    What do you expect the CSV file to be like? You can try the ConvertFrom-String cmdlet.
    https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/convertfrom-string

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  3. MotoX80 36,291 Reputation points
    2021-05-20T13:10:30.607+00:00

    Im mainly trying to get it to export to a csv.

    https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/export-csv?view=powershell-5.1

    The examples primarily show how to export an object (like a collection of processes) to a csv. That's fairly straightforward because objects have a name/value pair which correspond to the columns of a csv.

    You don't have an object to export, so you either need to build a custom object or just "manually" create the csv from that data that you parsed.

    A csv file is just a plain old text file with headers and data.

    Header1,Header2,"Header with spaces"  
    Data1, Data2, "Data, with, commas, and, spaces."   
    

    Step 1 is to identify what columns you want in the csv, and where you are going to get each data element. Then just write the data to a file.

    https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/out-file?view=powershell-5.1

    The first answer in this question shows how to create a custom psobject that is exported to a csv if you want to go that route.

    https://stackoverflow.com/questions/21047185/how-to-export-data-to-csv-in-powershell


  4. MotoX80 36,291 Reputation points
    2021-05-21T00:47:15.023+00:00

    Could you try doing this? Im just really confused and not really understanding :/

    I believe that there is an ancient proverb that goes something like this: Give a man a fish and you feed him for one day. Teach a man how to fish and you feed him for a lifetime.

    Sure, I could write it, but then if you ever want to modify it you'll be back to being really confused. So while this might be painful, in the long run you will benefit from learning more about PS.

    While you can reference $files as an array in C++ format, with PS there is an easier way. Paste this into Powershell_ise and run it.

    $files = @("File1","File2")  
    foreach ($file in $files){  
     "robocopy this file: {0} " -f $file  
    }  
    

    Update: I took the $files variable name too literally. I would expect you to have folder names in that array.

    More to come...

    Create variables for the source and destination folders. It will make it easier to reference/change them. And if you process the robocopy log each time, instead of appending to one big log file, it might be easier. Add in the code that I posted in a previous comment and see if you can parse the log.

    $srcFolder ='C:\temp\Src'  
    $dstFolder ='C:\temp\dst'  
    $folders = @("Foo1")  
    $logfile = "C:\temp\robocopylog.txt"   
    foreach ($f in $folders){  
        "Source is {0} " -f "$srcFolder\$f"   
        "Destination is {0} " -f "$dstFolder\f"  
    	robocopy.exe "$srcFolder\$f" "$dstFolder\$f" /e /l /unilog:$Logfile  
        $log = Get-Content $logfile             # read in the log for this run of robocopy, append it to a "job" log if you want all of the logs saved  
        $capture = $false                       # have we found where the file names start?  
        foreach ($l in $log) {  
            if ($l -match "  Total    Copied   Skipped ") {  
                "We found the ending point!"  
                $capture = $false  
            }  
            if (($capture) -and ($l -notmatch '-----------') -and ($l.trim() -ne '')) {        # ignore blank and separator lines   
                $l                                          # show the line to process   
                # add code here to look for lines that start with "New Dir", Save the directory name  
                # look for "New File", this line has the file size and file name.   
                # use .substring methods to parse the data you   
                # https://www.itechguides.com/powershell-substring/#:~:text=%20How%20to%20Extract%20a%20Powershell%20Substring%20from,may%20want%20to%20extract%20a%20PowerShell...%20More%20  
            }       
            if ($l -match " Options : ") {  
                "We found a starting point!"  
                $capture = $true  
            }  
        }   
    }  
    
    
    
    
    
    
    
    
    
     
    
      
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.