Last active
May 8, 2026 15:48
-
-
Save Polda18/ce83cd80e71eae960f04419c2fff2e6c to your computer and use it in GitHub Desktop.
Sorting script for sorting my GlobalSec dashcam videos automatically by date.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env powershell | |
| # --------------------------------------------------------------------------- | |
| # Script for sorting dashcam videos, using Powershell on Windows and on Linux | |
| # (provided you have Powershell installed) | |
| # Made by Polda18 | |
| # --------------------------------------------------------------------------- | |
| # I made this script to sort my videos from my GlobalSec dashcam, so I can | |
| # find specific clips from a specific day easily on my hard drive. I had to | |
| # sort them manually before writing this script and it was painfully slow. | |
| # Now I can sort them automatically and it's blazing fast. I just run this | |
| # script and all of the videos get sorted blazingly fast. | |
| # --------------------------------------------------------------------------- | |
| # How to use: | |
| # 1. Place this script in a directory where you want to sort your dashcam | |
| # videos. | |
| # 2. Make a subdirectory where you place your videos to be sorted, e.g. | |
| # '!Sort', I just put an exclamation mark there so it's actually sorted | |
| # highest before all the date sorted directories on Windows. | |
| # 3. Run this script from the directory that contains it and that contains | |
| # the subdirectory containing all of the unsorted videos. | |
| # Replace the '!Sort' with the name of your subdirectory holding unsorted | |
| # videos. (I put an exclamation mark at the start because it makes it show | |
| # up before the subdirectories containing sorted videos on Windows, which | |
| # I use, you don't have to use it, it's just my preference for convenience) | |
| $videos = Get-ChildItem '.\!Sort' | |
| # Loop through the files inside that directory and sort them | |
| foreach ($v in $videos) { | |
| # Save the filename of each video file for later | |
| $vname = $v.Name | |
| # Regex pattern to look for to sort the videos by date | |
| # and the replacement format for an ISO date string | |
| # ----------------------------------------------------------- | |
| # Tweak the regex for whatever naming scheme your dashcam | |
| # uses, provided it uses some sort of date&time format in | |
| # the file name. For other videos like that, you should | |
| # use other means to extract the date of the file. I found | |
| # these methods unreliable in case my dashcam doesn't sync | |
| # the time properly and it ends up saving the file in the | |
| # past, which would require me to edit the metadata of the | |
| # file instead of just the file name itself, to correct the | |
| # mistake. Since it makes the date&time stamp part of the | |
| # filename, it's easier to simply just rename the file and | |
| # use that for date sorting. To correct the actual date of | |
| # the file, I'd have to use a Commander that supports it. | |
| # For me, that's Total Commander, which I found to be easiest | |
| # to use, and which also works well on Windows 9x even with | |
| # the latest version available (I use it in my Windows 98 VM). | |
| $regex = '^(\d{4})(\d{2})(\d{2})' | |
| $subst = '$1-$2-$3' | |
| # Test if the filename stats with the date. | |
| if ($vname -match $regex) { | |
| # Let's format it nicely into a standard ISO date string. | |
| $vdate = $matches[0] -replace $regex, $subst | |
| } else { | |
| # Fallback to a zero date in case of failure. That way | |
| # files that do not contain the date in the name get sorted | |
| # into something you can retrieve them from, so you won't | |
| # lose anything. Optionally you may also replace this line | |
| # with "continue", it just means files that do not adhere | |
| # to the naming format will simply just remain in the unsorted | |
| # directory, as the script will skip them. | |
| $vdate = '0000-00-00' | |
| } | |
| # Create a new directory with the formatted ISO date as their name | |
| # if it doesn't exist already. | |
| if (-not (Test-Path -Path ".\$vdate")) { | |
| New-Item -ItemType "Directory" -Name $vdate | |
| } | |
| # Finally move the video file to its date directory. | |
| Move-Item -Path $v -Destination ".\$vdate\$vname" | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # --------------------------------------------------------------------------- | |
| # Script for sorting dashcam videos, rewritten from Powershell into Bash | |
| # for non-Windows systems utiliting bash as their default scripting system | |
| # in case powershell is not installed | |
| # Made by Polda18 | |
| # --------------------------------------------------------------------------- | |
| # I made this script to sort my videos from my GlobalSec dashcam, so I can | |
| # find specific clips from a specific day easily on my hard drive. I had to | |
| # sort them manually before writing this script and it was painfully slow. | |
| # Now I can sort them automatically and it's blazing fast. I just run this | |
| # script and all of the videos get sorted blazingly fast. | |
| # Replace the '!Sort' with the name of your subdirectory holding unsorted | |
| # videos. (I put an exclamation mark at the start because it makes it show | |
| # up before the subdirectories containing sorted videos on Windows, which | |
| # I use, you don't have to use it, it's just my preference for convenience) | |
| sortdir='./!Sort' | |
| videos=$(ls "$sortdir") | |
| # Loop through the files inside that directory and sort them | |
| for v in $videos ; do | |
| # Regex pattern to look for to sort the videos by date | |
| # and the replacement format for an ISO date string | |
| # ----------------------------------------------------------- | |
| # Tweak the regex for whatever naming scheme your dashcam | |
| # uses, provided it uses some sort of date&time format in | |
| # the file name. For other videos like that, you should | |
| # use other means to extract the date of the file. I found | |
| # these methods unreliable in case my dashcam doesn't sync | |
| # the time properly and it ends up saving the file in the | |
| # past, which would require me to edit the metadata of the | |
| # file instead of just the file name itself, to correct the | |
| # mistake. Since it makes the date&time stamp part of the | |
| # filename, it's easier to simply just rename the file and | |
| # use that for date sorting. To correct the actual date of | |
| # the file, I'd have to use a Commander that supports it. | |
| # For me, that's Total Commander, which I found to be easiest | |
| # to use, and which also works well on Windows 9x even with | |
| # the latest version available (I use it in my Windows 98 VM). | |
| regex='^([0-9]{4})([0-9]{2})([0-9]{2})' | |
| #subst = '$1-$2-$3' # Not needed in Bash | |
| # Test if the filename stats with the date. | |
| if [[ "$v" =~ $regex ]]; then | |
| # Let's format it nicely into a standard ISO date string. | |
| vdate="${BASH_REMATCH[1]}-${BASH_REMATCH[2]}-${BASH_REMATCH[3]}" | |
| else | |
| # Fallback to a zero date in case of failure. That way | |
| # files that do not contain the date in the name get sorted | |
| # into something you can retrieve them from, so you won't | |
| # lose anything. Optionally you may also replace this line | |
| # with "continue", it just means files that do not adhere | |
| # to the naming format will simply just remain in the unsorted | |
| # directory, as the script will skip them. | |
| vdate='0000-00-00' | |
| fi | |
| # Create a new directory with the formatted ISO date as their name | |
| # if it doesn't exist already. | |
| if [[ ! -d "$vdate" ]]; then | |
| mkdir "$vdate" | |
| fi | |
| # Finally move the video file to its date directory. | |
| mv "$sortdir/$v" "./$vdate/" | |
| done |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Alright, I think it's ready. Both the Powershell and the Bash scripts are working as intended now, for some reason Bash doesn't recognize the
\dfor digits (an alias to[0-9]), so I have to use[0-9]explicitly. Bash also requires that there are no spaces around the equals sign at the variable assignments, which is something I didn't know as well. There's always something new to learn, things just come up at some point. So this 3rd revision should be now final. Hopefully, I won't have to amend any more bug fixes.Again, this script with filename regex pattern matching will only work in case your dashcam filenames include the dates of the clips. Otherwise you'd have to match against the file creation/update timestamp. Just tweak the regex to match the date format used in the filenames. For my GlobalSec dashcam, the filename is
YYYYMMDDHHMMSS_XXXXXX.MP4, which is full year, month, day, hour, minute, second, followed by an incremental integer identification of the clip on the SD card (it resets after every wipe), starting with000001, and obviously a.MP4extension for the MP4 container format. To extract the date in the correct ISO string format, I match against 3 matching groups and separate them with dashes so it forms the usualYYYY-MM-DDformat.In case any more bug fixes and revisions are necessary, I'll keep you informed.