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

link|improve this question

38% accept rate
feedback

2 Answers

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.

link|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
feedback

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

link|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
feedback

Your Answer

 
or
required, but never shown

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