Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
$Dirs =
"E:\Pictures\Screenshots",
"E:\Pictures\Steam Screenshots",
"E:\Videos\Recordings\GeForce Experience\Autodesk Flow Design",
"E:\Videos\Recordings\GeForce Experience\Autodesk Flow Design\FFBatch",
"E:\Videos\Recordings\GeForce Experience\Genshin Impact",
"E:\Videos\Recordings\GeForce Experience\Genshin Impact\FFBatch",
"E:\Pictures\bh3rd",
"E:\Pictures\Genshin Impact Screenshots"
$TargetSubDir = "old"
$Age = 14 # days
ForEach ($Dir in $Dirs) {
Write-Output "Directory: $Dir"
Write-Output "Target directory: $($Dir)\$TargetSubDir"
$TargetSubDirIsCont = Test-Path -Path "$($Dir)\$TargetSubDir" -PathType Container
If (!$TargetSubDirIsCont) {
If (Test-Path -Path "$($Dir)\$TargetSubDir" -PathType Leaf) {
Write-Output "Target directory is somehow a generic file, deleting..."
Remove-Item "$($Dir)\$TargetSubDir"
}
Write-Output "Creating target directory..."
New-Item -Path "$($Dir)\$TargetSubDir" -ItemType Directory
}
$Files = Get-ChildItem -Attributes !Directory+!System "$Dir"
$FilesCount = ($Files | Measure-Object).Count
Write-Output "Found $FilesCount generic file(s) in directory."
If ($FilesCount -eq 0) {
Write-Output ""
Continue
}
$Count = 0
ForEach ($File in $Files) {
If ($File.LastWriteTime -lt (Get-Date).AddDays(-$Age).Date) {
Write-Output "$($File.Name) is $Age day(s) old, moving..."
Move-Item -Path $File.FullName -Destination "$($Dir)\$TargetSubDir"
$Count++
}
}
If ($Count) {
Write-Output "Successfully moved $Count file(s) to target directory.`n"
} Else {
Write-Output "No files older than $Age day(s).`n"
}
}
@BobbyWibowo
Copy link
Author

BobbyWibowo commented Aug 20, 2022

PowerShell script, designed to be periodically ran by Task Scheduler, to move older files on multiple directories into their respective sub-directories

Windows Explorer has rather awful performance when listing directories with hundreds of files, especially on HDD, if on custom "Sort by" setting (on my case specifically, Date / Descending)

So we just move much older files into a sub-directory to ensure Windows Explorer will mainly be sorting through tens of files or so (screenshots directories tend to get filled quickly)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment