{"id":64663,"date":"2007-06-13T23:57:00","date_gmt":"2007-06-13T23:57:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/06\/13\/how-can-i-rename-a-file-every-day-using-the-current-date-as-part-of-the-file-name\/"},"modified":"2007-06-13T23:57:00","modified_gmt":"2007-06-13T23:57:00","slug":"how-can-i-rename-a-file-every-day-using-the-current-date-as-part-of-the-file-name","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-rename-a-file-every-day-using-the-current-date-as-part-of-the-file-name\/","title":{"rendered":"How Can I Rename a File Every Day, Using the Current Date as Part of the File Name?"},"content":{"rendered":"<p><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\"> \n<P>Hey, Scripting Guy! I have a file that has the name BackupFile-06122007.txt, where the <B>06122007<\/B> represents yesterday\u2019s date (June 12, 2007). Each morning I would like to run a script that changes the name of that file so that it uses <I>today\u2019s<\/I> date: BackupFile-06<B>13<\/B>2007.txt. How can I do that?<BR><BR>&#8212; GZ <\/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, GZ. You know, one thing you can count on is that the Scripting Guys will always be the same, no matter where they are or what they\u2019re doing. For example, last week at <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/teched07\/default.mspx\"><B>TechEd 2007<\/B><\/A> Scripting Guys Jean Ross and Greg Stemp managed to get themselves caught in a torrential downpour while visiting Disney\u2019s <A href=\"http:\/\/disneyworld.disney.go.com\/wdw\/parks\/parkLanding?id=AKLandingPage\" target=\"_blank\"><B>Animal Kingdom<\/B><\/A>. Considering they had to walk for about half an hour or so before they could find a taxi to take them back to the hotel, they got completely and totally soaked.<\/P>\n<TABLE class=\"dataTable\" id=\"ERD\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P><B>Note<\/B>. OK, so maybe they didn\u2019t <I>have<\/I> to walk for half an hour; truth be told, the walk would have been considerably shorter had they not walked right past the exit and essentially circled the entire park before finding the way out. But, then again, if they were capable of doing a simple thing like following the signs that say <I>Exit<\/I>, well, they really wouldn\u2019t be Scripting Guys, would they?<\/P>\n<P>And while he didn\u2019t say a word, we have no doubt that the taxi driver was <I>thrilled<\/I> to have two sopping wet Scripting Guys pile into the back of his previously-dry cab.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>The very next day the Scripting Guys showed up at the TechEd 2007 attendee party, and rode <I>Popeye and Bluto\u2019s Bilge-<\/I><I>Rat Barges<\/I> at Universal Studio\u2019s <A href=\"http:\/\/www.universalorlando.com\/ioa_index.html\" target=\"_blank\"><B>Island of Adventure<\/B><\/A> theme park. As you might expect, they got completely and totally soaked.<\/P>\n<TABLE class=\"dataTable\" id=\"EUE\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P><B>Note<\/B>. Here\u2019s a handy travel tip. Theme parks can be expensive; there\u2019s no two ways about that. However, with a little ingenuity you can experience many theme park rides in the comfort of your own home. For example, <A href=\"http:\/\/www.universalorlando.com\/ioa_attr_popeye.html\" target=\"_blank\"><B>to recreate the experience<\/B><\/A> of riding <I>Popeye and Bluto\u2019s<\/I> you can do this: sit down in a chair, and use a makeshift seatbelt to strap yourself in. Then have someone dump 15 or 20 buckets of water over your head. That\u2019s pretty much the whole ride right there.<\/P>\n<P>And, yes, now that you mention it, the Scripting Guys <I>did<\/I> ride this ride several times. In a row.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>After that somewhat damp experience what do you suppose Scripting Guy Greg Stemp did upon arriving back in Seattle? That\u2019s right: he went to his son\u2019s baseball game, a game played (at least until the umpires finally called it off) in a driving rainstorm. And needless to say, a game where he got completely and totally soaked.<\/P>\n<P>Apparently it really <I>is<\/I> true: the more things change the more they stay the same.<\/P>\n<P>Fortunately, that\u2019s also true when it comes to answering scripting-related questions. Before the Scripting Guys left for TechEd they tried to answer a new question each and every day. And now that they\u2019re back, well, like they say, the more things change, the more they stay the same:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>strMonth = Month(Date &#8211; 1)<\/p>\n<p>If Len(strMonth) = 1 Then\n    strMonth = &#8220;0&#8221; &amp; strMonth\nEnd If<\/p>\n<p>strDay = Day(Date &#8211; 1)<\/p>\n<p>If Len(strDay) = 1 Then\n    strDay = &#8220;0&#8221; &amp; strDay\nEnd If<\/p>\n<p>strYear = Year(Date &#8211; 1)<\/p>\n<p>strFileName = &#8220;C:\\\\Test\\\\BackupFile-&#8221; &amp; strMonth &amp; strDay &amp; strYear  &amp; &#8220;.txt&#8221;<\/p>\n<p>Set colFiles = objWMIService.ExecQuery _\n    (&#8220;Select * From CIM_DataFile Where Name = &#8216;&#8221; &amp; strFileName &amp; &#8220;&#8216;&#8221;)<\/p>\n<p>For Each objFile in colFiles<\/p>\n<p>    strMonth = Month(Date)<\/p>\n<p>    If Len(strMonth) = 1 Then\n        strMonth = &#8220;0&#8221; &amp; strMonth\n    End If<\/p>\n<p>    strDay = Day(Date)<\/p>\n<p>    If Len(strDay) = 1 Then\n        strDay = &#8220;0&#8221; &amp; strDay\n    End If<\/p>\n<p>    strYear = Year(Date)<\/p>\n<p>    strNewFileName = &#8220;C:\\\\Test\\\\BackupFile-&#8221; &amp; strMonth &amp; strDay &amp; strYear  &amp; &#8220;.txt&#8221;<\/p>\n<p>    errResult = objFile.Rename(strNewFileName)\nNext\n<\/PRE>\n<P>Sorry; hopefully you can read the script code OK. Looks like we\u2019re still dripping a little bit. And <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/teched07\/wednesday.mspx\"><B>Greg\u2019s hat<\/B><\/A> may never be the same again.<\/P>\n<P>But enough about all that. As you can see, even it first glance, this is a slightly-more complicated script than you might have expected. Why? Well, before we can rename a file we have to bind to that file using a script; in order to do that, we need to know the complete file path. And <I>that\u2019s<\/I> a problem: after all, each day the file is going to have a different name. (For example, if today this is named BackupFile-06122007.txt, then tomorrow it will be named BackupFile-06132007.txt, and so on.) Thus we have to do two things here: figure out the current name of the file, and <I>then<\/I> rename it.<\/P>\n<P>Sounds hard, doesn\u2019t it? However, because GZ needs to run this script every single day this turns out to be surprisingly easy.<\/P>\n<P>Let\u2019s show you what we mean by that. Our script starts off by connecting to the WMI service on the local computer (although this script works equally well against a file stored on a remote computer). This is the point where we would typically bind to the file in question. So why don\u2019t we do that now? That\u2019s easy: we don\u2019t know which file to bind to. Therefore, before we can do anything else we need to figure out the file path for our target file. And because part of that path (and part of the file name) consists of yesterday\u2019s date, we run this block of code:<\/P><PRE class=\"codeSample\">strMonth = Month(Date &#8211; 1)<\/p>\n<p>If Len(strMonth) = 1 Then\n    strMonth = &#8220;0&#8221; &amp; strMonth\nEnd If<\/p>\n<p>strDay = Day(Date &#8211; 1)<\/p>\n<p>If Len(strDay) = 1 Then\n    strDay = &#8220;0&#8221; &amp; strDay\nEnd If<\/p>\n<p>strYear = Year(Date &#8211; 1)\n<\/PRE>\n<P>What are we doing here? Well, to begin with, we use the <B>Month<\/B> function to determine the month for <I>yesterday\u2019s<\/I> date; that\u2019s what the <B>Date \u2013 1<\/B> is for (the current date minus 1 day). The Month function is going to return an integer value representing the month; for example, the function returns 10 if the yesterday\u2019s month fell in October, 12 if yesterday\u2019s month fell in December, and 6 if yesterday\u2019s month fell in June. <\/P>\n<P>And yes, you\u2019re right: we already have a problem, don\u2019t we? Suppose we used this date in the file name: 1112007. So is that 1\/11\/2007, or is that 11\/1\/2007? Truthfully, there\u2019s no way to tell. Therefore, we need to make sure that the months (and days) are listed as two-digit values, with leading zeroes used to pad the single-digit months. (In other words, modifying the month of June to be 06 rather than 6.)<\/P>\n<P>Fortunately, that\u2019s pretty easy to take care of: we just use the <B>Len<\/B> function to determine the number of characters in the month (technically, in the variable strMonth). If there\u2019s only one character in the month we then use this block of code to add a leading 0, changing 6 to 06:<\/P><PRE class=\"codeSample\">strMonth = &#8220;0&#8221; &amp; strMonth\n<\/PRE>\n<P>What if there are already two characters in the variable strMonth? Then we don\u2019t do anything at all; we\u2019re fine. And if there are <I>three<\/I> characters in the month? Then that means the Scripting Guys really need to stop taking their laptops on water rides like <I>Popeye and Bluto<\/I>. (It\u2019s just so hard to tear yourself away from email, you know?)<\/P>\n<P>We then repeat this process for the day, using the <B>Day<\/B> function to extract the day portion of yesterday\u2019s date and, again, adding a leading zero if needed:<\/P><PRE class=\"codeSample\">strDay = Day(Date &#8211; 1)<\/p>\n<p>If Len(strDay) = 1 Then\n    strDay = &#8220;0&#8221; &amp; strDay\nEnd If\n<\/PRE>\n<P>Finally, we use the <B>Year<\/B> function to assign the year for yesterday\u2019s date to a variable named strYear:<\/P><PRE class=\"codeSample\">strYear = Year(Date &#8211; 1)\n<\/PRE>\n<P>In this case there\u2019s no need to add a leading zero because, by default, years are reported back as four-digit values.<\/P>\n<P>After we have the individual pieces we can then construct yesterday\u2019s date \u2013 and, in turn, the current file path \u2013 by using this line of code:<\/P><PRE class=\"codeSample\">strFileName = &#8220;C:\\\\Test\\\\BackupFile-&#8221; &amp; strMonth &amp; strDay &amp; strYear  &amp; &#8220;.txt&#8221;\n<\/PRE>\n<P>As you can see, this is a pretty straightforward equation: we\u2019re just combining the file path and the first part of the file name (<B>BackupFile-<\/B>) with the variables representing the month, day, and year, tacking the <B>.txt<\/B> file extension on the end. The only tricky part here is that the \\ is a reserved character in WMI; among other things, that means that this character (like all reserved characters) must be \u201cescaped\u201d before that character can be used in a WMI Where clause. So how do you escape a character in WMI? That\u2019s easy: you just preface it with a \\. If you\u2019ve been sitting there wondering, \u201cWhy C:<B>\\\\<\/B>Test<B>\\\\<\/B>BackupFile-? Why not C:\\Test\\BackupFile-?\u201d well, now you know.<\/P>\n<P>After we\u2019ve figured out the current name of our file we then use a regular old <B>ExecQuery<\/B> call to bind to the file, using the variable strFileName to represent the file path:<\/P><PRE class=\"codeSample\">Set colFiles = objWMIService.ExecQuery _\n    (&#8220;Select * From CIM_DataFile Where Name = &#8216;&#8221; &amp; strFileName &amp; &#8220;&#8216;&#8221;)\n<\/PRE>\n<P>Assuming all goes according to plan (and with the Scripting Guys everything <I>always<\/I> goes according to plan; we just have a tendency to make really bad plans) we should get back a collection (with the name colFiles) consisting of all the files with the designated file path. And because file paths must be unique on a computer that means we\u2019ve located \u2013 and connected to \u2013 our target file.<\/P>\n<P>That also means that we\u2019re just about ready to rename the file. Before we can do that, though, we need to create a new name for our file. Fortunately we don\u2019t have to put much thought into that: we already know that the file is going to be named C:\\Test\\BackupFile-<I>insert current date here<\/I>-.txt. With that in mind we go ahead and repeat the same process we just finished: we grab the month, day, and year, albeit this time for <I>today\u2019s<\/I> date, not yesterday\u2019s date. Once we have all that data we construct a new file name and store that value in the variable strNewFileName:<\/P><PRE class=\"codeSample\">strNewFileName = &#8220;C:\\\\Test\\\\BackupFile-&#8221; &amp; strMonth &amp; strDay &amp; strYear  &amp; &#8220;.txt&#8221; \n<\/PRE>\n<P>Now all we need is one line of code and the <B>Rename<\/B> method and we can rename the file:<\/P><PRE class=\"codeSample\">errResult = objFile.Rename(strNewFileName\n<\/PRE>\n<P>And there you have it.<\/P>\n<P>Incidentally, we realize that many of you were unable to make it to the Scripting Son\u2019s baseball game, the one played in the torrential downpour. (But don\u2019t worry: he\u2019s got a good 50-60 games left this season.) If you\u2019re interested in knowing what the game was like, here\u2019s a handy hint: get in the car, drive for 50 miles, then sit down in a chair and have someone pour buckets of water over your head for the next two hours. And then get in the car and drive back home. That\u2019s pretty much all there was to it.<\/P>\n<TABLE class=\"dataTable\" id=\"E3AAC\" 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>. So are the Scripting Guys a little tired of spending all their time underwater? No, not really. After all, our <I>careers<\/I> have been underwater for years; we might as well join them.<\/P><\/TD><\/TR><\/TBODY><\/TABLE><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I have a file that has the name BackupFile-06122007.txt, where the 06122007 represents yesterday\u2019s date (June 12, 2007). Each morning I would like to run a script that changes the name of that file so that it uses today\u2019s date: BackupFile-06132007.txt. How can I do that?&#8212; GZ Hey, GZ. You know, one [&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":[38,3,12,5],"class_list":["post-64663","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-files","tag-scripting-guy","tag-storage","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I have a file that has the name BackupFile-06122007.txt, where the 06122007 represents yesterday\u2019s date (June 12, 2007). Each morning I would like to run a script that changes the name of that file so that it uses today\u2019s date: BackupFile-06132007.txt. How can I do that?&#8212; GZ Hey, GZ. You know, one [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64663","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=64663"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64663\/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=64663"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=64663"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=64663"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}