{"id":65523,"date":"2007-02-14T00:48:00","date_gmt":"2007-02-14T00:48:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/02\/14\/how-can-i-change-the-last-modified-timestamp-for-all-the-files-in-a-folder\/"},"modified":"2007-02-14T00:48:00","modified_gmt":"2007-02-14T00:48:00","slug":"how-can-i-change-the-last-modified-timestamp-for-all-the-files-in-a-folder","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-change-the-last-modified-timestamp-for-all-the-files-in-a-folder\/","title":{"rendered":"How Can I Change the Last Modified Timestamp for All the Files in a Folder?"},"content":{"rendered":"<p><H2><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> <\/H2>\n<P>Hey, Scripting Guy! How can I change the last modified timestamp for all the files in a folder? I\u2019d like the first file to have a specified timestamp, then each subsequent file to have a timestamp one minute later than the previous file.<BR><BR>&#8212; AF<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, AF; glad to see that you\u2019re taking a break from the <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/funzone\/games\/default.mspx\"><B>2007 Winter Scripting Games<\/B><\/A>. Is that because we\u2019re concerned that, between your regular job and the Scripting Games, you might be working too hard? Heck no; it\u2019s because we\u2019re concerned that <I>we<\/I> might be working too hard. At the time today\u2019s column was being written the Scripting Games had only been running for a few hours, and we\u2019d already received several hundred entries, each of which still has to be tested, scored, and recorded. Day 1 isn\u2019t even half over yet, and we\u2019re already way behind on tallying the results.<\/P>\n<P>But don\u2019t worry; after all, if anyone is used to be hopelessly behind on their commitments it\u2019s the Scripting Guys.<\/P>\n<P>At any rate, we feel like we deserve to take a little break from the Scripting Games. (Not that we\u2019ve actually tested, scored, or recorded any of the entries yet, mind you; we got tired just <I>counting<\/I> the number of entries.) With that in mind, let\u2019s see if we can figure out a way to change the last modified timestamp for all the files in a folder.<\/P>\n<P>Well, that was easy: we can\u2019t. Or, to be a little more specific, we can\u2019t think of any way to carry out this task using VBScript; for better or worse, neither the FileSystemObject, the Shell Application object, nor WMI have the ability to modify file system timestamps. But if you\u2019re willing to give <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx\"><B>Windows PowerShell<\/B><\/A> a try, not only can you carry out this task, but \u2013 even better \u2013 you can do so with a minimal amount of effort:<\/P><PRE class=\"codeSample\">$a = Get-Date &#8220;2\/13\/2007 8:00 AM&#8221;<\/p>\n<p>$b = Get-ChildItem &#8220;C:\\Scripts&#8221;<\/p>\n<p>foreach ($i in $b)\n{\n    $i.LastWriteTime = $a\n    $a = $a.AddMinutes(1)    \n}<\/p>\n<p>$b\n<\/PRE>\n<P>Explain how this all works? Well, sure we can do that (although we <I>were<\/I> hoping to get by with minimal effort ourselves). As you can see, we start out simply enough, using the <B>Get-Date<\/B> Cmdlet to create a datetime object ($a) equivalent to 8:00 AM on February 13, 2007:<\/P><PRE class=\"codeSample\">$a = Get-Date &#8220;2\/13\/2007 8:00 AM&#8221;\n<\/PRE>\n<P>As you can probably guess, we\u2019ll use $a to represent our starting date and time.<\/P>\n<P>We then use this line of code, and the <B>Get-ChildItem<\/B> Cmdlet, to retrieve a collection of all the files found in the folder C:\\Scripts:<\/P><PRE class=\"codeSample\">$b = Get-ChildItem &#8220;C:\\Scripts&#8221;\n<\/PRE>\n<P>Incidentally, AF, you noted in your email that you\u2019d like the files to be sorted alphabetically. Turns out that was something we could do with even <I>less<\/I> than minimal effort; by default, Get-ChildItem returns its collection already sorted in alphabetical order. That means that, at the moment, we have a collection of files that looks a little something like this:<\/P><PRE class=\"codeSample\">Mode                LastWriteTime     Length Name\n&#8212;-                &#8212;&#8212;&#8212;&#8212;-     &#8212;&#8212; &#8212;-\n-a&#8212;         2\/11\/2007   3:31 PM         89 1024.ps1\n-a&#8212;        12\/13\/2006   9:11 AM         56 1026.ps1\n-a&#8212;         1\/21\/2007   6:19 PM        133 1030.ps1\n-a&#8212;          8\/4\/2006   4:41 PM         99 1032.ps1\n-a&#8212;          2\/9\/2006   6:04 PM        146 1035.ps1\n-a&#8212;         1\/22\/2005   8:05 AM         68 1045.ps1\n<\/PRE>\n<P>You\u2019re right, that <I>was<\/I> easy, wasn\u2019t it? And guess what: changing the value of the <B>LastWriteTime<\/B> property for each file isn\u2019t all that much harder. In fact, that\u2019s what we do with this block of code:<\/P><PRE class=\"codeSample\">foreach ($i in $b)\n    {\n        $i.LastWriteTime = $a\n        $a = $a.AddMinutes(1)    \n    }\n<\/PRE>\n<P>What we\u2019re doing here is setting up a For Each loop that loops through all the items in $b; that is, through all the files in the folder C:\\Scripts:<\/P><PRE class=\"codeSample\">foreach ($i in $b)\n<\/PRE>\n<P>Each time we run through the loop we\u2019re going to do two things. First, we\u2019re going to set the <B>LastWriteTime<\/B> property of the file (represented by the variable $i) to the datetime value stored in $a:<\/P><PRE class=\"codeSample\">$i.LastWriteTime = $a\n<\/PRE>\n<P>That means that the first file in the collection will be assigned a LastWriteTime of 8:00 AM on February 13, 2007.<\/P>\n<P>Now, if we wanted all the files in C:\\Scripts to have the <I>same<\/I> timestamp we\u2019d be done. However, that\u2019s not what we want; instead, we want each file to have a slightly different timestamp. That\u2019s the purpose of the second line of code inside our For Each loop:<\/P><PRE class=\"codeSample\">$a = $a.AddMinutes(1)\n<\/PRE>\n<P>What we\u2019re doing here is using the <B>AddMinutes<\/B> method to increment our datetime value ($a) by 1 minute (hence the 1 that we pass to AddMinutes as the method parameter). As you no doubt recall, $a started off life being equal to 8:00 AM on February 13, 2007. By adding a minute to this value, we\u2019re going to make $a equal to 8:01 AM on February 13, 2007. In turn, that will cause the second file in the collection to be assigned a LastWriteTime one minute later than the first file timestamp. And then the third file will be assigned a timestamp one minute later than <I>that<\/I>, the fourth file a timestamp one minute later than the third file\u2019s, etc.<\/P>\n<P>The net result? This:<\/P><PRE class=\"codeSample\">Mode                LastWriteTime     Length Name\n&#8212;-                &#8212;&#8212;&#8212;&#8212;-     &#8212;&#8212; &#8212;-\n-a&#8212;         2\/13\/2007   8:00 AM         89 1024.ps1\n-a&#8212;         2\/13\/2007   8:01 AM         56 1026.ps1\n-a&#8212;         2\/13\/2007   8:02 AM        133 1030.ps1\n-a&#8212;         2\/13\/2007   8:03 AM         99 1032.ps1\n-a&#8212;         2\/13\/2007   8:04 AM        146 1035.ps1\n-a&#8212;         2\/13\/2007   8:05 AM         68 1045.ps1\n<\/PRE>\n<P>Which, all in all, seems to be just the thing you were hoping you would get.<\/P>\n<P>As for the Scripting Guys, break time is over and it\u2019s time to get to work scoring all those Scripting Games entries. Yes, sir, time to do some real work. Right \u2026 unless you have another question, AF? Oh, OK, then we guess it\u2019s back to work. Um, unless someone <I>else<\/I> has a question. Anyone? Don\u2019t be shy now. Anyone at all \u2026.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I change the last modified timestamp for all the files in a folder? I\u2019d like the first file to have a specified timestamp, then each subsequent file to have a timestamp one minute later than the previous file.&#8212; AF Hey, AF; glad to see that you\u2019re taking a break from [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[13,38,11,3,12,5],"class_list":["post-65523","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-dates-and-times","tag-files","tag-folders","tag-scripting-guy","tag-storage","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I change the last modified timestamp for all the files in a folder? I\u2019d like the first file to have a specified timestamp, then each subsequent file to have a timestamp one minute later than the previous file.&#8212; AF Hey, AF; glad to see that you\u2019re taking a break from [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65523","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=65523"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65523\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=65523"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=65523"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=65523"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}