Script to Move Files Based on Date Modified

PiERiT

2[H]4U
Joined
Oct 8, 2010
Messages
2,589
I'm sure this is possible with PowerShell, if not a simple batch file, but I'm not sure the proper syntax.

I download a lot of... videos... and they all get dumped into a single folder. I want a script that moves files out of that folder into a different folder once they're over a month old. That's easy enough but I also want files moved out of that 'other folder' into yet another folder once those are two months old, and so on.

For example if I ran this script on 6/1 with what I currently have, everything modified in May would stay there, everything modified in April would go into an April folder, everything modified in March into March, etc. Then, if I run it again on 7/1, everything modified in May that exists in the primary folder would get moved into a May folder.

I hope that makes sense. I can create the folders, so the script doesn't need to account for that, and they can be named 1-12 rather than by month, if that makes the scripting easier, but the original folder needs to be separate and always contain the past 30 days worth of files. Any help is appreciated.
 
Last edited:

Vengance_01

Supreme [H]ardness
Joined
Dec 23, 2001
Messages
6,775
function delFilePastXDate() {
$hostname = 'Hostname'
$folder = 'Path'
Get-ChildItem $folder -Filter *.pdf | Where-Object { $_.Lastwritetime -lt (date).addDays(-180)} | Remove-Item
}
$hostname = 'hostname'
$date = Get-Date -uformat %Y%m%d-%H%M
delFilePastXDate

This will delete files, so I am guessing you could change it to move a file. Also when I used this I was looking for pdf's only so I guess you could do *.*
 

PiERiT

2[H]4U
Joined
Oct 8, 2010
Messages
2,589
I tweaked that a bit and added a -gt for the dates to make a range and ended up with this:

gci "d:\plex\other videos" -recurse | where-object {$_.lastwritetime -gt (date).adddays(-60) -and $_.lastwritetime -lt (date).adddays(-30)} | move-item -destination "d:\plex\other videos\30"
gci "d:\plex\other videos" -recurse | where-object {$_.lastwritetime -gt (date).adddays(-90) -and $_.lastwritetime -lt (date).adddays(-60)} | move-item -destination "d:\plex\other videos\60"
gci "d:\plex\other videos" -recurse | where-object {$_.lastwritetime -gt (date).adddays(-120) -and $_.lastwritetime -lt (date).adddays(-90)} | move-item -destination "d:\plex\other videos\90"
gci "d:\plex\other videos" -recurse | where-object {$_.lastwritetime -gt (date).adddays(-150) -and $_.lastwritetime -lt (date).adddays(-120)} | move-item -destination "d:\plex\other videos\120"
gci "d:\plex\other videos" -recurse | where-object {$_.lastwritetime -gt (date).adddays(-180) -and $_.lastwritetime -lt (date).adddays(-150)} | move-item -destination "d:\plex\other videos\150"
gci "d:\plex\other videos" -recurse | where-object {$_.lastwritetime -gt (date).adddays(-210) -and $_.lastwritetime -lt (date).adddays(-180)} | move-item -destination "d:\plex\other videos\180"
gci "d:\plex\other videos" -recurse | where-object {$_.lastwritetime -gt (date).adddays(-240) -and $_.lastwritetime -lt (date).adddays(-210)} | move-item -destination "d:\plex\other videos\210"
gci "d:\plex\other videos" -recurse | where-object {$_.lastwritetime -gt (date).adddays(-270) -and $_.lastwritetime -lt (date).adddays(-240)} | move-item -destination "d:\plex\other videos\240"
gci "d:\plex\other videos" -recurse | where-object {$_.lastwritetime -gt (date).adddays(-300) -and $_.lastwritetime -lt (date).adddays(-270)} | move-item -destination "d:\plex\other videos\270"
gci "d:\plex\other videos" -recurse | where-object {$_.lastwritetime -gt (date).adddays(-330) -and $_.lastwritetime -lt (date).adddays(-300)} | move-item -destination "d:\plex\other videos\300"
gci "d:\plex\other videos" -recurse | where-object {$_.lastwritetime -gt (date).adddays(-360) -and $_.lastwritetime -lt (date).adddays(-330)} | move-item -destination "d:\plex\other videos\330"
gci "d:\plex\other videos" -recurse | where-object {$_.lastwritetime -lt (date).adddays(-360)} | move-item -destination "d:\plex\other videos\360"

The last line dumping everything older than a year into the 360 folder. It could be cleaner, but it works.

Thanks!
 
Last edited:

Vengance_01

Supreme [H]ardness
Joined
Dec 23, 2001
Messages
6,775
I tweaked that a bit and added a -gt for the dates to make a range and ended up with this:

gci "d:\plex\other videos" -recurse | where-object {$_.lastwritetime -gt (date).adddays(-60) -and $_.lastwritetime -lt (date).adddays(-30)} | move-item -destination "d:\plex\other videos\30"
gci "d:\plex\other videos" -recurse | where-object {$_.lastwritetime -gt (date).adddays(-90) -and $_.lastwritetime -lt (date).adddays(-60)} | move-item -destination "d:\plex\other videos\60"
gci "d:\plex\other videos" -recurse | where-object {$_.lastwritetime -gt (date).adddays(-120) -and $_.lastwritetime -lt (date).adddays(-90)} | move-item -destination "d:\plex\other videos\90"
gci "d:\plex\other videos" -recurse | where-object {$_.lastwritetime -gt (date).adddays(-150) -and $_.lastwritetime -lt (date).adddays(-120)} | move-item -destination "d:\plex\other videos\120"
gci "d:\plex\other videos" -recurse | where-object {$_.lastwritetime -gt (date).adddays(-180) -and $_.lastwritetime -lt (date).adddays(-150)} | move-item -destination "d:\plex\other videos\150"
gci "d:\plex\other videos" -recurse | where-object {$_.lastwritetime -gt (date).adddays(-210) -and $_.lastwritetime -lt (date).adddays(-180)} | move-item -destination "d:\plex\other videos\180"
gci "d:\plex\other videos" -recurse | where-object {$_.lastwritetime -gt (date).adddays(-240) -and $_.lastwritetime -lt (date).adddays(-210)} | move-item -destination "d:\plex\other videos\210"
gci "d:\plex\other videos" -recurse | where-object {$_.lastwritetime -gt (date).adddays(-270) -and $_.lastwritetime -lt (date).adddays(-240)} | move-item -destination "d:\plex\other videos\240"
gci "d:\plex\other videos" -recurse | where-object {$_.lastwritetime -gt (date).adddays(-300) -and $_.lastwritetime -lt (date).adddays(-270)} | move-item -destination "d:\plex\other videos\270"
gci "d:\plex\other videos" -recurse | where-object {$_.lastwritetime -gt (date).adddays(-330) -and $_.lastwritetime -lt (date).adddays(-300)} | move-item -destination "d:\plex\other videos\300"
gci "d:\plex\other videos" -recurse | where-object {$_.lastwritetime -gt (date).adddays(-360) -and $_.lastwritetime -lt (date).adddays(-330)} | move-item -destination "d:\plex\other videos\330"
gci "d:\plex\other videos" -recurse | where-object {$_.lastwritetime -lt (date).adddays(-361)} | move-item -destination "d:\plex\other videos\360"

The last line dumping everything older than a year into the 360 folder. It could be cleaner, but it works.

Thanks!
Hey no problem glad something I wrote was useful to someone LOL. I am not programmer or scripting person at all. I just cobble stuff together out need when I haft too.
 
Top