{"id":68673,"date":"2005-10-25T15:05:00","date_gmt":"2005-10-25T15:05:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/10\/25\/how-can-i-delete-a-backup-file-created-on-the-previous-day\/"},"modified":"2005-10-25T15:05:00","modified_gmt":"2005-10-25T15:05:00","slug":"how-can-i-delete-a-backup-file-created-on-the-previous-day","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-delete-a-backup-file-created-on-the-previous-day\/","title":{"rendered":"How Can I Delete a Backup File Created on the Previous Day?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! Each day we have a program that creates a file with a name similar to this: backup_20050607.bak. How can I delete the previous day\u2019s file?<BR><BR>&#8212; JC<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, JC. Hmmm, a script that goes out and automatically deletes whatever happened the day before. Boy, could we have used a script like <I>that<\/I> a few weeks ago!<\/P>\n<P>Yes, we know. But that\u2019s a story we probably shouldn\u2019t tell.<\/P>\n<P>Besides, we have a column to write here. You want a script that can delete a file that has a name like backup_20050607.bak, with the 20050607 representing the previous day (in this case, June 7, 2005)? All you had to do was ask:<\/P><PRE class=\"codeSample\">dtmYesterday = Date &#8211; 1<\/p>\n<p>strYear = Year(dtmYesterday)<\/p>\n<p>strMonth = Month(dtmYesterday)\nIf Len(strMonth) = 1 Then\n    strMonth = &#8220;0&#8221; &amp; strMonth\nEnd If<\/p>\n<p>strDay = Day(dtmYesterday)\nIf Len(strDay) = 1 Then\n    strDay = &#8220;0&#8221; &amp; strDay\nEnd If<\/p>\n<p>strYesterday = strYear &amp; strMonth &amp; strDay<\/p>\n<p>strFileName = &#8220;C:\\Backups\\backup_&#8221; &amp; strYesterday &amp; &#8220;.bak&#8221;<\/p>\n<p>Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nobjFSO.DeleteFile(strFileName)\n<\/PRE>\n<P>As you probably guessed, the tricky part here is constructing the file name; once we have that, deleting the file is a piece of cake. So then how do we construct that file name?<\/P>\n<P>Well, the one part of the file name that varies from day-to-day is the part that represents the date the backup file was generated. That means all we have to do is construct that portion of the file name; the rest can then be hard-coded in. Therefore, we start off by determining the date for the previous day; we do that by subtracting one day from the current date and then storing that value in a variable named dtmYesterday:<\/P><PRE class=\"codeSample\">dtmYesterday = Date &#8211; 1\n<\/PRE>\n<P>Next we use the <B>Year<\/B> function to grab the four-digit year value (e.g., <B>2005<\/B>) from dtmYesterday; that value gets stored in the variable strYear. Following that, we use the <B>Month<\/B> function to grab the month value from dtmYesterday.<\/P>\n<P>Note, however, that we have to do a little extra coding when it comes to the month (and to the day, too). Why? Well, suppose we\u2019re dealing with June. In that case, the Month function returns the value <B>6<\/B>. That\u2019s fine, except in our file name two digits are allocated for the month; the month needs to be listed as <B>06<\/B>. Therefore, we have to determine whether we have a one-digit month or a two-digit month. If it\u2019s one-digit month, we then have to place a leading zero in front of the number. That\u2019s what we do here:<\/P><PRE class=\"codeSample\">If Len(strMonth) = 1 Then\n    strMonth = &#8220;0&#8221; &amp; strMonth\nEnd If\n<\/PRE>\n<P>This is actually pretty straightforward code. The <B>Len<\/B> (length) function tells us how many digits are in the variable strMonth. If it\u2019s just 1, then we add a leading 0:<\/P><PRE class=\"codeSample\">strMonth = &#8220;0&#8221; &amp; strMonth\n<\/PRE>\n<P>If the length is equal to anything other than 1, then we leave well enough alone.<\/P>\n<P>After grabbing the month value we repeat this exact same process with the <B>Day<\/B> function, enabling us to retrieve the day portion of the date. That gets stashed in a variable cleverly-named strDay.<\/P>\n<P>And <I>that<\/I> gives us all the pieces we need to construct the file name. To do so, we first jam together the year, month, and day (giving us a string like <B>20050607<\/B>) using this line of code:<\/P><PRE class=\"codeSample\">strYesterday = strYear &amp; strMonth &amp; strDay\n<\/PRE>\n<P>Then we simply tack on the rest of the path information (in this example, we\u2019re assuming that the file is stored in the folder C:\\Backups):<\/P><PRE class=\"codeSample\">strFileName = &#8220;C:\\Backups\\backup_&#8221; &amp; strYesterday &amp; &#8220;.bak&#8221;\n<\/PRE>\n<P>As you can see, we just combine the strings <B>C:\\Backups\\backup_<\/B>, the date we constructed (<B>20050607<\/B>), and <B>.bak<\/B>. Put them all together and you\u2019ll end up with something like this:<\/P><PRE class=\"codeSample\">C:\\Backups\\backup_20050607.bak\n<\/PRE>\n<P>Cool.<\/P>\n<P>We then use these two lines of code to create an instance of the FileSystemObject and delete the file:<\/P><PRE class=\"codeSample\">Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nobjFSO.DeleteFile(strFileName)\n<\/PRE>\n<P>Because we used the FileSystemObject, this particular script will work only on the local machine. But what if the backup file is located on a remote computer? No problem; in that case we can use WMI to locate and remove the file. So then why didn\u2019t we use WMI in the first place? Well, the FileSystemObject can locate and delete the file in less than a second; WMI will take a little longer. We decided to go with speed and efficiency.<\/P>\n<P>But, like we said, that speed and efficiency won\u2019t do you much good if the file is on a remote computer. Fortunately, in a situation like that you can use a WMI script to delete the file. This script (which we won\u2019t discuss today) deletes the backup file from a remote computer named atl-fs-01:<\/P><PRE class=\"codeSample\">dtmYesterday = Date &#8211; 1<\/p>\n<p>strYear = Year(dtmYesterday)<\/p>\n<p>strMonth = Month(dtmYesterday)\nIf Len(strMonth) = 1 Then\n    strMonth = &#8220;0&#8221; &amp; strMonth\nEnd If<\/p>\n<p>strDay = Day(dtmYesterday)\nIf Len(strDay) = 1 Then\n    strDay = &#8220;0&#8221; &amp; strDay\nEnd If<\/p>\n<p>strYesterday = strYear &amp; strMonth &amp; strDay<\/p>\n<p>strFileName = &#8220;C:\\\\Backups\\\\backup_&#8221; &amp; strYesterday &amp; &#8220;.bak&#8221;<\/p>\n<p>strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#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\n    objFile.Delete\nNext\n<\/PRE>\n<P>Just like that, yesterday\u2019s gone, and we can all pretend that it never happened. Right, boss?<\/P>\n<TABLE id=\"EAF\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. Couldn\u2019t we have saved ourselves all this trouble and simply deleted all the files with a creation\/modification date equivalent to the previous day\u2019s date? Yes, provided that there would never be any other files stored in that folder that would have the same date and might inadvertently be deleted. All things considered, this seemed like the safest approach.<\/P><\/TD><\/TR><\/TBODY><\/TABLE><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! Each day we have a program that creates a file with a name similar to this: backup_20050607.bak. How can I delete the previous day\u2019s file?&#8212; JC Hey, JC. Hmmm, a script that goes out and automatically deletes whatever happened the day before. Boy, could we have used a script like that a [&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,40,3,12,5],"class_list":["post-68673","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-files","tag-filesystemobject","tag-scripting-guy","tag-storage","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! Each day we have a program that creates a file with a name similar to this: backup_20050607.bak. How can I delete the previous day\u2019s file?&#8212; JC Hey, JC. Hmmm, a script that goes out and automatically deletes whatever happened the day before. Boy, could we have used a script like that a [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68673","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=68673"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68673\/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=68673"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68673"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68673"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}