我想將包含子檔案夾的檔案夾 A 中的檔案復制到另一個檔案夾。這樣我就可以看到檔案夾 A 和子檔案夾中的所有檔案。
我使用了這個命令,它作業得很好:
md "d:\destination"
cd /d "d:\source\A"
for /r %d in (*) do copy "%d" "d:\destination\"
但是,有一些重復的檔案。所以我被要求洗掉現有檔案或洗掉重復檔案。 選擇 我想保留這兩個檔案。這可能嗎?如果是,我該怎么做?
uj5u.com熱心網友回復:
- 打開 PowerShell ISE
- 將下面列出的腳本粘貼到腳本窗格中,然后按播放。
遞回識別檔案夾中的所有檔案
$Source = "C:\Source"
(Get-ChildItem -Path $Source -File -Recurse -Force).FullName
# If you just want the name (Get-ChildItem -Path $Source -File -Recurse -Force).Name
遞回地將檔案從源檔案夾復制到目標檔案夾
$Source = "C:\Source"
$Destination = "C:\Destination"
# If you need to create the destination folder you can enter; New-Item -ItemType "File" -Path $Destination -Force
Get-ChildItem -Path $Source -File -Recurse -Force | Copy-Item -Destination $Destination -Verbose # -Force
(Get-ChildItem -Path $Destination -File -Recurse -Force).FullName
編輯:我從 Copy-Item 中洗掉了 -Force
更新
我又看了一遍你的問題,發現我沒有回答你所有的問題。以下腳本將識別源/目標目錄,識別兩者的檔案內容,驗證目錄是否存在,并將檔案從源復制到目標。它還將使用格式為“duplicate_name_date_time_extension”的新名稱復制重復檔案。您需要手動指定源目錄和目標目錄。
$Source = "C:\Source"
$Destination = "C:\Destination"
$Source_Content = Get-ChildItem -Path $Source -File -Recurse -Force
$Destination_Content = Get-ChildItem -Path $Destination -File -Recurse -Force
If ((Test-Path $Source) -and (Test-Path $Destination)){
Foreach ($File in $Source_Content){
$Date = Get-Date -UFormat "%d %b %Y"
$Time = Get-Date -UFormat "%H%M%S"
If ($Destination_Content.Name -contains $File.Name){
Write-Output "DUPLICATE:
Source: $($File.FullName)
Destination: $Destination\DUPLICATE_$($File.BaseName)_$($Date)_$($Time)_$($File.Extension)"
Copy-Item $($File.FullName) -Destination "$Destination\DUPLICATE_$($File.BaseName)_$($Date)_$($Time)_$($File.Extension)"
}
Else{
Copy-Item $($File.FullName) -Destination $Destination -Verbose
}
}
}
Else{
New-Item -ItemType "Directory" -Path $Destination -Force -Verbose
}
Start-Transcript
Get-ChildItem -Path $Destination -File -Recurse -Force
Stop-Transcript
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/369172.html
