Need a bash script to copy surveillance videos to their corresponding “date” folders.
The file name is
E1P3-20241026221904-20241026221950.mp4
where the yyyymmdd is in the middle of it. Don’t need the remaining time stamp.
This example is 2024/10/26
The dash is a common delimiter in all of the files.
I want to copy these from /MacHD/Users/dude/secvideo/E1P3-20241026221904-20241026221950.mp4 to /Volumes/coldstore/Video/_yyyy/mm/dd or /Volumes/coldstore/Video/_2024/10/26/E1P3-20241026221904-20241026221950.mp4 in this example.
Not sure how to extract the yyyymmdd from the file name then direct the ditto command to have the file land in /_yyyy/mm/dd folder. The script won’t be ran on the day of the video so I can’t just break up the date command.
I don’t speak bash, this is an AppleScript which extracts the significant information with text item delimiters
set sourcePath to "/MacHD/Users/dude/secvideo/E1P3-20241026221904-20241026221950.mp4"
set {saveTID, text item delimiters} to {text item delimiters, {"/"}}
set fileName to last text item of sourcePath
set text item delimiters to {"-"}
set dateString to item 2 of text items of sourcePath
set text item delimiters to saveTID
tell dateString to set {yr, mt, dy} to {text 1 thru 4, text 5 thru 6, text 7 thru 8}
set dateFolderPath to "_" & yr & "/" & mt & "/" & dy & "/" & fileName
set destinationPath to "/Volumes/coldstore/Video/" & dateFolderPath
do shell script "/usr/bin/ditto " & quoted form of sourcePath & space & quoted form of destinationPath
use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"
use scripting additions
set sourceFolderPath to "/MacHD/Users/dude/secvideo/" -- Shouldn't this begin with "/Users/"?
set sourcePath to sourceFolderPath & "E1P3-20241026221904-20241026221950.mp4"
set sourcePath to current application's NSString's stringWithString:(sourcePath)
set regexPattern to "^" & sourceFolderPath & "([[:alnum:]]++-(\\d{4})(\\d{2})(\\d{2})\\d{6}-\\d{14}\\.(?i)mp4)$"
set destinationPath to sourcePath's stringByReplacingOccurrencesOfString:(regexPattern) ¬
withString:("/Volumes/coldstore/Video/_$2/$3/$4/$1") ¬
options:(current application's NSRegularExpressionSearch) range:({0, sourcePath's |length|()})
-- display dialog (destinationPath as text)
if ((destinationPath's isEqualToString:(sourcePath)) as boolean) then return -- Source path doesn't match regex.
set fileManager to current application's NSFileManager's defaultManager()
fileManager's createDirectoryAtPath:(destinationPath's stringByDeletingLastPathComponent()) ¬
withIntermediateDirectories:(true) attributes:(missing value) |error|:(missing value)
fileManager's copyItemAtPath:(sourcePath) toPath:(destinationPath) |error|:(missing value)
Edit: Regex pattern tightened up. Early exit if it’s not matched.