imagepowershelltimelapsereorganizedatemodified

How to sort out images from a specific time(period) of day?


TIMELAPSE PROJECT

Over a year a new photo has been uploaded every 15 minutes from my webcam (thousands of photos)

I am looking for a simple way to
1. Pick one picture from each day, taken as close as possible to 12:00 midday
2. Then move all selected files to a subfolder

The filename format is YYYYMMDDHHMMSS (ex.20180715035717.jpg)
Date modified is also available in file details.

PS: Date modified/ LastWriteTime is -2 hours from when photo was taken

Any help would be appreciated

Screenshot of folder with files

Example: For each photo taken between 11:50 and 12:10 move to subfolder

LastWriteTime////Length////   Name

15.07.2018     08.37        2655543 20180715103707.jpg


15.07.2018     08.52        3294951 20180715105207.jpg


15.07.2018     09.07        2787347 20180715110707.jpg


15.07.2018     09.27        2632705 20180715112706.jpg


15.07.2018     09.42        2589219 20180715114206.jpg


15.07.2018     10.02        2767032 20180715120208.jpg  - MOVE TO SUBFOLDER


15.07.2018     10.27        2997687 20180715122726.jpg


15.07.2018     10.52        3071746 20180715125224.jpg


15.07.2018     11.13        2981523 20180715131308.jpg


15.07.2018     11.37        3074120 20180715133708.jpg


15.07.2018     11.57        2753426 20180715135708.jpg


15.07.2018     12.17        2575961 20180715141709.jpg


15.07.2018     12.37        2664982 20180715143710.jpg


15.07.2018     12.52        3049712 20180715145210.jpg


15.07.2018     13.16        2637337 20180715151618.jpg


15.07.2018     13.31        2755671 20180715153118.jpg


15.07.2018     13.46        2937633 20180715154618.jpg


15.07.2018     14.07        2591824 20180715160724.jpg


15.07.2018     14.22        2616955 20180715162225.jpg


15.07.2018     14.37        2632948 20180715163725.jpg


15.07.2018     14.52        2760379 20180715165225.jpg


15.07.2018     15.07        3080011 20180715170725.jpg


15.07.2018     15.27        3339818 20180715172706.jpg


15.07.2018     15.42        3136413 20180715174206.jpg


15.07.2018     15.58        3015146 20180715175806.jpg


15.07.2018     16.21        2597969 20180715182111.jpg


15.07.2018     16.41        2627375 20180715184112.jpg


15.07.2018     16.56        2685684 20180715185613.jpg


15.07.2018     17.13        2590604 20180715191313.jpg


15.07.2018     17.37        2621325 20180715193718.jpg


/// REMOVED SOME LINES


16.07.2018     06.08        1956348 20180716080822.jpg


16.07.2018     06.27        2119143 20180716082705.jpg


16.07.2018     06.47        2176428 20180716084706.jpg


16.07.2018     07.02        3243543 20180716090207.jpg


16.07.2018     07.26        2013755 20180716092649.jpg


16.07.2018     07.46        2017713 20180716094649.jpg


16.07.2018     08.01        3565675 20180716100150.jpg


16.07.2018     08.16        3578500 20180716101651.jpg


16.07.2018     08.37        3807611 20180716103705.jpg


16.07.2018     08.57        3728615 20180716105729.jpg


16.07.2018     09.12        3712588 20180716111230.jpg


16.07.2018     09.27        3747855 20180716112730.jpg


16.07.2018     09.42        3744401 20180716114230.jpg


16.07.2018     09.57        3593773 20180716115730.jpg - MOVE TO SUBFOLDER


16.07.2018     10.17        3189216 20180716121732.jpg


16.07.2018     10.33        3400749 20180716123307.jpg


16.07.2018     10.48        3501471 20180716124807.jpg

Solution

  • If you want to use the timestamps in the file names, the below code should do what you want:

    $sourceFolder = 'D:\test'
    $destination  = 'D:\PicturesAroundNoon'
    
    # if the destination path does not exist, ceate it
    if (!(Test-Path -Path $destination -PathType Container)) {
        $null = New-Item -Path $destination -ItemType Directory
    }
    # get a list of FileInfo objects for the files to move
    Get-ChildItem -Path $sourceFolder -Filter '*.jpg' -File | 
        Where-Object { $_.BaseName -match '^\d{14}$' -and $_.Name.Substring(8,2) -match '11|12' } | 
        Group-Object @{Expression = {$_.BaseName.Substring(0,8)}} | 
        ForEach-Object {
            if ($_.Group.Count -eq 1) { $fileToMove = $_.Group[0] }  # return the only file for thatdate
            else {
                if ($_.Group.Count -eq 2) {
                    $index = 0
                }
                else {
                    $index = [math]::Floor($_.Group.Count / 2)
                }
                # get the middle two files from the collection and test which is closest to 12:00
                $file1, $file2 = ($_.Group | Sort-Object)[$index..($index + 1)]
                # get the dates from the file BaseName property
                $date1 = [datetime]::ParseExact($file1.BaseName, 'yyyyMMddHHmmss', $null)
                $date2 = [datetime]::ParseExact($file2.BaseName, 'yyyyMMddHHmmss', $null)
                $noon  = Get-Date -Year $date1.Year -Month $date1.Month -Day $date1.Day -Hour 12 -Minute 0 -Second 0
                # get the time distance to 12:00 in seconds
                $dif1 = [math]::Abs(($date1 - $noon).TotalSeconds)
                $dif2 = [math]::Abs(($date2 - $noon).TotalSeconds)
                $fileToMove = if ($dif1 -lt $dif2) { $file1 } else { $file2 }
            }
            # now move the file to its destination
            $fileToMove | Move-Item -Destination $destination -WhatIf
        }
    

    Remove the -WhatIf if you are satisfied with the results shown in the console