Based on this "(Get-ChildItem $source -Filter .| Sort LastAccessTime -Descending)[0]" you only ever expect to copy 1 file per day. And it sounds like the problem is that the script copies a file over even when no new files have been added to $source. Hopefully I have that right.
Maybe you can add a filter like below, assuming your file is added on a regular schedule
(Get-ChildItem $source -Filter .| Sort LastAccessTime -Descending | ? {$_.LastAccessTime -gt $(get-date).AddDays(-1))[0] #may want to use LastWriteTime or CreationTime in place of LastAccessTime. Can also fiddle with .AddDays - .AddMinutes, .AddHours, etc.
Alternatively you can check your $bkp folder to see if the file exists before copying it:
@(Get-ChildItem $source -Filter *.*| Sort LastAccessTime -Descending)[0] | % {
#check if file exists in $bkp before copying from $source
#"$($_.name)*" part tries to account for the file in $bkp having a timestamp appended to the name
$x = get-childitem $bkp -recurse | ? {$_.name -like "$($_.name)*"}
if(!$x){
Copy-Item -path $_.FullName -destination $("$bkp$_$timestamp") -force
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…