Skip to content

Instantly share code, notes, and snippets.

@chrisbrownie
Created June 15, 2024 00:06
Show Gist options
  • Save chrisbrownie/6143a61da556ba606ec7c250e280df73 to your computer and use it in GitHub Desktop.
Save chrisbrownie/6143a61da556ba606ec7c250e280df73 to your computer and use it in GitHub Desktop.
Clean up images in a folder, moving them to subfolders named YYYY-MM based on the image's creation date.
$screenshotsDir = "$($env:userprofile)\OneDrive\Pictures\Screenshots"
$fileType = "png"
$images = Get-ChildItem -Path $screenshotsDir -filter "*.$fileType"
for ($i=0; $i -lt $images.Length; $i++) {
$monthYearString = $images[$i].CreationTime.Year.ToString() + "-" + $images[$i].CreationTime.Month.ToString().PadLeft(2,"0")
$folder = Join-Path $screenshotsDir $monthYearString
if (-not (Test-Path -Path $folder -PathType Container)) {
# Creating path
New-Item -Path $folder -ItemType Directory
}
Move-Item $images[$i].FullName $folder
}
@chrisbrownie
Copy link
Author

Or this one for Dropbox\Camera Uploads:

$years = 2012..2024
$months = 1..12

foreach ($year in $years) {
    foreach ($month in $months) {
        $monthnice = $month.ToString().PadLeft(2,"0")
        $str = "$year-$monthnice"
        if (-not (Test-Path -Path $str -PathType Container)) { New-Item -Path $str -ItemType Directory }
        Get-ChildItem "$str*" -File | % { Move-Item $_ $str }
    }
}

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