3

In Windows, is there a way to update automatically (or with a simple script) the date of a folder with the latest modified date of any of its files (recursive)?

7 Answers 7

4

You can do this in PowerShell. Something like this to update the current folder based on files it directly contains:

$lastModified = (dir . | ?{!$_.PSIsContainer} | sort LastWriteTime | select -last 1).LastWriteTime
$folder = get-item .
$folder.LastWriteTime = $lastModified

(This will fail if there are no files in the folder or if the user does not have suitable permissions.)

EDIT: Ensure only one file (the last) is found so sort pipeline has a singular result.

3
  • on Win7 I get the error Exception setting "LastWriteTime": "Cannot convert null to type "System.DateTime"." when running this. It has to do with the first line, the .LastWriteTime suffix is resolving to null. Removing it creates a value for $lastmodified, but not the one we want. May 26, 2011 at 17:24
  • solved first line, one needs to grab first item from list and not the list itself: $fileList = (dir . | ?{!$_.PSIsContainer} | sort LastWriteTime), followed by $lastModified = $fileList[0].LastWriteTime. Still doesn't work though, setting the last mod time says Exception setting "LastWriteTime": "The process cannot access the file 'D:\bugs' because it is being used by another process." but for the life of my I can't figure out what that process might be (and I've tried many different folders.) I think the offending process is itself! May 26, 2011 at 17:58
  • @Matt: fixed your first issue (forgot to only return a single item to get the date). For your second: it could be that PSH is holding the folder open, I would use Handles or Process Explorer (both Sysinternals) to determine what has the folder open.
    – Richard
    May 27, 2011 at 8:04
2

I just did a simple test - if you create an empty file in the directory, it changes the folder modified date to to that time. When you delete the file, that still counts as a modification, so it stays modified at that time.

To do so recursively, you'd have to create an empty file in every folder you wish to have the date changed in. This is still a fairly simple solution if you're willing to do some simple programming.

If you want to change the date to something other than the current time, this solution obviously will not work.

1

You can use Bulk File Changer.

BulkFileChanger is a small utility that allows you to create files list from multiple folders, and then make some action on them - Modify their created/modified/accessed time, change their file attribute (Read Only, Hidden, System), run an executable with these files as parameter, and copy/cut paste into Explorer.

alt text

3
  • would this require manual intervention other than running it of course?
    – Notitze
    Jun 1, 2010 at 9:16
  • 1
    +1 for highlighting a useful utility that can change file and folder dates. -1 that it can't be used in the manner asked for (set folder date based on contents, recursively) May 26, 2011 at 17:06
  • Indeed, this is a good program by Nir Sofer, but the folder mod/creation date can only be set manually. Nir wrote another program which does this much more elegantly, see my answer on FolderTimeUpdate. Jun 4, 2018 at 3:05
1

FolderTimeUpdate

Really good choice for non-scripting program. Aside from not being configurably automatic (in the GUI), its functionality is exactly that desired:

FolderTimeUpdate is a simple tool for Windows that scans all files and folders under the base folder you choose, and updates the 'Modified Time' of every folder according the latest modified time of the files stored in it.

enter image description here

Most of this can be accomplished on the command line as well, so conceivably you could automate it by setting it to run at intervals, or perhaps even monitor certain folders.

The GUI allows control of subfolder depth to recurse, inclusion and exclusion wildcards, and even whether to change the file creation time also (same as modified time, or match oldest creation or modified time).

Program by the excellent and prolific Nir Sofer (the same author of Bulk File Changer in a less relevant answer).

0

Not pretty, but functional. Done in bash / cygwin

==================
find -maxdepth 1 -type d | grep -v "^\.$" | while read D ; do
  cd "$D"
  F=`ls -1tr | tail -1`
  cd ..
  touch -r "$D/$F" "$D"
done
==================

Brian

0

Here is a modified version of Richard's answer. It's also PowerShell script. Instead of setting the modified time of the current directory, it sets the modified time of all first-level subdirectories of the current directory. Another difference is that it looks recursively for the last modified time. Hope it's helpful for someone else.

$parentPath = Get-Location
Get-ChildItem | where {$_.PsIsContainer} | foreach {
    cd -LiteralPath $_.FullName
    $files = dir -Recurse . | where {-Not $_.PsIsContainer}
    if ($files.Count -eq 0) {
        echo "note: $($_.Name) contains no files"
    } else {
        $_.LastWriteTime = ($files | sort LastWriteTime | select -last 1).LastWriteTime
    }
}
cd -LiteralPath $parentPath
0

Do Nothing! Mission accomplished!

The last modified date of a folder is always that of the last modified file in it.

3
  • If the last modified file has been deleted/moved, the date doesn't go back, so in some cases, this doesn't work if you want to have the date of the last modified file currently in the folder.
    – satibel
    Apr 3, 2017 at 8:20
  • The OP said "update" not "postdate".
    – user477799
    Apr 3, 2017 at 8:21
  • It also doesn't work recursively at least on windows 7 with the structure \1\2\3.exe 1 and 2 are from 2016 I've pasted 3.exe which is from 2003 result: 1 is still in 2016 2 is in 2017 3.exe is in 2003.
    – satibel
    Apr 3, 2017 at 8:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.