Need a script please..

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
Schuller
Posts: 189
Joined: 02 May 2023 21:08

Need a script please..

Post by Schuller »

Need a script to do the following...

Move all files from within a single directory(not root) to target destinations wherein these files will be placed/categorized into folders which may or may not exist that are named based off the first part of their file name and then further into daily sub-folders that may or may not exist that are named based off the created date that is shown in the file name and not its actual created date. Also upon move the actual created and modified date of these files should be changed to match what is shown in the filename and the filename should also be formatted to match the example below.

Example:

Source:
G:\Reolink\Reolink_Downloads\209 Red Door-0-20250817000725-20250817000755.mp4
G:\Reolink\Reolink_Downloads\209 Laneway-0-20250823211229-20250823211314.mp4


Target:
J:\Red Door Video (209)\2025-08-17 - Sunday\209 Red Door - 08-17-25 @ 12.07.25 AM - Sunday.mp4 // created and modified date should be changed to match file name
J:\Laneway Video (209)\2025-08-23 - Saturday\209 Laneway - 08-23-25 @ 9.12.29 PM - Saturday.mp4 // created and modified date should be changed to match file name


Thanks

Norn
Posts: 483
Joined: 24 Oct 2021 16:10

Re: Need a script please..

Post by Norn »

For complex scripts like this, the AI ​​can usually give you a script that doesn't work, but you can consult the help documentation and provide feedback to fix it.
For reference only:

Code: Select all

// === CONFIG ===
    $src  = "G:\Reolink\Reolink_Downloads";       // Root folder to search
    $dest = "J:\";   // Base destination folder

// === PROCESS FILES FOUND BY QUICKSEARCH ===
// Find all mp4 files recursively in $src
    foreach($file, quicksearch("*.mp4 /r", $src), <crlf>) {

        if (exists($file) == 2) {continue} // skip if not a file
        $name = gpc($file, "file");

        // Split parts by "-"
        $part1 = gettoken($name, 1, "-"); // "209 Red Door"
        $part3 = gettoken($name, 3, "-"); // "20250817000725" (timestamp)

        // Camera number and name
        $camNum  = gettoken($part1, 1, " ");                // 209
        $camName = trim(substr($part1, strlen($camNum)+1)); // Red Door / Laneway

        // Timestamp
        $yyyy = substr($part3, 0, 4);
        $mm   = substr($part3, 4, 2);
        $dd   = substr($part3, 6, 2);
        $hh   = substr($part3, 8, 2);
        $mi   = substr($part3, 10, 2);
        $ss   = substr($part3, 12, 2);

        $dt   = "$yyyy-$mm-$dd $hh:$mi:$ss";
        $wday = formatdate($dt, "dddd");        // Day of week
        //$ampm = formatdate($dt, "hh.nn.ss tt"); // 12-hour format
        if ($hh < 12) {$ampm = formatdate($dt, "hh.nn.ss") . " AM"} else {$ampm = formatdate($dt, "hh.nn.ss") . " PM"}

        // Destination subfolder
        $subdir = $dest . "\" . $camName . " Video (209)\$yyyy-$mm-$dd - $wday";
        if !(exists($subdir)) { new($subdir, "dir"); }

        // New filename
        $newname = "$camNum $camName - " . formatdate($dt, "MM-dd-yy") . " @ $ampm - $wday.mp4";

        // Copy + rename
        copyitem($file, "$subdir\$newname");

        // Update timestamps
        timestamp("c", $dt, "$subdir\$newname");
        timestamp("m", $dt, "$subdir\$newname");
    }

 msg "Finished! Files have been copied and renamed into $dest";
Windows 11 24H2 @100% 2560x1440

Schuller
Posts: 189
Joined: 02 May 2023 21:08

Re: Need a script please..

Post by Schuller »

Thanks Norm for this "For Reference Only" script but unfortunately I don't have enough knowledge to make it work through to completion where it simply works. I tried as is just to see what happens and get I popups to confirm each file one by one then time stamping errors and then clicking on okay, nothing happens despite the final popup saying the files have been copied and renamed into J:\

Anyway, if you or anyone knows how to make it work through to completion then please help.

highend
Posts: 14577
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Need a script please..

Post by highend »

Code: Select all

    $src = "G:\Reolink\Reolink_Downloads";
    $dst = "J:";
    end (exists($src) != 2), quote($src) . <crlf 2> . "does not exist, aborted!";
    end (exists($dst) != 2), quote($dst) . <crlf 2> . "does not exist, aborted!";

    $items = quicksearch("*.mp4", $src, , "s");
    end (!$items), "No file(s) found in src path, aborted!";

    setting "BackgroundFileOps", 0;
    foreach($item, $items, <crlf>, "e") {
        $file = gpc($item, "file");
        $ext  = gpc($item, "ext");
        $date = gettoken(regexmatches($file, "[0-9]{14}"), 1, "|");
        if (!$date) { continue; }

        // Split & convert date
        $yyyy = substr($date, 0, 4);
        $yy   = substr($yyyy, 2, 2);
        $mm   = substr($date, 4, 2);
        $dd   = substr($date, 6, 2);
        $hh   = substr($date, 8, 2);
        $mi   = substr($date, 10, 2);
        $ss   = substr($date, 12, 2);

        // YYYY-MM-DD hh:nn:ss
        $date    = "$yyyy-$mm-$dd $hh:$mi:$ss";
        $weekday = format($date, "dddd"); // Monday, Tuesday, ...

        // Path from file name
        $parts      = gettoken($file, 1, "-", "t");
        $partsNum   = gettoken($parts, 1, " ", "t");
        $partsPlace = gettoken($parts, 2, " ", "t", 2);
        $newPath    = "$dst\$partsPlace Video ($partsNum)\$yyyy-$mm-$dd - $weekday";

        // New date
        // If the hour is 00, change it to 12 and use AM
        // If the hour is 12, keep it as 12 and use PM
        // If the hour is greater than 12, subtract 12 and use PM
        // Otherwise (1–11), keep the hour and use AM
        if     ($hh == "00") { $hh = 12; $suffix = "AM"; }
        elseif ($hh == 12)   { $suffix = "PM"; }
        elseif ($hh > 12)    { $hh -= 12; $suffix = "PM"; }
        else                 { $suffix = "AM"; }
        $newDate = "$hh.$mi.$ss $suffix";

        // New item name
        $newFile = "$parts - $mm-$dd-$yy @ $newDate - $weekday.$ext";

        // Timestamp old item
        timestamp "cm", $date, $item;

        // Move
        moveto $newPath, $item, , 2, 2, 4, 1, 0, 0, 1, 0, 0;

        // Rename moved
        renameitem($newFile, "$newPath\$file");
    }
    trayballoon "File(s) moved!", "Progress...", 1, 0;
One of my scripts helped you out? Please donate via Paypal

Norn
Posts: 483
Joined: 24 Oct 2021 16:10

Re: Need a script please..

Post by Norn »

Schuller wrote: 26 Aug 2025 16:32 Thanks Norm for this "For Reference Only" script but unfortunately I don't have enough knowledge to make it work through to completion where it simply works. I tried as is just to see what happens and get I popups to confirm each file one by one then time stamping errors and then clicking on okay, nothing happens despite the final popup saying the files have been copied and renamed into J:\

Anyway, if you or anyone knows how to make it work through to completion then please help.
I didn't add any checks in the code, but it works here.
Test.gif
Test.gif (302.26 KiB) Viewed 5947 times
Windows 11 24H2 @100% 2560x1440

Schuller
Posts: 189
Joined: 02 May 2023 21:08

Re: Need a script please..

Post by Schuller »

Not sure what happened on my end Norm but thanks again anyway.

Schuller
Posts: 189
Joined: 02 May 2023 21:08

Re: Need a script please..

Post by Schuller »

highend wrote: 26 Aug 2025 19:55

Code: Select all

    $src = "G:\Reolink\Reolink_Downloads";
    $dst = "J:";
    end (exists($src) != 2), quote($src) . <crlf 2> . "does not exist, aborted!";
    end (exists($dst) != 2), quote($dst) . <crlf 2> . "does not exist, aborted!";

    $items = quicksearch("*.mp4", $src, , "s");
    end (!$items), "No file(s) found in src path, aborted!";

    setting "BackgroundFileOps", 0;
    foreach($item, $items, <crlf>, "e") {
        $file = gpc($item, "file");
        $ext  = gpc($item, "ext");
        $date = gettoken(regexmatches($file, "[0-9]{14}"), 1, "|");
        if (!$date) { continue; }

        // Split & convert date
        $yyyy = substr($date, 0, 4);
        $yy   = substr($yyyy, 2, 2);
        $mm   = substr($date, 4, 2);
        $dd   = substr($date, 6, 2);
        $hh   = substr($date, 8, 2);
        $mi   = substr($date, 10, 2);
        $ss   = substr($date, 12, 2);

        // YYYY-MM-DD hh:nn:ss
        $date    = "$yyyy-$mm-$dd $hh:$mi:$ss";
        $weekday = format($date, "dddd"); // Monday, Tuesday, ...

        // Path from file name
        $parts      = gettoken($file, 1, "-", "t");
        $partsNum   = gettoken($parts, 1, " ", "t");
        $partsPlace = gettoken($parts, 2, " ", "t", 2);
        $newPath    = "$dst\$partsPlace Video ($partsNum)\$yyyy-$mm-$dd - $weekday";

        // New date
        // If the hour is 00, change it to 12 and use AM
        // If the hour is 12, keep it as 12 and use PM
        // If the hour is greater than 12, subtract 12 and use PM
        // Otherwise (1–11), keep the hour and use AM
        if     ($hh == "00") { $hh = 12; $suffix = "AM"; }
        elseif ($hh == 12)   { $suffix = "PM"; }
        elseif ($hh > 12)    { $hh -= 12; $suffix = "PM"; }
        else                 { $suffix = "AM"; }
        $newDate = "$hh.$mi.$ss $suffix";

        // New item name
        $newFile = "$parts - $mm-$dd-$yy @ $newDate - $weekday.$ext";

        // Timestamp old item
        timestamp "cm", $date, $item;

        // Move
        moveto $newPath, $item, , 2, 2, 4, 1, 0, 0, 1, 0, 0;

        // Rename moved
        renameitem($newFile, "$newPath\$file");
    }
    trayballoon "File(s) moved!", "Progress...", 1, 0;
This script works perfect highend :D

Thank you!

highend
Posts: 14577
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Need a script please..

Post by highend »

Thanks for the donation :mrgreen:
One of my scripts helped you out? Please donate via Paypal

Schuller
Posts: 189
Joined: 02 May 2023 21:08

Re: Need a script please..

Post by Schuller »

I think I need another similar type of script to do the following..

Move all files from within directories to target destinations wherein these files will be placed/categorized into folders which may or may not exist that are named based off the first part of the file name and then further into daily sub-folders that may or may not exist that are named based off the files actual created date. Also, upon move the actual created and modified date and time of these files should remain the same however the filename should be formatted to match the example below.

Example:

Source:

G:\Reolink\Reolink Local Recording\09152025\209 Laneway-00-230051-230104.mp4
G:\Reolink\Reolink Local Recording\09162025\209 Garage-00-193047-193120.mp4
G:\Reolink\Reolink Local Recording\09172025\209 Laneway-00-073927-073935.mp4
G:\Reolink\Reolink Local Recording\09172025\528 Driveway-00-113922-113945.mp4

Target:

J:\Laneway Video (209)\2025-09-15 - Monday\209 Laneway - 09-15-25 @ 11.00.51 PM - Monday.mp4 // created and modified date and time of file preserved
J:\Garage Video (209)\2025-09-16 - Tuesday\209 Garage - 09-16-25 @ 7.30.47 PM - Tuesday.mp4 // created and modified date and time of file preserved
J:\Laneway Video (209)\2025-09-17 - Wednesday\209 Laneway - 09-17-25 @ 7.39.27 AM - Wednesday.mp4 // created and modified date and time of file preserved
J:\Driveway Video (528)\2025-09-17 - Wednesday\528 Driveway - 09-17-25 @ 11.39.22 AM - Wednesday.mp4 // created and modified date and time of file preserved

Thanks

highend
Posts: 14577
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Need a script please..

Post by highend »

This should work:

Code: Select all

    $src = "G:\Reolink\Reolink Local Recording";
    $dst = "J:";
    end (exists($src) != 2), quote($src) . <crlf 2> . "does not exist, aborted!";
    end (exists($dst) != 2), quote($dst) . <crlf 2> . "does not exist, aborted!";

    $files = quicksearch("*.mp4", $src, , "s");
    end (!$files), "No file(s) found in src path, aborted!";

    setting "BackgroundFileOps", 0;
    $partNotFound = "";
    foreach($file, $files, <crlf>, "e") {
        $base   = gpc($file, "base");       // 209 Laneway-00-230051-230104
        $ext    = gpc($file, "ext");        // mp4
        $parent = gettoken($file, -2, "\"); // 09152025

        // Create formatted string for the first dst folder hierarchy from filename
        // 209 Laneway-00-230051-230104
        // Laneway Video (209)
        $part1 = gettoken($base, 1, "-", "t");  // 209 Laneway
        $num   = gettoken($part1, 1, " ", "t"); // 209
        $place = gettoken($part1, 2, " ", "t"); // Laneway
        $part1 = "$dst\$place Video ($num)";

        // Get date from file's parent
        // 09152025
        // yyyy-mm-dd - <day of week>
        if (!regexmatches($parent, "[0-9]{8}")) { continue; }
        $mm      = substr($parent, 0, 2); // 09
        $dd      = substr($parent, 2, 2); // 15
        $yyyy    = substr($parent, 4, 4); // 2025
        $yy      = substr($yyyy, 2, 2);   // 25
        $date    = "$yyyy-$mm-$dd";       // 2025-09-15
        $weekday = format($date, "dddd"); // Monday, Tuesday, ...
        $part2   = "$yyyy-$mm-$dd - $weekday";

        // Get time for the second part of the dst filename
        // 230051
        // 11.00.51 PM
        // The regex will capture two (start & end time), get only start
        $date = gettoken(regexmatches($base, "[0-9]{6}", "|"), 1, "|");
        if (!$date) { continue; }
        $hh = substr($date, 0, 2);
        $mi = substr($date, 2, 2);
        $ss = substr($date, 4, 2);

        // New date
        // If the hour is 00, change it to 12 and use AM
        // If the hour is 12, keep it as 12 and use PM
        // If the hour is greater than 12, subtract 12 and use PM
        // Otherwise (1–11), keep the hour and use AM
        if     ($hh == "00") { $hh = 12; $suffix = "AM"; }
        elseif ($hh == 12)   { $suffix = "PM"; }
        elseif ($hh > 12)    { $hh -= 12; $suffix = "PM"; }
        else                 { $suffix = "AM"; }

        // Trim a leading "0" from the hour
        $hh      = trim($hh, "0", "L");
        $newDate = "$hh.$mi.$ss $suffix";

        // Get formatted string for the dst filename
        $part3 = "$num $place - $mm-$dd-$yy @ $newDate - $weekday.$ext";

        // Build final path
        $finalPath = "$part1\$part2";

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

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

        // Rename moved!
        renameitem($part3, $movedFile);
    }
    trayballoon "File(s) moved!", "Progress...", 1, 0;
One of my scripts helped you out? Please donate via Paypal

Schuller
Posts: 189
Joined: 02 May 2023 21:08

Re: Need a script please..

Post by Schuller »

Some filenames have three parts in the base name like 209 Red Door* or 528 Gas Meter* and the script does not create those folders correctly but it does where the filenames have two parts like 209 Laneway or 209 Garage. I probably should have included more examples that shows that. What should be changed so that it works on both?

highend
Posts: 14577
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Need a script please..

Post by highend »

Replace

Code: Select all

$place = gettoken($part1, 2, " ", "t"); // Laneway
with:

Code: Select all

$place = gettoken($part1, 2, " ", "t", 2); // Laneway
One of my scripts helped you out? Please donate via Paypal

Schuller
Posts: 189
Joined: 02 May 2023 21:08

Re: Need a script please..

Post by Schuller »

Works perfect!
Thanks.

highend
Posts: 14577
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Need a script please..

Post by highend »

Again, thanks for the donation :tup:
One of my scripts helped you out? Please donate via Paypal

Post Reply