{"id":55913,"date":"2008-03-27T00:29:00","date_gmt":"2008-03-27T00:29:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2008\/03\/27\/hey-scripting-guy-how-can-i-change-the-last-accessed-date-of-a-file-to-match-its-last-modified-date\/"},"modified":"2008-03-27T00:29:00","modified_gmt":"2008-03-27T00:29:00","slug":"hey-scripting-guy-how-can-i-change-the-last-accessed-date-of-a-file-to-match-its-last-modified-date","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-change-the-last-accessed-date-of-a-file-to-match-its-last-modified-date\/","title":{"rendered":"Hey, Scripting Guy! How Can I Change the Last-Accessed Date of a File to Match Its Last-Modified Date?"},"content":{"rendered":"<p><img decoding=\"async\" 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\" \/> <\/p>\n<p>Hey, Scripting Guy! How can I change the last-accessed date for all the files in a folder? I want the last-accessed date to match the date and time that the file was last modified.<\/p>\n<p>&#8212; GW<\/p>\n<p><img decoding=\"async\" height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\" \/><img decoding=\"async\" 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 decoding=\"async\" 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> <\/p>\n<p>Hey, GW. Say, did you realize that yesterday was Opening Day for Major League Baseball? If you didn\u2019t, well, don\u2019t feel bad; even the Scripting Guy who writes this column forgot that yesterday was Opening Day. In fact, he didn\u2019t remember anything at all about Opening Day until he got up Tuesday morning, turned on the TV, and saw that the Boston Red Sox were leading the Oakland A\u2019s 6-5 in the bottom of the tenth.<\/p>\n<p>Before you ask, no, the Scripting Guy who writes this column doesn\u2019t get to sleep all day; instead, he \u2013 very grudgingly \u2013 gets up at 6:15 every morning. However, the game was being played, and was very nearly over, because of one of those curious decisions that only Major League Baseball can come up with. Baseball is considered America\u2019s national pastime, and Opening Day is \u2013 along with the World Series \u2013 one of the cornerstones of the entire season. \u201cSo let\u2019s see,\u201d reasoned the folks who run Major League Baseball. \u201cWe have America\u2019s national pastime, we have a game that all baseball fans are interested in watching \u2013 wait, I\u2019ve got it: we\u2019ll play the opening game of the season in Japan, and have that game take place in the middle of the night when most Major League Baseball fans are sound asleep!\u201d<\/p>\n<table class=\"dataTable\" id=\"E3C\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\"><b>Note<\/b>. If you ever wondered how baseball could end up with the designated hitter (albeit in only one of the two leagues); World Series games played in November; and an All-Star game that ends in a tie (!), well, now you know.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>Oddly enough, however, it\u2019s the <i>Japanese<\/i> baseball fans that the Scripting Guy who writes this column feels bad for. (And not just because they got stuck having to watch the Oakland A\u2019s.) As most of you probably know, they already play baseball in Japan, and it\u2019s actually a pretty cool experience: the fans in the outfield bleachers stand through the entire game; they sing songs; they do cheers; they shoot off fireworks; they bang drums. Oh, and they sell beer, too, although instead of having some fat guy walk around with a tray of plastic glasses they have girls walk around with kegs of beer strapped to their backs. <\/p>\n<p>No, we\u2019re serious. Here\u2019s a picture the Scripting Guy who writes this column took of one such beer vendor:<\/p>\n<p><img decoding=\"async\" height=\"441\" alt=\"Japanese Baseball\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/japan1.jpg\" width=\"346\" border=\"0\" \/> <\/p>\n<p>You\u2019re trying to tell us that the Japanese have girls walking around with kegs of beer strapped to their backs and yet they need <i>Major League Baseball<\/i>? Major League Baseball, where even the cup holders on each and every seat have advertisements? Major League Baseball, where they actually tried to put advertisements <a href=\"http:\/\/www.usatoday.com\/sports\/baseball\/2004-05-06-spider-man-plan-dropped_x.htm\" target=\"_blank\"><b>on the bases<\/b><\/a>, before public outcry caused them to beat a hasty retreat? Please.<\/p>\n<p>In case you\u2019re wondering, the Scripting Guy who writes this column speaks from experience: he\u2019s actually <i>been<\/i> to a Japanese Pacific League game. A few years ago, when the Scripting Son was playing on an all-star team that toured the Tokyo area, the Scripting Guy who writes this column watched the Chiba Lotte Marines defeat the Orix Blue Wave (a team that has since merged with the Kintetsu Buffaloes.) <\/p>\n<p>In fact, here\u2019s his \u201cBobby Seat\u201d ticket featuring a picture of manager Bobby Valentine:<\/p>\n<p><img decoding=\"async\" height=\"190\" alt=\"Japanese Baseball\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/japan2.jpg\" width=\"350\" border=\"0\" \/> <\/p>\n<p>Good luck trying to find a Bobby Seat in the US! <\/p>\n<p>If you, like the Scripting Guy who writes this column, missed Opening Day, well, that\u2019s too bad: after all, there\u2019s only one <i>true<\/i> Opening Day. And that\u2019s doubly bad because many of you are probably already a bit distraught over the fact that VBScript provides no way to modify the last-accessed date of a file. You can use the FileSystemObject to retrieve the value of the last-accessed date, but there\u2019s no way \u2013 no way that we know of, at least \u2013 to <i>change<\/i> that value.<\/p>\n<table class=\"dataTable\" id=\"EQE\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\"><b>Note<\/b>. However, you <i>can<\/i> change the value of the last-modified date, if that helps. For more information, see our <i>TechNet Magazine <\/i>article <a href=\"http:\/\/technet.microsoft.com\/en-us\/magazine\/cc194399.aspx\" target=\"_blank\"><b>Our Favorite Shell Games<\/b><\/a>.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>But don\u2019t despair. Although there\u2019s nothing we can do about Opening Day 2008 (well, not unless Scripting Guy Peter Costantini finishes his time machine), there <i>is<\/i> something we can do about changing the last-accessed date for all the files in a folder. As long as you\u2019re willing to use <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx\"><b>Windows PowerShell<\/b><\/a> (and there\u2019s no reason why you shouldn\u2019t be willing to use it) this little script should do the trick:<\/p>\n<pre class=\"codeSample\">Get-ChildItem C:\\Scripts | Where-Object {$_.PSIsContainer -eq $False} |\nForEach-Object {$_.LastAccessTime = $_.LastWriteTime}\n<\/pre>\n<p>As you can see, this is just a single line of PowerShell code. (Well, OK, it <i>looks<\/i> like two lines. But that\u2019s because we had to break the line in order to get it to fit nicely on the page.) We start out in simple enough fashion, using the <b>Get-ChildItem<\/b> cmdlet to retrieve a collection of all the items in the folder C:\\Scripts. That\u2019s what this hunk of code is for:<\/p>\n<pre class=\"codeSample\">Get-ChildItem C:\\Scripts\n<\/pre>\n<p>As we noted a second ago, Get-ChildItem is going to return <i>all<\/i> of the items in the folder C:\\Scripts; that includes subfolders as well as files. That\u2019s fine, except that all we care about are files (and not just for the purposes of this script; the only things the Scripting Guys care about, <i>period<\/i>, are files). Because of that, we next take our collection and \u201cpipe\u201d it to the <b>Where-Object<\/b> cmdlet:<\/p>\n<pre class=\"codeSample\">Where-Object {$_.PSIsContainer -eq $False}\n<\/pre>\n<table class=\"dataTable\" id=\"EIG\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\"><b>Note<\/b>. For those of you who haven\u2019t worked with PowerShell, the <b>$_<\/b> syntax represents the current object in the pipeline. That simply means that \u2013 what\u2019s that? Oh, you say you don\u2019t know what the pipeline is either? Then you should take a look at our article <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/winpsh\/manual\/pipe.mspx\"><b>Piping and the Pipeline<\/b><\/a>.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>What we do next is ask Where-Object to filter the collection for us, retaining only those items where the <b>PSIsContainer<\/b> property is False ($False). As you might have guessed, if PSIsContainer is False then we\u2019re dealing with a file; if PSIsContainer is True then we\u2019re dealing with a folder (that is, with a container). <\/p>\n<p>Finally, we take the filtered collection and pipe it to the <b>ForEach-Object<\/b> cmdlet:<\/p>\n<pre class=\"codeSample\">ForEach-Object {$_.LastAccessTime = $_.LastWriteTime}\n<\/pre>\n<p>As the name implies, ForEach-Object is going to loop through each and every object in the filtered collection and then do something to each of those objects. What\u2019s it going to do? That\u2019s easy: it\u2019s going to set the value of the object\u2019s <b>LastAccessTime<\/b> property to the value of its <b>LastWriteTime<\/b> property. In other words, it\u2019s going to change the last-accessed date to match the value of the last-modified date.<\/p>\n<p>Which is all we ever wanted to do in the first place.<\/p>\n<p>We should probably note that it <i>is<\/i> possible to use VBScript to go the opposite route: to change the last-modified date so it matches the last-accessed date. (Which might work for you, GW; after all, the net result is that the last-modified date and the last-accessed date end up being identical.) If it\u2019s OK to go that route then this script should help:<\/p>\n<pre class=\"codeSample\">Set objShell = CreateObject(\"Shell.Application\")\nSet objFSO = CreateObject(\"Scripting.FileSystemObject\")\n\nSet objFolder = objShell.NameSpace(\"C:\\Scripts\")\nSet colItems = objFolder.Items\n\nFor Each objItem In colItems\n    Set objFile = objFSO.GetFile(objItem.Path)\n    dtmNewDate = objFile.DateLastAccessed\n    objItem.ModifyDate  = dtmNewDate\nNext\n<\/pre>\n<p>For more information, see our <a href=\"http:\/\/technet.microsoft.com\/en-us\/magazine\/cc194399.aspx\" target=\"_blank\"><b>TechNet Magazine article<\/b><\/a>.<\/p>\n<p>As for the Major League Baseball season, here\u2019s an interesting piece of trivia for you. Oakland and Boston will play two official games in Japan, then return home and resume spring training; Boston, for example, will come back and play three exhibition games against the Dodgers (boo!) before continuing the regular season on April 1<sup>st<\/sup> against Oakland. Cool, huh? Maybe next year the Seattle Mariners and the Kansas City Royals will go to Australia in November, play the first two games of the 2009 season, then come back to the US and wait for spring training to start in February.<\/p>\n<p>And yes, we realize that games played in November could conflict with the 2008 World Series. But that\u2019s OK; most likely the Mariners and the Royals won\u2019t have to worry about any such conflict. <\/p>\n<table class=\"dataTable\" id=\"EFAAC\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p><b>Note<\/b>. Especially the Mariners: their two big-money, big-name free agent pitchers \u2013 Eric Bedard and Carlos Silva \u2013 both have spring training earned-run averages above 9.20.<\/p>\n<p>Oh, well. Football season can\u2019t be far off, right?<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I change the last-accessed date for all the files in a folder? I want the last-accessed date to match the date and time that the file was last modified. &#8212; GW Hey, GW. Say, did you realize that yesterday was Opening Day for Major League Baseball? If you didn\u2019t, well, [&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,3,4,12,5,45],"class_list":["post-55913","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-dates-and-times","tag-files","tag-scripting-guy","tag-scripting-techniques","tag-storage","tag-vbscript","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I change the last-accessed date for all the files in a folder? I want the last-accessed date to match the date and time that the file was last modified. &#8212; GW Hey, GW. Say, did you realize that yesterday was Opening Day for Major League Baseball? If you didn\u2019t, well, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/55913","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=55913"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/55913\/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=55913"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=55913"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=55913"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}