{"id":71073,"date":"2004-11-04T17:39:00","date_gmt":"2004-11-04T17:39:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2004\/11\/04\/how-can-i-delete-all-files-older-than-a-specified-date\/"},"modified":"2004-11-04T17:39:00","modified_gmt":"2004-11-04T17:39:00","slug":"how-can-i-delete-all-files-older-than-a-specified-date","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-delete-all-files-older-than-a-specified-date\/","title":{"rendered":"How Can I Delete All Files Older Than a Specified Date?"},"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\u2019d like to have a script that can search my computer for all files older than a certain date, and then automatically delete those files. Can I do that?<BR><BR>&#8212; GM<\/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, GM. Can you write a script that will search for and delete all the old files on your computer? You bet. <I>Should<\/I> you write a script that searches for and deletes all the old files on your computer? That\u2019s a separate question that we\u2019ll deal with in a minute.<\/P>\n<P>Let\u2019s start with the code itself. Here\u2019s a script that searches for all the files on your computer that were created before November 2, 2003 and then echoes back the name of each file:<\/P><PRE class=\"codeSample\">strDate = &#8220;20031102000000.000000+000&#8221;<\/p>\n<p>strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject _\n    (&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\nSet colFiles = objWMIService.ExecQuery _\n    (&#8220;Select * From CIM_DataFile Where CreationDate &lt; &#8216;&#8221; &amp; strDate &amp; &#8220;&#8216;&#8221;)\nFor Each objFile in colFiles\n    Wscript.Echo objFile.Name\nNext\n<\/PRE>\n<P>We know what you\u2019re thinking: how does the script know that our target date is November 2, 2003? After all, November 2, 2003 doesn\u2019t appear <I>anywhere<\/I> in the script.<\/P>\n<P>Believe it or not, it does; it might not look like a date, but <B>20031102000000.000000+000<\/B> &#8211; the value we assign to the variable strDate &#8211; represents November 2, 2003. The date looks weird because WMI uses the UTC (Universal Time Coordinate) format. With the UTC format, the first four digits (<B>2003<\/B>) represent the year; the next two (<B>11<\/B>) represent the month; and the next two (<B>02<\/B>) represent the day. In other words, <B>20031102<\/B> is the same as <B>11\/02\/2003<\/B>.<\/P>\n<P>Incidentally, the next six digits represent the hour, minute, and seconds, in 24-hour format; we\u2019ve left these at 0 because we aren\u2019t worried about a specific time (that is, we\u2019re not looking for files created before 2:37 PM on November 2, 2003). The six digits after the period represent milliseconds; those should always be left as zeroes. Finally, the <B>+000<\/B> represents the offset from Greenwich Mean Time. This offset allows you to coordinate dates and times between computers in different time zones, which is something we aren\u2019t going to worry about today. Instead, we left the offset at <B>+000<\/B>, which tells the computer to work with the local time.<\/P>\n<P>So do we <I>have<\/I> to pass the date and time in this UTC format? Yes. And in Windows 2000 (and prior versions of Windows) you\u2019ll have to type the data in like we did here. In Windows XP and Windows 2003, however, there\u2019s a new WMI object &#8211; SWbemDateTime &#8211; that allows you to type in a regular old date (like 11\/2\/2003), and then automatically converts that date to UTC format.<\/P>\n<TABLE class=\"dataTable\" id=\"EEE\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">Want to know more about SWbemDateTime? Watch the webcast \u201cThing the Scripting Guys Never Told You,\u201d from <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/webcasts\/archive.mspx#ECAA\"><B>Scripting Week 2<\/B><\/A>.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>After assigning the date to strDate what we have left is just a plain old WMI script, one that searches for all the files with a <B>CreationDate<\/B> earlier than 11\/2\/2003. When we find one, we echo the file name.<\/P>\n<P>Of course, you asked for a script that <I>deletes<\/I> old files. And if that\u2019s what you want to do, then just replace the line <B>Wscript.Echo objFile.Name<\/B> with this line of code:<\/P><PRE class=\"codeSample\">objFile.Delete\n<\/PRE>\n<P>Why didn\u2019t we put this line of code in the script for you? Well, we wanted you to think about this script before actually running it. Suppose you have one disk drive (C) on your computer. Do you <I>really<\/I> want to run a script that deletes all the files created earlier than November 2, 2003? After all, that\u2019s going to delete most of your operating system and application files. Generally speaking, not the sort of thing you want to do.<\/P>\n<P>What that means is that you might need to be a bit more clever when searching for files. For example, maybe your computer has two drives: drive C has your operating system and all your applications, and drive D has all your documents. In that case, you could write a WQL query that searches only drive D for files created before November 2, 2003:<\/P><PRE class=\"codeSample\">strDate = &#8220;20031102000000.000000+000&#8221;<\/p>\n<p>strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject _\n    (&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\nSet colFiles = objWMIService.ExecQuery _\n    (&#8220;Select * From CIM_DataFile Where CreationDate &lt; &#8216;&#8221; &amp; strDate &amp; &#8220;&#8216;&#8221; &amp; _\n        &#8221; AND Drive = &#8216;D:'&#8221;)\nFor Each objFile in colFiles\n    Wscript.Echo objFile.Name\nNext\n<\/PRE>\n<P>Alternatively, maybe your concern is with old and out-of-date Word documents. In that case, search only for files with a <B>doc<\/B> file extension that were created prior to November 2, 2003:<\/P><PRE class=\"codeSample\">strDate = &#8220;20031102000000.000000+000&#8221;<\/p>\n<p>strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject _\n    (&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\nSet colFiles = objWMIService.ExecQuery _\n    (&#8220;Select * From CIM_DataFile Where CreationDate &lt; &#8216;&#8221; &amp; strDate &amp; &#8220;&#8216;&#8221; &amp; _\n        &#8221; AND Extension = &#8216;doc'&#8221;)\nFor Each objFile in colFiles\n    Wscript.Echo objFile.Name\nNext\n<\/PRE>\n<P>There are plenty of other ways to finely-hone your searches; for more details, see the <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/guide\/sas_fil_overview.mspx\"><B>Files and Folders<\/B><\/A> chapter in the Microsoft Windows 2000 Scripting Guide.<\/P>\n<P>Incidentally a lot of people ask if it\u2019s possible to search for files based on the last time those files were modified. Of course; just take one of the preceding scripts, cross out CreationDate, and write in <B>LastModified<\/B>. In other words:<\/P><PRE class=\"codeSample\">strDate = &#8220;20031102000000.000000+000&#8221;<\/p>\n<p>strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject _\n    (&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\nSet colFiles = objWMIService.ExecQuery _\n    (&#8220;Select * From CIM_DataFile Where LastModified &lt; &#8216;&#8221; &amp; strDate &amp; &#8220;&#8216;&#8221;)\nFor Each objFile in colFiles\n    Wscript.Echo objFile.Name\nNext\n<\/PRE><BR>\n<DIV>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"0\" width=\"100%\" border=\"0\">\n<TBODY>\n<TR>\n<TD class=\"\"><A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/nov04\/hey1104.mspx#top\"><IMG height=\"9\" alt=\"Top of page\" src=\"http:\/\/www.microsoft.com\/technet\/mnplibrary\/templates\/MNP2.Common\/images\/arrow_px_up.gif\" width=\"7\" border=\"0\"><\/A><A class=\"topOfPage\" href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/nov04\/hey1104.mspx#top\">Top of page<\/A><\/TD><\/TR><\/TBODY><\/TABLE><\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I\u2019d like to have a script that can search my computer for all files older than a certain date, and then automatically delete those files. Can I do that?&#8212; GM Hey, GM. Can you write a script that will search for and delete all the old files on your computer? You bet. [&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-71073","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\u2019d like to have a script that can search my computer for all files older than a certain date, and then automatically delete those files. Can I do that?&#8212; GM Hey, GM. Can you write a script that will search for and delete all the old files on your computer? You bet. [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71073","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=71073"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71073\/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=71073"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=71073"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=71073"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}