Powershell out-file csv delimiting

2k views Asked by At

I have a powershell command using robocopy to list files. I am trying to output to csv but it does not separate the columns as needed. I get two columns where there should be 5. is there a delimiter parameter for out-file csv?

Here is the current code:

robocopy .\Documents\ NULL /L /S /V /E /BYTES /FP /NC /NDL /XJ /TS /R:1 /W:2 | Out-File -Append -FilePath "c:\beehive_20150608.csv" -Encoding ASCII

I tried export-csv and it only brings back the string length of the path.

Any suggestions?

1

There are 1 answers

4
n0rd On BEST ANSWER

I assume you need to list all files under some folder. You can use Get-ChildItem -Recurse for that instead of robocopy, filter only files with Where-Object then pick only properties you want with Select-Object:

Get-ChildItem .\Documents -Recurse | Where-Object {!$_.PsIsContainer}| Select-Object -property LastWriteTime,Length,FullName | Export-Csv aaa.csv

If you need to use robocopy you may try parsing it's output:

robocopy .\Documents\ NULL /L /S /V /E /BYTES /FP /NC /NDL /XJ /TS /R:1 /W:2 /njh /njs | Where-Object {$_ -match '^\s*([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+(.*?)\s*$'} | ForEach-Object { new-object PSObject -property @{Length=$matches[1];Date=$matches[2];Time=$matches[3];Name=$matches[4]}} | Export-Csv aaa.csv

It's ugly, but should do what you need. I added /njh and /njs arguments to your command line to suppress robocopy's header and footer, Where-Object removes non-matching (empty) lines and ForEach-Object creates object with named properties using $Matches created by -match operator used in Where-Object.