![](/img/trans.png)
[英]How to copy selected files from one folder to another in Batch file?
[英]How To copy only new files from one folder to another with PowerShell
我在完成Powershell腳本時遇到了一些麻煩。 我有一個程序可以將圖像下載到一個臨時文件夾中,然后在一個小時內它將刪除圖像文件。 有時我想保留一些文件,因此我正在創建一個Powershell腳本,將圖像文件從臨時文件夾復制到新文件夾,以便我可以對它們進行排序並保留我想要的內容。
在網上搜索時,我在TomsITpro上的“如何使用PowerShell同步文件夾”上找到了一個腳本。 該腳本在兩個文件夾之間雙向復制文件。 當我嘗試將其修改為僅以一種方式復制時,出現Powershell錯誤。
$DownloadFolder = 'D:\TempImages'
$KeepFolder = 'D:\KeepImages'
$DownloadFiles = Get-ChildItem -Path $DownloadFolder
$KeepFiles = Get-ChildItem -Path $KeepFolder
$FileDiffs = Compare-Object -ReferenceObject $DownloadFiles -DifferenceObject $KeepFiles
$FileDiffs | foreach {
$copyParams = @{
'Path' = $_.InputObject.Fullname
}
if ($_.SideIndicator -eq '=>')
{
$copyParams.Destination = $KeepFolder
}
Copy-Item @copyParams
}
該腳本可以很好地進行工作並進行比較,並且僅在新文件已經存在的情況下才進行復制。 問題是當我從d:\\ script文件夾運行它時,它將所有文件復制到d:\\ script文件夾而不是目標文件夾D:\\ KeepImages。 我在這里做錯了什么?
對此Powershell腳本的任何幫助將不勝感激。
嘗試添加:Copy-Item -Force -Destination $ KeepFolder
而不是Copy-Item @copyParams
$DownloadFolder = 'D:\TempImages'
$KeepFolder = 'D:\KeepImages'
$DownloadFiles = Get-ChildItem -Path $DownloadFolder
$KeepFiles = Get-ChildItem -Path $KeepFolder
$FileDiffs = Compare-Object -ReferenceObject $DownloadFiles -DifferenceObject $KeepFiles
$FileDiffs | foreach {
$copyParams = @{
'Path' = $_.InputObject.Fullname
}
$Downloadll = $copyParams.path
if ($_.SideIndicator -eq '=>')
{
Copy-Item $Downloadll -Destination $KeepFolder
}
}
嘗試這個
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.