Move files to specific directories based on file modified by date

Hi All,

I’m trying to make sense of 1000’s of photos and I’m looking for something a little different to the year - month standard option. I’m trying to sort my pictures between specific dates for instance.

For example:

Pics from between 05-01-2000 & 04-02-2000 to be moved to directory Year 1\Month 0
Pics from between 05-02-2000 & 04-03-2000 to be moved to directory Year 1\Month 1
Pics from between 05-03-2000 & 04-04-2000 to be moved to directory Year 1\Month 2
Pics from between 05-04-2000 & 04-05-2000 to be moved to directory Year 1\Month 3

From searching i have found the following script

[CmdletBinding(SupportsShouldProcess=$true)]
param (
    [Parameter(Mandatory=$true)][string]$SourceDirectory,
    [Parameter(Mandatory=$true)][string]$DestinationDirectory,
    [Parameter(Mandatory=$true)][string]$ModifiedAfter,
    [Parameter(Mandatory=$true)][string]$ModifiedBefore
)

Get-ChildItem -Path $SourceDirectory |
Where-Object {
    $_.LastWriteTime `
        -gt (Get-Date $ModifiedAfter) `
        -and $_.LastWriteTime -lt (Get-Date $ModifiedBefore) } |
ForEach-Object { $_ | Copy-Item -Destination $DestinationDirectory }

You run it by

.\Copy-Files-Modified-Between-Dates `
    -SourceDirectory C:\Temp\all `
    -DestinationDirectory C:\Temp\subset `
    -ModifiedAfter '2020-11-01 18:00' `
    -ModifiedBefore '2020-11-02'

My question is can this be made to increment onto the next month automatically or to reference a file for info maybe a csv or text file to say

Pics from between 05-04-2010 & 04-05-2010 to be moved to directory Year 10\Month 3

If you guys have any better recommendations that would be great to see.

thanks

James P,
Welcome to the forum. :wave:t3:

Of course. You can modify the code to whatever you like. :man_shrugging:t3:

I’d recommend to keep it as simple as possible. Why not using Years as names for the folders instead of “Year 1”, “Year 2” … ? I’d use 2 digit numbers for the months instead of “Month 0”, “Month 1” …

1 Like

Hi Olaf,

Thanks for replying, could you advise on how this can be done then please?
thanks

What exactly do you mean?

Again … in general … if you use the property LastWriteTime of the files you can access their individual subproperties by their so called dot notation.

$ExampleFile = 
Get-ChildItem -File | 
    Select-Object -First 1

$ExampleFile.LastWriteTime.Year
$ExampleFile.LastWriteTime.Month

You just have to query the desired folder for all desired files and process them in a foreach loop as needed. Get the LastWriteTime of the file, check if there already are folders for the year and the month, if not create them and move or copy the file there. If there already is a proper destination folder, move or copy the file there immediately.

BTW: You don’t have to re-invent the wheel again and again … in the vast majority of the cases you are not the very first one with a given task. So it is pretty likely that someone already shared some code you can adapt easily to your needs …

I googled it for you and found this … it looks like a good start for own experiments:

1 Like