Create folders and move files based on file creation date (month and year)

She who must be obeyed (my wife) has thousands of photos stored in a single pictures folder. She now realizes there is a better way and to get there would like to rationalize all the existing photos into individual folders based on photo creation date (yyyy-MM). She tells me the fix should be simple, so…

I have found the following script that should do what I want (but doesn’t):

Blockquote
$rootpath = ‘C:\Users\ABC\Pictures’
$files = Get-ChildItem $rootpath -File
foreach($file in $files) {
$foldername = $file.CreationTime.ToString(‘yyyy-MM’)
$topath = “$rootpath$foldername”
if(-not(Test-Path $topath -PathType Container)){
New-Item $topath -ItemType Directory
}
Move-Item $file.FullName $topath
} > Blockquote

The problem is that I have test run the scipt in different root folders with the same results, e.g., that only 2 subfolders are generated 2024-2, 2024-3, with pictures move into those folders.

I am stuck. Any help would be appreciated.

hip

Add a ‘-Recurse’ at the end of your Get-ChildItem cmdlet

$files = Get-ChildItem $rootpath -File
# should be
$files = Get-ChildItem $rootpath -File -Recurse
2 Likes

Work perfectly!

Thanks for the help.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.