Get-ChildItem带有日志记录和Copy-Item的一条命令

huangapple go评论67阅读模式
英文:

Get-ChildItem with Logging and Copy-Item in one command

问题

以下是您要翻译的内容:

基本上,我的目标是将一个文件夹的内容复制到另一个文件夹,排除一个名称,并记录已复制的所有内容。

我在记录Get-ChildItem命令与管道结合使用的部分遇到了问题-->Copy-Item

下面的命令将所有来自Get-ChildItem命令的数据记录到日志文件中:

$SourcePath = "D:\TEST"
$DestPath = "C:\TEST"
$LogDetailFile = "C:\Temp\CopyDetail.log"
$Exclude = "!_Archive_!"

Get-ChildItem $SourcePath -Recurse | Where {$_.FullName -notmatch $Exclude} |
Select FullName | Add-Content $LogDetailFile

当我尝试使用下一个管道命令将它们复制到$DestPath时,它不起作用:

Get-ChildItem $SourcePath -Recurse | Where {$_.FullName -notmatch $Exclude} |
Copy-Item -Destination {Join-Path $DestPath $_.FullName.Substring($SourcePath.length)} | 
Add-Content $LogFile

当我不使用记录选项运行它时,一切都正常,整个数据都被复制:

Get-ChildItem $SourcePath -Recurse | Where {$_.FullName -notmatch $Exclude} |
Copy-Item -Destination {Join-Path $DestPath $_.FullName.Substring($SourcePath.length)}

我已经尝试过切换管道顺序,但它不起作用。我在哪里出错了?如何将一个文件夹的所有内容复制到另一个文件夹,并将所有已复制的项目记录到日志文件中?

现在,如果我想要两件事 - 记录和复制这些项目,我需要运行两个命令,只想在一个命令中完成它。

英文:

Basically my goal is copy content of a folder to another folder with exclusion one name and also log everything what has been copied.

I'm stock on logging Get-ChildItem command combined with Pipeline --> Copy-Item

Bellow command will put to log file all data from command Get-ChildItem:

$SourcePath	= "D:\TEST"
$DestPath = "C:\TEST"
$LogDetailFile = "C:\Temp\CopyDetail.log"
$Exclude = "!_Archive_!"

Get-ChildItem $SourcePath -Recurse | Where {$_.FullName -notmatch $Exclude} |
Select FullName | Add-Content $LogDetailFile

When I extra add with next Pipeline command to copy them to $DestPath it wont work:

Get-ChildItem $SourcePath -Recurse | Where {$_.FullName -notmatch $Exclude} |
Copy-Item -Destination {Join-Path $DestPath $_.FullName.Substring($SourcePath.length)} | 
Add-Content $LogFile

When I done it without logging options, then everything works fine and whole data is copied:

Get-ChildItem $SourcePath -Recurse | Where {$_.FullName -notmatch $Exclude} |
Copy-Item -Destination {Join-Path $DestPath $_.FullName.Substring($SourcePath.length)}

I tried already switch pipelines between but it don't work.
What I'm missing here? How to copy everything from one Folder to another and log all copied items to logfile?

Right now If I want to have 2 things - logging and copy those items I need to run 2 commands, just want to have it in one command.

答案1

得分: 1

ForEach-Object 可以帮助你,在这里将两者结合的原因是因为这两个命令 (Add-ContentCopy-Item) 都没有输出,所以没有内容可以传递。

$SourcePath = 'D:\TEST'
$DestPath = 'C:\TEST'
$LogDetailFile = 'C:\Temp\CopyDetail.log'
$Exclude = '!_Archive_!'

Get-ChildItem $SourcePath -Recurse | ForEach-Object {
    if($_.FullName -notmatch $Exclude) {
        $_ | Copy-Item -Destination { Join-Path $DestPath $_.FullName.Substring($SourcePath.Length) }
        $_ | Select-Object FullName # 可能需要在这里使用 `-ExpandProperty`
    }
} | Add-Content $LogDetailFile
英文:

ForEach-Object can help you here, the reason why combining both breaks is because neither of those cmdlets (Add-Content and Copy-Item) produce output so there is nothing to pipe to.

$SourcePath = 'D:\TEST'
$DestPath = 'C:\TEST'
$LogDetailFile = 'C:\Temp\CopyDetail.log'
$Exclude = '!_Archive_!'

Get-ChildItem $SourcePath -Recurse | ForEach-Object {
    if($_.FullName -notmatch $Exclude) {
        $_ | Copy-Item -Destination { Join-Path $DestPath $_.FullName.Substring($SourcePath.Length) }
        $_ | Select-Object FullName # Probably need `-ExpandProperty` here
    }
} | Add-Content $LogDetailFile

huangapple
  • 本文由 发表于 2023年4月13日 20:45:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76005597.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定