我想使用-replace删除引导路径信息。
robocopy.exe C:\FOLDER\Data2 C:\FOLDER\Data2 /l /nocopy /is /e /fp /ns /nc /njh /njs /log:c:\temp\FolderList.txt
我的输出:
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
我想要的输出:
Documents\
Documents\1.txt
Documents\2.txt
Documents\3.txt
Documents\4.txt
Documents\5.txt
Documents\TEST\
Documents\TEST\5.txt
如果您的输出是日志文件的内容:
$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
如果您的输出被写入终端,您可以将robocopy的输出重定向到一个文件,对其进行读取,并用空字符串替换您不需要的路径的所有出现:
$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)