Last active
January 27, 2022 15:57
-
-
Save yphastos/9f781ba1f4f63109e1ea205854c382dc to your computer and use it in GitHub Desktop.
Sort files in folders by year/month
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
<# | |
Original from: | |
https://www.thomasmaurer.ch/2015/03/move-files-to-folder-sorted-by-year-and-month-with-powershell/ | |
Sort files inside a folder and move them to YYYY/MM folders. | |
#> | |
# Get the files which should be moved, without folders | |
$files = Get-ChildItem 'C:\Users\Thomas\OneDrive\OneDrive Camera Roll' -Recurse | where {!$_.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 = 'C:\Users\Thomas\OneDrive\pictures\Albums' | |
foreach ($file in $files) | |
{ | |
# Get year and Month of the file | |
# 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() | |
$month = $file.LastWriteTime.Month.ToString() | |
# Out FileName, year and month | |
$file.Name | |
$year | |
$month | |
# Set Directory Path | |
$Directory = $targetPath + "\" + $year + "\" + $month | |
# Create directory if it doesn't exsist | |
if (!(Test-Path $Directory)) | |
{ | |
New-Item $directory -type directory | |
} | |
# Move File to new location | |
$file | Move-Item -Destination $Directory | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice and clean! Not like other similar tools which require to install tons of dependencies (one even required Visual C++ 14.0...)