Link to home
Start Free TrialLog in
Avatar of auk_experts
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
Avatar of Benjamin Voglar
Benjamin Voglar
Flag of Slovenia image

How to scedule powershell script :

https://community.spiceworks.com/how_to/17736-run-powershell-scripts-from-task-scheduler

Save script below as xxx.ps1


Get-ChildItem -Path "\\share\folder" |
Where-Object {
  [datetime]::ParseExact($_.BaseName, "yyyyMMddHHmmss", $null) -gt (Get-Date).AddMinutes(-15)
} |
Move-Item -Destination "\\shre\folder2"

Open in new window

Avatar of auk_experts
auk_experts

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\movefolder.ps1:3 char:25
+   [datetime]::ParseExact <<<< ($_.BaseName, "yyyyMMddHHmmss", $null) -gt (Get-Date).AddMinutes(-15)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Benjamin Voglar
Benjamin Voglar
Flag of Slovenia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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)
Thanks, without change the -gt it worked  great...