Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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)?

share|improve this question

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.

share|improve this answer
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. – matt wilkie May 26 '11 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! – matt wilkie May 26 '11 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 '11 at 8:04

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

share|improve this answer
would this require manual intervention other than running it of course? – Notitze Jun 1 '10 at 9:16
+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) – matt wilkie May 26 '11 at 17:06

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.

share|improve this answer

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"


share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .