I am trying to copy over files from a source folder to a destination folder. I would like to only copy files that have been modified in the last 20 mins. While I am copying I would also like to append the date and time to the end of the file name. The script I currently have is:
$DestinationFolder = "C:\Output\"
$timespan = new-timespan -minutes 20
$Files = Get-ChildItem "C:\Input\*" -File
foreach ($File in $Files) {
if ($File.LastWriteTime -gt $timespan)
{
Copy-Item -Path $_.FullName -Destination $DestinationFolder$($_.BaseName)_$ ($_.LastWriteTime.ToString('yyyyMMdd_hhmmss'))$($_.Extension)
}
}
I am getting error messages in powershell when I attempt to test my scipt:
Could not compare "07/21/2017 07:31:01" to "00:20:00". Error: "Cannot convert the "00:20:00" value of type "System.TimeSpan" to type "System.DateTime"." At line:2 char:9 + if ($File.LastWriteTime -gt $timespan) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : ComparisonFailure