Skip to content

Instantly share code, notes, and snippets.

@BobbyWibowo
Last active September 10, 2022 12:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BobbyWibowo/96a4df249197634875ff63be6b3f7568 to your computer and use it in GitHub Desktop.
Save BobbyWibowo/96a4df249197634875ff63be6b3f7568 to your computer and use it in GitHub Desktop.
$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

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