Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Move your camera roll pictures in folders, i've liked year / year-month with month having leading 0 (originally at https://www.thomasmaurer.ch/2015/03/move-files-to-folder-sorted-by-year-and-month-with-powershell/)
$TagLib = "C:\Program Files\PackageManagement\NuGet\Packages\TagLibSharp.2.2.0\lib\netstandard2.0\TagLibSharp.dll"
Add-Type -Path $TagLib
[System.Reflection.Assembly]::LoadFile($TagLib)
# Get the files which should be moved, without folders
$files = Get-ChildItem 'X:\OneDrive\Pictures' -Recurse | Where-Object { !$_.PSIsContainer }
# List Files which will be moved
$files
# Target Filder where files should be moved to. The script will automatically create a folder for the year and month.
$targetPath = 'X:\OneDrive\Photos'
foreach ($file in $files) {
$tag = [TagLib.File]::Create($file)
# Get year and Month of the file
if ($tag.ImageTag.DateTime) {
$year = $tag.ImageTag.DateTime.Year.ToString()
# add 0 prefix to month
$month = $tag.ImageTag.DateTime.Month.ToString("00")
}
elseif ($tag.Tag.DateTime) {
$year = $tag.Tag.DateTime.Year.ToString()
# add 0 prefix to month
$month = $tag.Tag.DateTime.Month.ToString("00")
}
else {
# I used LastWriteTime since this are synced files and the creation day will be the date when it was synced
$year = $file.LastWriteTime.Year.ToString()
# add 0 prefix to month
$month = $file.LastWriteTime.Month.ToString("00")
}
# Out FileName, year and month
$file.Name
$year
$month
# Set Directory Path , change as wanted
$Directory = $targetPath + "\" + $year + "\" + $year + "-" + $month
# Create directory if it doesn't exists
if (!(Test-Path $Directory)) {
New-Item $directory -type directory
}
# Move File to new location, remove -Force if you might have files with the same name, i've used it since i knew that duplicates are ok to overwrite
$file | Move-Item -Destination $Directory -Force
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment