Assuming that you are using this to arrange photos into folders, and that your file names start with YYYYMMDD, then the following script works well. This method was preferred to the method above because we copied/pasted/moved and hence the creation and modified dates were not the same as the dates taken.
Also, to run this script, just paste into a text file with an extension .ps1, right click on the file, and select run with powershell.
# Get the files which should be moved, without folders
$files = Get-ChildItem 'C:\Users\YourName\SourceFolder\YourPhotosFolder' -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\YourName\SourceFolder\YourAlbumnsFolder'
foreach ($file in $files)
{
# Get year and Month of the file using the filename
$bn = $file.basename.ToString()
$year = $bn.substring(0,4)
$month = $bn.substring(4,2)
$day = $bn.substring(6,2)
# Out FileName, year and month
$file.Name
$year
$month
$day
# Set Directory Path
$Directory = $targetPath + "\" + $year + "\" + $day + "-" + $month + "-" + $year
# 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
}
If you want to use the last modified date then use the following (slightly modified version of the previous code)
# Get the files which should be moved, without folders
$files = Get-ChildItem 'C:\Users\YourName\SourceFolder\YourPhotosFolder' -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\YourName\SourceFolder\YourAlbumnsFolder'
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()
$day = $file.LastWriteTime.Day.ToString()
# Out FileName, year and month
$file.Name
$year
$month
$day
# Set Directory Path
$Directory = $targetPath + "\" + $year + "\" + $day + "-" + $month + "-" + $year
# 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
}
If you want to use the created date then use the following (slightly modified version of the previous code)
# Get the files which should be moved, without folders
$files = Get-ChildItem 'C:\Users\YourName\SourceFolder\YourPhotosFolder' -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\YourName\SourceFolder\YourAlbumnsFolder'
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.CreationTime.Year.ToString()
$month = $file.CreationTime.Month.ToString()
$day = $file.CreationTime.Day.ToString()
$hour = $file.CreationTime.Hour.ToString()
# Out FileName, year and month
$file.Name
$year
$month
$day
# Set Directory Path
$Directory = $targetPath + "\" + $year + "\" + $day + "-" + $month + "-" + $year
# 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
}
Entire project code on GitHub