{"id":68833,"date":"2005-10-03T11:43:00","date_gmt":"2005-10-03T11:43:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/10\/03\/how-can-i-list-all-the-files-in-a-folder-ordered-by-creation-date\/"},"modified":"2005-10-03T11:43:00","modified_gmt":"2005-10-03T11:43:00","slug":"how-can-i-list-all-the-files-in-a-folder-ordered-by-creation-date","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-list-all-the-files-in-a-folder-ordered-by-creation-date\/","title":{"rendered":"How Can I List All the Files in a Folder, Ordered By Creation Date?"},"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! How can I list all the files in a folder, ordered by creation date?<BR><BR>&#8212; CL<\/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, CL. You know, if we were ambitious and hard-working, we\u2019d sit down and write you a script that uses WMI to return all the files in a folder. That script would grab information about all those files and store that data in a disconnected recordset. We\u2019d then set the sort order on that recordset to arrange the files by creation date and time. (Well, after we went through a lot of gyrations to convert WMI\u2019s date-time values to a readable date-time format.) Finally we\u2019d echo all the values in the recordset back to the screen. It would take a lot of time and involve a lot of coding, but in the end you\u2019d have your sorted list of files and everyone would say, \u201cWow, those Scripting Guys really go the extra mile for their readers, don\u2019t they?\u201d<\/P>\n<P>As it turns out, though, we <I>aren\u2019t<\/I> ambitious and hard-working; instead, we\u2019re the Scripting Guys. And as the Scripting Guys we\u2019re always looking for the quickest and easiest way to solve a problem. Therefore, instead of writing a long and complicated script we grabbed a copy of <A href=\"http:\/\/null\/technet\/scriptcenter\/tools\/logparser\/default.mspx\"><B>Log Parser 2.2<\/B><\/A> and dashed off just a few lines of code:<\/P><PRE class=\"codeSample\">Set objLogParser = CreateObject(&#8220;MSUtil.LogQuery&#8221;)\nSet objInputFormat = CreateObject(&#8220;MSUtil.LogQuery.FileSystemInputFormat&#8221;)\nobjInputFormat.Recurse = 0<\/p>\n<p>Set objOutputFormat = CreateObject(&#8220;MSUtil.LogQuery.NativeOutputFormat&#8221;)\nobjOutputFormat.rtp = -1<\/p>\n<p>strQuery = &#8220;SELECT Name, CreationTime FROM &#8216;C:\\Scripts\\*.*&#8217; &#8221; &amp; _\n    &#8220;WHERE NOT Attributes LIKE &#8216;%D%&#8217; ORDER BY CreationTime&#8221;\nobjLogParser.ExecuteBatch strQuery, objInputFormat, objOutputFormat\n<\/PRE>\n<P>And guess what? Not only does this script work, but it returns the list of files &#8211; sorted by creation date and time &#8211; in practically no time whatsoever. Very cool.<\/P>\n<P>Admittedly, we usually don\u2019t recommend solutions that aren\u2019t built into the operating system; that\u2019s because we hate to make people download and install something if it\u2019s not absolutely necessary. When it comes to enumerating files, however, it\u2019s more than worth your while to download and install Log Parser; any time you have to grab information about a set of files you\u2019ll find Log Parser <I>way<\/I> better than either WMI or the FileSystemObject. Seven or eight lines of Log Parser code versus 60 or 70 lines of WMI code? We\u2019ll leave that decision up to you.<\/P>\n<P>We won\u2019t take time to explain everything there is to know about Log Parser; for more information, you might want to see the <I>Tales from the Script<\/I> column <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/tales\/sg0105.mspx\"><B>All You Need is Log (Well, Log Parser)<\/B><\/A>. For now, we\u2019ll just note that the script begins by creating an instance of the Log Parser object, better known by the catchy name <B>MSUtil.LogQuery<\/B>. We then create two additional objects, the first specifying the object we\u2019re working with (in this case, the file system, though we could also work with event logs, Active Directory, the registry, and other items), the second indicating the output type we want to use (in this sample script, all we\u2019re doing is writing data to the command window). These two lines of code create the input object and tell Log Parser <I>not<\/I> to retrieve files from any subfolders:<\/P><PRE class=\"codeSample\">Set objInputFormat = CreateObject(&#8220;MSUtil.LogQuery.FileSystemInputFormat&#8221;)\nobjInputFormat.Recurse = 0\n<\/PRE>\n<P>What if we <I>did<\/I> want to retrieve values for any and all subfolders? In that case all we\u2019d have to do is set the value of the <B>Recurse<\/B> property to -1:<\/P><PRE class=\"codeSample\">objInputFormat.Recurse = -1\n<\/PRE>\n<P>Meanwhile, these two lines of code create the output object, and tell Log Parser to display all the data without pausing:<\/P><PRE class=\"codeSample\">Set objOutputFormat = CreateObject(&#8220;MSUtil.LogQuery.NativeOutputFormat&#8221;)\nobjOutputFormat.rtp = -1\n<\/PRE>\n<P>Alternatively, we could have told Log Parser to show us 10 rows of data, pause until we pressed a key on the keyboard, then show us the next 10 rows of data. To show data in batches of 10 all we\u2019d have to do is set the value of the <B>rtp<\/B> property to 10:<\/P><PRE class=\"codeSample\">objOutputFormat.rtp = 10\n<\/PRE>\n<P>Next we configure the SQL query for retrieving file information. If you have a little knowledge about SQL this query should be relatively easy to parse; as you can see, we\u2019re just asking for the Name and CreationTime of all the files in C:\\Scripts. In addition, we want the returned data ordered by creation date and time, with the oldest files listed first:<\/P><PRE class=\"codeSample\">strQuery = &#8220;SELECT Name, CreationTime FROM &#8216;C:\\Scripts\\*.*&#8217; &#8221; &amp; _\n    &#8220;WHERE NOT Attributes LIKE &#8216;%D%&#8217; ORDER BY CreationTime DESC&#8221;\n<\/PRE>\n<P>The only unusual thing in this query is our WHERE clause: <B>WHERE NOT Attributes LIKE &#8216;%D%<\/B>&#8216;. Without going into a long explanation, this clause filters out folders, and thus returns only files. A file system object that contains the Directory attribute is a folder; because we don\u2019t <I>want<\/I> folders we use the WHERE NOT syntax to eliminate any object possessing the Directory (%D% for short) attribute.<\/P>\n<P>Finally we call the <B>ExecuteBatch<\/B> method to run the query and write the returned data to the command window. And, in a second or two, we get back something that looks like this:<\/P><IMG border=\"0\" alt=\"Log Parser 2.0\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/lp1.jpg\" width=\"375\" height=\"252\"> \n<P>And, no, we didn\u2019t have to enter any special commands to get this nice tabular output; Log Parser takes care of all that for us. That <I>is<\/I> really nice of it, isn\u2019t it?<\/P>\n<P>Granted, we didn\u2019t have to work very hard to get back these results. But it\u2019s quick and it\u2019s easy. And look at it this way: nobody has to <I>know<\/I> we didn\u2019t work very hard, do they?<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I list all the files in a folder, ordered by creation date?&#8212; CL Hey, CL. You know, if we were ambitious and hard-working, we\u2019d sit down and write you a script that uses WMI to return all the files in a folder. That script would grab information about all those [&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,719,98,3,12,5],"class_list":["post-68833","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-files","tag-log-parser","tag-logs-and-monitoring","tag-scripting-guy","tag-storage","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I list all the files in a folder, ordered by creation date?&#8212; CL Hey, CL. You know, if we were ambitious and hard-working, we\u2019d sit down and write you a script that uses WMI to return all the files in a folder. That script would grab information about all those [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68833","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=68833"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68833\/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=68833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68833"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}