Change created & modified date for files & folders to filename date

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

Change created & modified date for files & folders to filename date

Post by Schuller »

Hi,

I have seen, tried and attempted to modify very similar scripts found in the forum but have not had any success so if someone could please help with a script for my particular situation.

I need a script that will change both the created date and modified date of files and folders that I select to match the filename date. All the filenames start with the date and have characters after that and the dates are all formatted like this: 2024-11-18.

Filename Example: 2024-11-18(CIBC-Qtrade) $100.00

Thanks
Last edited by Schuller on 19 Nov 2024 08:57, edited 1 time in total.

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

Re: Change created & modified date for files & folders to filename date

Post by highend »

Code: Select all

    setting "BackgroundFileOps", 0;

    $items = <get SelectedItemsPathNames>;
    end (!$items), "No item(s) selected, aborted!";

    foreach($item, $items, <crlf>, "e") {
        $date = regexmatches(gpc($item, "file"), "^\d{4}-\d{2}-\d{2}");
        if (!$date) { continue; }

        // If you want a different hour:min:second than 00:00:00,
        // change it here!
        $date .= " 00:00:00";

        timestamp "cm", $date, $item;
    }
One of my scripts helped you out? Please donate via Paypal

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

Re: Change created & modified date for files & folders to filename date

Post by Schuller »

Works perfect!
Thanks

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

Re: Change created & modified date for files & folders to filename date

Post by highend »

My pleasure :tup:
One of my scripts helped you out? Please donate via Paypal

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

Re: Change created & modified date for files & folders to filename date

Post by Schuller »

Hi,

I've tried to modifying some existing scripts to get what I'm looking for but I am having no success for this particular situation.

I"m trying to change both the created date and modified date of files and folders that I select to match the filename date which in this case is only the 4 digit year(yyyy). So all the filenames in question start with the year only and then have varying characters after that. I'm just looking to change the created date and modified to Jan 1st, 12:00am, for both the created and modified.

Filename Examples:
2024_Amazon (change created and modified date to 2024-01-01 12:00am)
2025_Netflix (change created and modified date to 2025-01-01 12:00am)

Thanks

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

Re: Change created & modified date for files & folders to filename date

Post by highend »

Code: Select all

    $items = <get SelectedItemsPathNames>;
    end (!$items), "No item(s) selected, aborted!";

    setting "BackgroundFileOps", 0;

    foreach($item, $items, <crlf>, "e") {
        $year = regexmatches(gpc($item, "base"), "^\d{4}");
        if (!$year) { continue; }

        timestamp "cm", $year . "-01-01 12:00:00", $item;
    }
One of my scripts helped you out? Please donate via Paypal


tomuser
Posts: 135
Joined: 04 Dec 2009 18:52

Re: Change created & modified date for files & folders to filename date

Post by tomuser »

highend wrote: 18 Nov 2024 12:11

Code: Select all

    setting "BackgroundFileOps", 0;

    $items = <get SelectedItemsPathNames>;
    end (!$items), "No item(s) selected, aborted!";

    foreach($item, $items, <crlf>, "e") {
        $date = regexmatches(gpc($item, "file"), "^\d{4}-\d{2}-\d{2}");
        if (!$date) { continue; }

        // If you want a different hour:min:second than 00:00:00,
        // change it here!
        $date .= " 00:00:00";

        timestamp "cm", $date, $item;
    }
How should that script be changed to if file naming format has time included in addition to date, like "20250724_133144.pdf" for example (it may have or may not have additional text in the end before extension but first part is always same - date_time), and then have that time part also used with timestamp "cm" (or just "c" or just "m" based on need) to set correct timestamp for files?

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

Re: Change created & modified date for files & folders to filename date

Post by highend »

Code: Select all

    setting "BackgroundFileOps", 0;

    $items = <get SelectedItemsPathNames>;
    end (!$items), "No item(s) selected, aborted!";

    foreach($item, $items, <crlf>, "e") {
        $date = regexmatches(gpc($item, "file"), "^\d{8}_\d{6}");
        if (!$date) { continue; }
        $pattern = "^(\d{4})(\d{2})(\d{2})_(\d{2})(\d{2})(\d{2})";
        $date    = regexreplace($date, $pattern, "$1-$2-$3 $4:$5:$6");

        timestamp "cm", $date, $item;
    }
One of my scripts helped you out? Please donate via Paypal

tomuser
Posts: 135
Joined: 04 Dec 2009 18:52

Re: Change created & modified date for files & folders to filename date

Post by tomuser »

Thank you very much!

Post Reply