0

I want to remove leading path information using -replace.

robocopy.exe C:\FOLDER\Data2 C:\FOLDER\Data2 /l /nocopy /is /e /fp /ns /nc /njh /njs  /log:c:\temp\FolderList.txt

My output :

C:\FOLDER\Data2\
C:\FOLDER\Data2\Documents\
        C:\FOLDER\Data2\Documents\1.txt
        C:\FOLDER\Data2\Documents\2.txt
        C:\FOLDER\Data2\Documents\3.txt
        C:\FOLDER\Data2\Documents\4.txt
        C:\FOLDER\Data2\Documents\5.txt
C:\FOLDER\Data2\Documents\TEST\
        C:\FOLDER\Data2\Documents\TEST\5.txt

My desired output :

    Documents\
            Documents\1.txt
            Documents\2.txt
            Documents\3.txt
            Documents\4.txt
            Documents\5.txt
    Documents\TEST\
            Documents\TEST\5.txt
Arbelac
  • 1,698
  • 6
  • 37
  • 90

1 Answers1

0

In case your output is the content of your log file:

$path = 'C:\FOLDER\Data2\'
$pattern = [regex]::Escape($path)
$newContent = @()
Get-Content -Path "c:\temp\FolderList.txt" | ForEach-Object {$newContent += $_ -replace $pattern, ''}
Set-Content -Path "c:\temp\FolderList.txt" -Value $newContent

In case your output is written to the terminal, you can redirect the output of robocopy to a file, read it and replace every occurence of your unwanted path by an empty string:

$path = 'C:\FOLDER\Data2\'
$tempFile = New-TemporaryFile
Start-Process robocopy.exe -ArgumentList "`"$path`" `"$path`" /l /nocopy /is /e /fp /ns /nc /njh /njs  /log:c:\temp\FolderList.txt" -Wait -RedirectStandardOutput ($tempFile.FullName)

$pattern = [regex]::Escape($path)
Get-Content -Path ($tempFile.FullName) | ForEach-Object {Write-Host "$($_ -replace $pattern, '')"}

Remove-Item -Path ($tempFile.FullName)
stackprotector
  • 10,498
  • 4
  • 35
  • 64