Page 1 of 1

Script to move/archive files and into newly created directories

Posted: 01 Feb 2025 03:23
by Schuller
Hi,

I'm trying to get around the standard limitations of the archival features of my DVR system and hoping XY's scripting can help me achieve that. I need a script or scripts to do the following by way of user buttons:

Move all files from within a directory(not root) to a target destination wherein these files will be placed based off their created date into daily sub-folders named with the same date as the files created date but with some slight name formatting. These target sub-folders may or may not exist and of course if the latter then it should be created by the script and have a name format like this: yyyy-mm-dd - dddd

Example 1: (User Button 1)

Source:
I:\Agent DVR\video\Red Door (209)\209 Red Door - 01-31-25 @ 3-37-23 PM - Friday.mp4 // If possible, I would like to replace "-" to "." (h.mm.ss AM/PM) in the time only upon move for these video files
I:\Agent DVR\video\Red Door (209)\grabs\209 Red Door - 12-29-24 @ 8.01.23 PM - Sunday.jpg
I:\Agent DVR\video\Red Door (209)\grabs\209 Red Door - 01-31-25 @ 11.37.42 AM - Friday.jpg
I:\Agent DVR\video\Red Door (209)\grabs\209 Red Door - 01-31-25 @ 3.01.23 PM - Friday.jpg

Target:
J:\Red Door Video (209)\2025-01-31 - Friday\209 Red Door - 01-31-25 @ 3.37.23 PM - Friday.mp4
K:\Red Door Grabs (209)\2024-12-29 - Sunday\209 Red Door - 12-29-24 @ 8.01.23 PM - Sunday.jpg
K:\Red Door Grabs (209)\2025-01-31 - Friday\209 Red Door - 01-31-25 @ 11.37.42 AM - Friday.jpg
K:\Red Door Grabs (209)\2025-01-31 - Friday\209 Red Door - 01-31-25 @ 3.01.23 PM - Friday.jpg


Example 2: (User Button 2)

Source:
I:\Agent DVR\video\Driveway (528)\528 Driveway - 01-30-25 @ 11-18-58 PM - Thursday.mkv // If possible, I would like to replace "-" to "." (h.mm.ss AM/PM) in the time only upon move for these video files
I:\Agent DVR\video\Doorbell (528)\528 Doorbell - 01-30-25 @ 4-53-22 AM - Thursday.mkv // If possible, I would like to replace "-" to "." (h.mm.ss AM/PM) in the time only upon move for these video files
I:\Agent DVR\video\Driveway (528)\grabs\528 Driveway - 01-31-25 @ 4.41.46 PM - Friday.jpg
I:\Agent DVR\video\Doorbell (528)\grabs\528 Doorbell - 01-31-25 @ 12.19.10 AM - Friday.jpg

Target:
J:\Doorbell & Driveway Video (528)\2025-01-30 - Thursday\528 Driveway - 01-30-25 @ 11.18.58 PM - Thursday.mkv
J:\Doorbell & Driveway Video (528)\2025-01-30 - Thursday\528 Doorbell - 01-30-25 @ 4.53.22 AM - Thursday.mkv
K:\Doorbell & Driveway Grabs (528)\2025-01-31 - Friday\528 Driveway - 01-31-25 @ 4.41.46 PM - Friday.jpg
K:\Doorbell & Driveway Grabs (528)\2025-01-31 - Friday\528 Doorbell - 01-31-25 @ 12.19.10 AM - Friday.jpg

Thanks

Re: Script to move/archive files and into newly created directories

Posted: 01 Feb 2025 08:14
by highend
For both buttons:
Alle files share the same root: "I:\Agent DVR\video\"
The rule for button 1 is: Move only files that contain exactly "Red Door (209)" in their path?
The rule for button 2 is: Move only files that contain exactly "Driveway (528)" or "Doorbell (528)" in their path?

Re: Script to move/archive files and into newly created directories

Posted: 01 Feb 2025 09:22
by Schuller
Yes, both buttons share the same root: "I:\Agent DVR\video\"
The rule for button 1 is: Move only files that contain exactly "Red Door (209)" in their path?
Correct!
The rule for button 2 is: Move only files that contain exactly "Driveway (528)" or "Doorbell (528)" in their path?
Correct!

Drive K:\ stores the image files and Drive J:\ stores the video files.

Re: Script to move/archive files and into newly created directories

Posted: 01 Feb 2025 09:33
by Schuller
Here is a screenshot of the how I would like the folders to appear in the archived/target location. I've been doing this manually so far.
2025-02-01_Doorbell & Driveway.png
2025-02-01_Doorbell & Driveway.png (161.81 KiB) Viewed 1043 times

Re: Script to move/archive files and into newly created directories

Posted: 01 Feb 2025 10:34
by highend
For user button 1 (can't promise that I have time for button 2 up to tomorrow evening):

Making a backup of your "I:\Agent DVR\video\Red Door (209)" folder first would be a wise choice!
If the script works fine, you can delete the backup...

When the script has finished (without showing a window with failed files) you can remove the "I:\Agent DVR\video\Red Door (209)" manually afterwards!

Code: Select all

    $src = "I:\Agent DVR\video";
    end (exists($src) != 2), quote($src) . <crlf 2> . "does not exist, aborted!";

    $dstVideo    = "J:";
    $dstGraphics = "K:";
    end (exists($dstVideo) != 2),    quote($dstVideo)    . <crlf 2> . "does not exist, aborted!";
    end (exists($dstGraphics) != 2), quote($dstGraphics) . <crlf 2> . "does not exist, aborted!";

    $files = quicksearch('/paths="*\Red Door (209)\*" /types=mp4;jpg', $src, , "s");
    end (!$files), "No file(s) found in src path, aborted!";


    setting "BackgroundFileOps", 0;
    $partNotFound = "";
    foreach($file, $files, <crlf>, "e") {
        // Cut root
        // Red Door (209)\209 Red Door - 01-31-25 @ 3-37-23 PM - Friday.mp4
        // Red Door (209)\grabs\209 Red Door - 12-29-24 @ 8.01.23 PM - Sunday.jpg
        $fileCutRoot = replace($file, $src . "\");

        // Add "Video" / "Grabs" info & get file names
        $part1 = gettoken($fileCutRoot, 1, "\"); // Red Door (209)
        if (strpos($fileCutRoot, "\grabs\") != -1) {
            $part1    = regexreplace($part1, " \(", " Grabs ("); // Red Door Grabs (209)
            $partFile = gettoken($fileCutRoot, 3, "\");          // 209 Red Door - 12-29-24 @ 8.01.23 PM - Sunday.jpg
        } else {
            $part1    = regexreplace($part1, " \(", " Video ("); // Red Door Video (209)
            $partFile = gettoken($fileCutRoot, 2, "\");          // 209 Red Door - 01-31-25 @ 3-37-23 PM - Friday.mp4
        }

        // Create second path part out of filename
        // 209 Red Door - 01-31-25 @ 3-37-23 PM - Friday.mp4 => 2025-01-31 - Friday
        // Get day part
        $part2Day = regexmatches($partFile, "(Mon|Tues|Wednes|Thurs|Fri|Satur|Sun)day");
        if (!$part2Day) {
            $partNotFound .= "No day : " . $file . <crlf>;
            continue;
        }

        // Get mm-dd-yy part
        $part2Date = gettoken(regexmatches($partFile, "\d{2}-\d{2}-\d{2}"), 1, "|"); // 01-31-25
        if (!$part2Date) {
            $partNotFound .= "No date: " . $file . <crlf>;
            continue;
        }
        // Reformat date
        // 01-31-25 => 2025-01-31
        $part2Date = regexreplace($part2Date, "(\d{2})-(\d{2})-(\d{2})", "20$3-$1-$2");

        // Put them together as part2
        $part2 = $part2Date . " - " . $part2Day;

        // Part3, file name
        // Replace: 3-37-23 AM/PM => 3.37.23 AM/PM
        // 209 Red Door - 01-31-25 @ 3-37-23 PM - Friday.mp4 => 209 Red Door - 01-31-25 @ 3.37.23 PM - Friday.mp4
        $time = regexmatches($partFile, "\d{1,2}[-.]\d{2}[-.]\d{2} (AM|PM)");
        $time = replace($time, "-", ".");
        $part3 = regexreplace($partFile, "\d{1,2}[-.]\d{2}[-.]\d{2} (AM|PM)", $time);

        // Build final path
        if (gpc($file, "ext") LikeI "mp4") {
            $finalPath = $dstVideo . "\" . $part1 . "\" . $part2;
        } elseif (gpc($file, "ext") LikeI "jpg") {
            $finalPath = $dstGraphics . "\" . $part1 . "\" . $part2;
        }

        // Build final file
        $movedFile = $finalPath . "\" . $partFile;

        // Move!
        moveto $finalPath, $file, , 2, 2, 4, 1, 0, 0, 1, 0, 0;

        // Rename moved!
        renameitem($part3, $movedFile);
    }
    if ($partNotFound) {
        text "File(s) not moved because of a missing part:" . <crlf 2> . $partNotFound;
    }
    trayballoon "File(s) moved!", "Progress...", 1, 0;

Re: Script to move/archive files and into newly created directories

Posted: 01 Feb 2025 19:21
by Schuller
Now that button click was absolutely incredible!

I swear I have the ultimate DVR companion tool with XY and so much more.

Thanks again.

Re: Script to move/archive files and into newly created directories

Posted: 02 Feb 2025 11:28
by highend
Ok, here is the one for button 2.
A few changes to the first one but nothing really major :)

Code: Select all

    $src = "I:\Agent DVR\video";
    end (exists($src) != 2), quote($src) . <crlf 2> . "does not exist, aborted!";

    $dstVideo    = "J:";
    $dstGraphics = "K:";
    end (exists($dstVideo) != 2),    quote($dstVideo)    . <crlf 2> . "does not exist, aborted!";
    end (exists($dstGraphics) != 2), quote($dstGraphics) . <crlf 2> . "does not exist, aborted!";

    $files = quicksearch('/paths="*\Driveway (528)\*";"*\Doorbell (528)\*" /types=mkv;jpg', $src, , "s");
    end (!$files), "No file(s) found in src path, aborted!";


    setting "BackgroundFileOps", 0;
    $partNotFound = "";
    foreach($file, $files, <crlf>, "e") {
        // Cut root
        // Driveway (528)\528 Driveway - 01-30-25 @ 11-18-58 PM - Thursday.mkv
        // Doorbell (528)\grabs\528 Doorbell - 01-31-25 @ 12.19.10 AM - Friday.jpg
        $fileCutRoot = replace($file, $src . "\");

        // Add "Video" / "Grabs" info & get file names
        $part1 = "Doorbell & Driveway ";
        if (strpos($fileCutRoot, "\grabs\") != -1) {
            $part1    .= "Grabs (528)";
            $partFile  = gettoken($fileCutRoot, 3, "\"); // 528 Driveway - 01-30-25 @ 11-18-58 PM - Thursday.mkv
        } else {
            $part1    .= "Video (528)";
            $partFile  = gettoken($fileCutRoot, 2, "\"); // 528 Doorbell - 01-31-25 @ 12.19.10 AM - Friday.jpg
        }

        // Create second path part out of filename
        // 528 Driveway - 01-30-25 @ 11.18.58 PM - Thursday.mkv => 2025-01-30 - Thursday
        // Get day part
        $part2Day = regexmatches($partFile, "(Mon|Tues|Wednes|Thurs|Fri|Satur|Sun)day");
        if (!$part2Day) {
            $partNotFound .= "No day : " . $file . <crlf>;
            continue;
        }

        // Get mm-dd-yy part
        $part2Date = gettoken(regexmatches($partFile, "\d{2}-\d{2}-\d{2}"), 1, "|"); // 01-30-25
        if (!$part2Date) {
            $partNotFound .= "No date: " . $file . <crlf>;
            continue;
        }
        // Reformat date
        // 01-30-25 => 2025-01-30
        $part2Date = regexreplace($part2Date, "(\d{2})-(\d{2})-(\d{2})", "20$3-$1-$2");

        // Put them together as part2
        $part2 = $part2Date . " - " . $part2Day;

        // Part3, file name
        // Replace: 11-18-58 AM/PM => 11.18.58 AM/PM
        // 528 Driveway - 01-30-25 @ 11-18-58 PM - Thursday.mkv => 528 Driveway - 01-30-25 @ 11.18.58 PM - Thursday.mkv
        $time = regexmatches($partFile, "\d{1,2}[-.]\d{2}[-.]\d{2} (AM|PM)");
        $time = replace($time, "-", ".");
        $part3 = regexreplace($partFile, "\d{1,2}[-.]\d{2}[-.]\d{2} (AM|PM)", $time);

        // Build final path
        if (gpc($file, "ext") LikeI "mkv") {
            $finalPath = $dstVideo . "\" . $part1 . "\" . $part2;
        } elseif (gpc($file, "ext") LikeI "jpg") {
            $finalPath = $dstGraphics . "\" . $part1 . "\" . $part2;
        }

        // Build final file
        $movedFile = $finalPath . "\" . $partFile;

        // Move!
        moveto $finalPath, $file, , 2, 2, 4, 1, 0, 0, 1, 0, 0;

        // Rename moved!
        renameitem($part3, $movedFile);
    }
    if ($partNotFound) {
        text "File(s) not moved because of a missing part:" . <crlf 2> . $partNotFound;
    }
    trayballoon "File(s) moved!", "Progress...", 1, 0;

Re: Script to move/archive files and into newly created directories

Posted: 03 Feb 2025 10:20
by Schuller
Just ran this second script works perfect too.

Thanks again!

Re: Script to move/archive files and into newly created directories

Posted: 03 Feb 2025 10:21
by highend
:tup: