使用 PowerShell 比较不同的文件扩展名和复制文件
Comparing Different File extension and Copying Files with PowerShell
场景:*.pdf 在文件夹 1 *.rtf 在文件夹 2,两个文件夹的文件名相同,但扩展名不同。需要比较这两个文件夹并仅复制等于 => 从 Folder2 到 Folder3 的 *.rtf。
脚本:
Compare-Object (Get-childitem $Folder1 -Recurse | foreach-object {$_.basename}) (Get-childitem $Folder2 -Recurse | foreach-object {$_.basename}) -Property RelativePath, Name, Length |
Sort RelativePath, Name, Length -desc | % {
if ($file -ne $_.RelativePath)
{ $_ }
$file = $_.RelativePath
} | Where-Object {$_.SideIndicator -eq "=>"} | ForEach-Object {Copy-Item ("$Folder2\" + $file) -Destination ("$Folder3\" + $file) -Force}
通常你应该有一个问题。我不确定您是从哪里得到 属性 RelativePath
的,所以我介绍这个应该很容易理解。我本可以将它变成一行命令,但同样,这更具可读性。
$Folder1 = "C:\temp\folder1"
$Folder2 = "C:\temp\folder2"
$Folder3 = "C:\temp\folder3"
$Folder1Contents = Get-childitem $Folder1 -Recurse -Include *.pdf
$Folder2Contents = Get-childitem $Folder2 -Recurse -Include *.rtf
Compare-Object $Folder1Contents $Folder2Contents -Property BaseName -PassThru | Where-Object{$_.SideIndicator -eq "=>"} | Move-Item -Destination $Folder3 -Force -WhatIf
将 $Folder1
中的 pdf 文件与 $Folder2
中的 rtf 文件进行比较。如果 $Folder2
中有 .rtf 文件,那么它将被移动到 $Folder3
。 -Force
将覆盖该目录中先前存在的文件,因此在完成测试后删除 -WhatIf
。
最终结果:我添加了一个 Transcript 和 if/elseif 语句用于记录。双比较对象只是为了将比较结果显示在日志中。感谢您的帮助,马特,您让我免去了数小时的头痛。
$Folder1 = "C:\temp\folder1"
$Folder2 = "C:\temp\folder2"
$Folder3 = "C:\temp\folder3"
$log = "C:\logs\compare.log"
$ErrorActionPreference="SilentlyContinue"
Stop-Transcript | out-null
$ErrorActionPreference = "Continue"
$OutputFileLocation = "$log"
Start-Transcript -path $OutputFileLocation -append
$folder1contents = Get-childitem $folder1 -Recurse -Include *.pdf
$folder2contents = Get-childitem $folder1 -Recurse -Include *.rtf
$filecheck = "C:\temp\folder1\*.pdf"
$filepresent = Test-Path $filecheck
if ($filepresent -eq $false)
{
Write-host "No Files To Compare"
}
elseif ($filepresent -eq $true)
{
Compare-Object $folder1contents $folder2contents -Property BaseName -PassThru
Compare-Object $folder1contents $folder2contents -Property BaseName -PassThru | Where-Object{$_.SideIndicator -eq "=>"} | Copy-Item -Destination $folder3 -Force
}
stop-transcript
场景:*.pdf 在文件夹 1 *.rtf 在文件夹 2,两个文件夹的文件名相同,但扩展名不同。需要比较这两个文件夹并仅复制等于 => 从 Folder2 到 Folder3 的 *.rtf。
脚本:
Compare-Object (Get-childitem $Folder1 -Recurse | foreach-object {$_.basename}) (Get-childitem $Folder2 -Recurse | foreach-object {$_.basename}) -Property RelativePath, Name, Length |
Sort RelativePath, Name, Length -desc | % {
if ($file -ne $_.RelativePath)
{ $_ }
$file = $_.RelativePath
} | Where-Object {$_.SideIndicator -eq "=>"} | ForEach-Object {Copy-Item ("$Folder2\" + $file) -Destination ("$Folder3\" + $file) -Force}
通常你应该有一个问题。我不确定您是从哪里得到 属性 RelativePath
的,所以我介绍这个应该很容易理解。我本可以将它变成一行命令,但同样,这更具可读性。
$Folder1 = "C:\temp\folder1"
$Folder2 = "C:\temp\folder2"
$Folder3 = "C:\temp\folder3"
$Folder1Contents = Get-childitem $Folder1 -Recurse -Include *.pdf
$Folder2Contents = Get-childitem $Folder2 -Recurse -Include *.rtf
Compare-Object $Folder1Contents $Folder2Contents -Property BaseName -PassThru | Where-Object{$_.SideIndicator -eq "=>"} | Move-Item -Destination $Folder3 -Force -WhatIf
将 $Folder1
中的 pdf 文件与 $Folder2
中的 rtf 文件进行比较。如果 $Folder2
中有 .rtf 文件,那么它将被移动到 $Folder3
。 -Force
将覆盖该目录中先前存在的文件,因此在完成测试后删除 -WhatIf
。
最终结果:我添加了一个 Transcript 和 if/elseif 语句用于记录。双比较对象只是为了将比较结果显示在日志中。感谢您的帮助,马特,您让我免去了数小时的头痛。
$Folder1 = "C:\temp\folder1"
$Folder2 = "C:\temp\folder2"
$Folder3 = "C:\temp\folder3"
$log = "C:\logs\compare.log"
$ErrorActionPreference="SilentlyContinue"
Stop-Transcript | out-null
$ErrorActionPreference = "Continue"
$OutputFileLocation = "$log"
Start-Transcript -path $OutputFileLocation -append
$folder1contents = Get-childitem $folder1 -Recurse -Include *.pdf
$folder2contents = Get-childitem $folder1 -Recurse -Include *.rtf
$filecheck = "C:\temp\folder1\*.pdf"
$filepresent = Test-Path $filecheck
if ($filepresent -eq $false)
{
Write-host "No Files To Compare"
}
elseif ($filepresent -eq $true)
{
Compare-Object $folder1contents $folder2contents -Property BaseName -PassThru
Compare-Object $folder1contents $folder2contents -Property BaseName -PassThru | Where-Object{$_.SideIndicator -eq "=>"} | Copy-Item -Destination $folder3 -Force
}
stop-transcript