Powershell grouping files that created within n minutes

I’m aiming to merge video files that created by a dashcam.

While driving, the camera writes a new file at every n minutes. If n=3, 30 minutes of driving generates 10 files that “date created” attribute within 3 minutes.

An example folder content:

Filename Date Created Group
1.ts 2024-09-03 19:00 Group 1
2.ts 2024-09-03 19:03 Group 1
3.ts 2024-09-03 19:06 Group 1
4.ts 2024-09-03 19:20 Group 2
5.ts 2024-09-03 19:23 Group 2
6.ts 2024-09-03 19:40 Group 3
7.ts 2024-09-03 19:44 Group 4

Desired output (in a loop probably):

1.ts, 2.ts, 3.ts    
4.ts, 5.ts    
6.ts    
7.ts

How can I get get 4 groups of filenames from this input in Powershell? I then will combine these group of files to a single video per group so I have a single video per drive session.
(Transport Stream files are directly concenateable without processing with ffmpeg etc. so I can use a simple copy /b "input files" output.ts)

Thanks

Here you go. You can run the script by copying the code in a text editor, save the file as yourname.ps1 drap&drop it into a powershell window and append your parameter at the end before starting it with enter e.g:
myscriptname.ps1 “pathtomyfolder” 4

You can copy the path to the folder via right click on it and “copy adress”.
the timegap parameter defaults to 3. the video appending i coould not test, for that I would need some sample ts files (joke). U can build that in in the end

param(
[string]$targetFolder,
[int]$timegap=3
)

# Define the main list to hold all the drive collections with titles
    $listOfLists = @()
    
    # Get all files in the folder, ordered by creation time (oldest first)
    $files = Get-ChildItem -Path $targetFolder | Sort-Object CreationTime
    
    # Initialize variables
    $previousFile = $null
    $driveCollection = @()
    $collectionNumber = 1
    
    foreach ($file in $files) {
        if ($previousFile) {
            # Calculate the time difference in minutes between the current and previous file
            $timeDifference = ($file.CreationTime - $previousFile.CreationTime).TotalMinutes
    
            # If the time difference is more than 3 minutes, start a new drive collection
            if ($timeDifference -gt $timegap) {
                # Add the current drive collection to the list of lists with a title (if not empty)
                if ($driveCollection.Count -gt 0) {
                    $title = "Collection $collectionNumber"
                    $listOfLists += $title
                    $listOfLists += [System.Collections.ArrayList]$driveCollection.Clone()
                    $collectionNumber++
                }
                # Start a new drive collection
                $driveCollection.Clear()
            }
        }
    
        # Add the current file with its creation time to the current drive collection
        $driveCollection += "$($file.Name) - Created: $($file.CreationTime)"
    
        # Set the current file as the previous file for the next iteration
        $previousFile = $file
    }
    
    # Add the last drive collection to the list of lists with a title (if not empty)
    if ($driveCollection.Count -gt 0) {
        $title = "Collection $collectionNumber"
        $listOfLists += $title
        $listOfLists += [System.Collections.ArrayList]$driveCollection.Clone()
    }

# Output the list of lists for verification

$listOfLists

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật