Powershell is great (as am I) for automating boring little tasks.

So the question came to put 18 thousand plus image files that were in one folder into folders per year of Date created.

So to no longer keep you in suspense, here is the complete code

PowerShell
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$SourceDir = ""
$DestinationDir = ""
 
Write-Host "Source directory: " + $SourceDir
Write-Host "Destination directory:" + $DestinationDir
 
$files = get-childitem $SourceDir *.* -File |Select-Object -last 10
 
Write-Host "Number of files to move:" $files.Count
foreach($file in $files)
{
    $Directory = $DestinationDir + "\" + $file.LastWriteTime.Date.ToString("yyyy")
    if(!(Test-Path $Directory))
    {
        Write-Host "Creating directory"
        New-Item $Directory -type directory
    }
    Write-Host "To directory:" $Directory
    Write-Host "Moving file:" $file.ToString()
    Move-Item $file.fullname $Directory
    Write-Host "File moved"
} 
$SourceDir = ""
$DestinationDir = ""

Write-Host "Source directory: " + $SourceDir
Write-Host "Destination directory:" + $DestinationDir

$files = get-childitem $SourceDir *.* -File |Select-Object -last 10

Write-Host "Number of files to move:" $files.Count
foreach($file in $files)
{
    $Directory = $DestinationDir + "\" + $file.LastWriteTime.Date.ToString("yyyy")
    if(!(Test-Path $Directory))
    {
        Write-Host "Creating directory"
        New-Item $Directory -type directory
    }
    Write-Host "To directory:" $Directory
    Write-Host "Moving file:" $file.ToString()
    Move-Item $file.fullname $Directory
    Write-Host "File moved"
} 

This only takes the last 10 files and it uses the LastWriteTime but you get the drift.

Just fill in the Source and Destination Directories and you’re good to go.

Most important parts.

PowerShell
1
$files = get-childitem $SourceDir *.* -File |Select-Object -last 10
$files = get-childitem $SourceDir *.* -File |Select-Object -last 10

Gets the files form the sourcedirectory and selects the last 10

PowerShell
1
2
3
4
5
6
$Directory = $DestinationDir + "\" + $file.LastWriteTime.Date.ToString("yyyy")
    if(!(Test-Path $Directory))
    {
        Write-Host "Creating directory"
        New-Item $Directory -type directory
    }
$Directory = $DestinationDir + "\" + $file.LastWriteTime.Date.ToString("yyyy")
    if(!(Test-Path $Directory))
    {
        Write-Host "Creating directory"
        New-Item $Directory -type directory
    }

Makes a new directory per year if it not already exists.

PowerShell
1
Move-Item $file.fullname $Directory
Move-Item $file.fullname $Directory

Moves the file.

Simple and quick. All 18k files moved in less than a minute.