2017-08-10 57 views
0

我有一個文件夾已滿,我們稱之爲文件夾A,其中一些(但不是全部)也存在於另一個文件夾中,稱爲文件夾B.Powershell腳本只複製目標文件夾中已存在的文件

B中的文件已過時,我想將這些文件的較新版本從A複製到B(覆蓋B中的文件),但不復制A中尚不存在的所有額外文件在B.

也有可能成爲B不在A.

有沒有辦法使用PowerShell做這個文件?我知道我可以用xcopy這樣做,如this question,但我正在尋找一個純Powershell解決方案。

我不在乎,如果文件是新的,舊的或不變等

回答

1

這是比較直接通過A中的文件,循環和檢查,如果他們在B.

$aDir = "C:\Temp\powershell\a" 
$bDir = "C:\Temp\powershell\b" 

$aFiles = Get-ChildItem -Path "$aDir" 
ForEach ($file in $aFiles) { 
    if(Test-Path $bDir\$file) { 
     Write-Output "$file exists in $bDir. Copying." 
     Copy-Item $aDir\$file $bDir 
    } else { 
     Write-Output "$file does not exist in $bDir." 
    } 
} 
+0

不該如果(Test-Path(Join-Path $ bDir $ file.Name))''和'更好' – LotPings

相關問題