One drive Camera Roll – folder for each month

If you use one drive to backup your photos and videos from your phone you might have noticed it puts all of the files in one folder per year.

I find it much easier to put one folder for month, this powershell will move all files into subfolders based on the month in the file name.

ie: a photo take on December 25th 2020 will be named, 20201225??????????????.

So the month is assumed to be the 4th and 5th characters of the filename, any files that dont have numbers in that position are left where they are. and yes I should test for months greater than 12 but didnt bother.

$filespath = "$env:onedrive\pictures\camera roll\2020"
$ff=get-childitem -Path $filespath -File 
foreach ( $f in $ff ) { 
    #$f
    if ( $f.name.substring(4,2) -match '[0-9][0-9]' ) { 
        $newpath=$f.Directoryname + "\" + $f.name.Substring(4,2)
        write-host "Moving file $f.name to $newpath" -ForegroundColor DarkYellow
        if (!(test-path -path $newpath)) {new-item -path $newpath -itemtype directory  }
        Move-Item $f.fullname $newpath 
    }
    else {
        "No match $f.name"
    }
}

About Jeff Turner

Technical director of Nano Tera Network Solutions.
This entry was posted in Hints and Tips, Powershell, VBS, VBA and other scripting.. Bookmark the permalink.