auk_experts
asked on
Powershell to move files from folder 1 to folder 2 based on hours of creation
I am looking for a script that can be scheduled to run every 15 min to identify folders in a UNC like \\fileserver\oldfolder and move the files older than 2 hours to a new UNC location like \\fileserver\newfolder.
Please advise and provide a script to it.
Thanks
Please advise and provide a script to it.
Thanks
ASKER
Thanks for responding , i got below error when i changed and executed . May be its date format what we use ?
Exception calling "ParseExact" with "3" argument(s): "String was not recognized as a valid DateTime."
At C:\Users\administrator.AUK \Desktop\m ovefolder. ps1:3 char:25
+ [datetime]::ParseExact <<<< ($_.BaseName, "yyyyMMddHHmmss", $null) -gt (Get-Date).AddMinutes(-15)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Thanks
Exception calling "ParseExact" with "3" argument(s): "String was not recognized as a valid DateTime."
At C:\Users\administrator.AUK
+ [datetime]::ParseExact <<<< ($_.BaseName, "yyyyMMddHHmmss", $null) -gt (Get-Date).AddMinutes(-15)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
With above i do not get any errors but i don't see any action also as there are some old files in the folder but they are not moved or anything done.
Oh. I read the question aggain:
Change from -15 to -240 This is 2 h
And then change the -gt (greater than) to -lt (less than)
Change from -15 to -240 This is 2 h
And then change the -gt (greater than) to -lt (less than)
ASKER
Thanks, without change the -gt it worked great...
https://community.spiceworks.com/how_to/17736-run-powershell-scripts-from-task-scheduler
Save script below as xxx.ps1
Open in new window