如何僅將新檔案從一個資料夾複製到另一個資料夾,並且僅複製 5 分鐘前新增的檔案?

如何僅將新檔案從一個資料夾複製到另一個資料夾,並且僅複製 5 分鐘前新增的檔案?

我想將檔案從一個驅動器複製到另一個驅動器,但我只想複製比目標檔案新的來源檔案。但是,我也想只複製過去 5 分鐘內更改過的來源檔案。

我建立了一個擴展名為 .bat 的檔案並使用了以下命令:

xcopy "C:\Users\Pictures\Newfolder"  "C:\Users\Pictures\Newfolder2\" /d:03/29/2019

但這個命令是針對日期的。

答案1

這個 PowerShell 腳本:

(Get-ChildItem 'C:\Users\Pictures\Newfolder' |
    Where {!($_.PSIsContainer) -and
           ($_.LastWriteTime -gt [datetime]::now.AddMinutes(-5)) -and 
           (!(Test-Path (Join-Path 'C:\Users\Pictures\Newfolder2' $_.Name)))}).FullName"

包裝在批次檔中:

:: Q:\Test\2019\03\29\SU_1419009.cmd
@Echo off
Set "Src=C:\Users\Pictures\Newfolder"  
Set "Dst=C:\Users\Pictures\Newfolder2"

For /f "usebackq delims=" %%A in (`
    powershell -NoP -C "(Get-ChildItem '%Src%'|Where {!($_.PSIsContainer) -and ($_.LastWriteTime -gt [datetime]::now.AddMinutes(-5)) -and (!(Test-Path (Join-Path '%Dst%' $_.Name)))}).FullName"
`) Do (
echo Copy "%%A" "%Dst%\"
     Copy "%%A" "%Dst%\"
)

因為這只會複製過去 5 分鐘內創建/更改的文件,如果我弄錯了,
您的意思是超過 5 分鐘的文件將更-gt改為-lt.

相關內容