Last active
September 10, 2022 12:27
-
-
Save BobbyWibowo/96a4df249197634875ff63be6b3f7568 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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)