Share via


Copy files modified in the last 5 min with Powershell

Question

Thursday, March 10, 2016 8:09 PM

My case is as below

 

I am having 

 Source Folder  = E:\AutomationScripts\Framework\Error\Latest File

Target Folder = E:\AutomationScripts\Framework\Error\Updated File

 

I want to copy all updated files in last 5 min from source folder to target folder

M using the code below:

Get-ChildItem -Path E:\AutomationScripts\Framework\Error\Latest File -Include * |
Where-Object {
  [datetime]::ParseExact($_.BaseName, "yyyyMMddHHmm", $null) -gt (Get-Date).AddMinutes(-5)
} |
Move-Item -Destination E:\AutomationScripts\Framework\Error\Updated File

But it is not working. My Updated File folder is empty always.



Thanks in advance

All replies (3)

Thursday, March 10, 2016 8:25 PM ✅Answered | 2 votes

dates are objects.  Don't look at them like strings.

$_.LastWriteTIme -gt [datetime]::Now.AddMinutes(-5)

\(ツ)_/


Friday, March 11, 2016 9:27 AM ✅Answered

Hi,

Your post mentioned that you'd like to copy the files to the target folder, so I would suggest you to alter the Move-Item to Copy-Item:

$Sourcefolder= "E:\AutomationScripts\Framework\Error\Latest File "
$Targetfolder= "E:\AutomationScripts\Framework\Error\Updated File"
Get-ChildItem -Path $Sourcefolder -Recurse|
Where-Object {
  $_.LastWriteTime -gt [datetime]::Now.AddMinutes(-5)
}| Copy-Item -Destination $Targetfolder

Hope it helps.

Best Regards,

Elaine

Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact [email protected].


Thursday, March 10, 2016 8:38 PM

Thnx JRV.........So my code need to be

Get-ChildItem -Path E:\AutomationScripts\Framework\Error\Latest File -Include * |
Where-Object {
 $_.LastWriteTIme -gt [datetime]::Now.AddMinutes(-5)
} |
Move-Item -Destination E:\AutomationScripts\Framework\Error\Updated File