Replace folder creation date with file date?

Discuss and share scripts and script files...
Post Reply
Megalith
Posts: 87
Joined: 24 Jan 2015 16:53

Replace folder creation date with file date?

Post by Megalith »

Some folders I moved to another drive had their original creation dates replaced. The best way to retrieve those dates would be to refer to the dates of the file(s) within those folders, which aren't touched in copy/move operations. I know I could do this by hand, but since I have to deal with a ton of sub-folders, Is there a script (or program) that could automatically scan and copy the earliest/latest creation date of files in a folder, and then apply that to the root folder?

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

Re: Replace folder creation date with file date?

Post by highend »

Select the root folder(s) first...

Code: Select all

    $log = "";
    setting "BackgroundFileOps", 0;
    foreach($folder, "<get SelectedItemsPathNames |>") {

        $files = folderreport("files:{Created yyyymmddhhnnss}|{fullname}", "r", $folder, "r", , "<crlf>");
        if !($files) { continue; }
        $files = formatlist($files, "r", "<crlf>");
        $item  = gettoken($files, 1, "<crlf>");
        $file  = gettoken($item, 2, "|");
        timestamp "c", $file, $folder;
        $log   = $log . quote($folder) . " got the creation date from: " . quote($file);
    }
    text $log;
One of my scripts helped you out? Please donate via Paypal

PeterH
Posts: 2827
Joined: 21 Nov 2005 20:39
Location: DE W11Pro 24H2, 1920*1200*100% 3840*2160*150%

Re: Replace folder creation date with file date?

Post by PeterH »

Another possibility: instead of Move use Backup (preserving dates!) and then Delete.
This also might be advantageous in means of security.

Megalith
Posts: 87
Joined: 24 Jan 2015 16:53

Re: Replace folder creation date with file date?

Post by Megalith »

I really like the script, highend. But is there a way to get the script to copy the earliest date instead of the latest date that is found?

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

Re: Replace folder creation date with file date?

Post by highend »

What is "the earliest date"?
One of my scripts helped you out? Please donate via Paypal

Megalith
Posts: 87
Joined: 24 Jan 2015 16:53

Re: Replace folder creation date with file date?

Post by Megalith »

Example: Folder has two image files. First image file has creation time of 11:20:30, second image file has creation time of 11:20:35. I want the script to copy the creation time of the first image file, which has the earlier creation date/time.

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

Re: Replace folder creation date with file date?

Post by highend »

Code: Select all

$files = formatlist($files, "s", "<crlf>");
One of my scripts helped you out? Please donate via Paypal

Megalith
Posts: 87
Joined: 24 Jan 2015 16:53

Re: Replace folder creation date with file date?

Post by Megalith »

Works great! :biggrin:

jjiron
Posts: 5
Joined: 08 Mar 2016 08:23

Re: Replace folder creation date with file date?

Post by jjiron »

My scenario below is such that only the FOLDERS (root and subfolders) received the date of the backup (ie today) while files within them have the correct dates.

Will this script fix those dates as well? IE Root will have the oldest date within the subfolders (2/1999) , and the subfolders the date of the oldest file within it? Subfolder1=2/2004, Subfolder2=2/1999

Root (3/2016)
~Subfolder1 (3/2016)
~~~file1 - dateA (1/2010)
~~~file2- dateB (2/2004)
~Subfolfder2 (3/2016)
~~~file3 - dateA (1/2008)
~~~file4- dateB (2/1999)

Thanks for the clarification - I am in the process of backing 5TB of data and now in the middle of the process realized there is a date issue on many folders and sub-folders,

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

Re: Replace folder creation date with file date?

Post by highend »

No. The script only sets the creation date for the root folder, not for all subfolders of root.

Try this instead:

Code: Select all

    setting "BackgroundFileOps", 0;

    foreach($rootFolder, "<get SelectedItemsPathNames |>") {
        $oldestFile = "";
        foreach($subFolder, quicksearch("/d", $rootFolder, "|")) {
            $files = folderreport("files:{Created yyyymmddhhnnss}|{fullname}", "r", $subFolder, "r", , "<crlf>");
            if !($files) { continue; }
            $files = formatlist($files, "s", "<crlf>");
            $item  = gettoken($files, 1, "<crlf>");
            $file  = gettoken($item, 2, "|");
            timestamp "c", $file, $subFolder;
            $oldestFile = $oldestFile . $item . "<crlf>";
        }
        $oldestFile = gettoken(gettoken(formatlist($oldestFile, "es", "<crlf>"), 1, "<crlf>"), 2, "|");
        timestamp "c", $oldestFile, $rootFolder;
    }
One of my scripts helped you out? Please donate via Paypal

admin
Site Admin
Posts: 66259
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: Replace folder creation date with file date?

Post by admin »

@jjiron
Curious: What is the idea behind setting the folder date to the date of the oldest file within?

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

Re: Replace folder creation date with file date?

Post by TheQwerty »

admin wrote:@jjiron
Curious: What is the idea behind setting the folder date to the date of the oldest file within?
Obviously I can't answer for jjiron but it makes some sense to me.

If you have a folder with a created date of March 8, 2016 then it might feel odd for its contents to have a created date earlier than that. "This files was created before the folder existed?"

Generally, I think many people have a hard time understanding that the folder's dates don't necessarily have any correlation to their content.

EDIT: I'd venture that most people would prefer the folder's created date be that of its oldest descendant and its modified and accessed date correspond to the most recent from its descendants. Clearly, there's performance (and other) problems with this behavior, but it might better match expectations. /edit

Then you see how easy it is to modify any item's date/time and lose trust in them entirely.

Post Reply