{"id":69083,"date":"2005-08-26T18:22:00","date_gmt":"2005-08-26T18:22:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/08\/26\/how-can-i-determine-the-20-largest-files-on-a-computer\/"},"modified":"2005-08-26T18:22:00","modified_gmt":"2005-08-26T18:22:00","slug":"how-can-i-determine-the-20-largest-files-on-a-computer","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-determine-the-20-largest-files-on-a-computer\/","title":{"rendered":"How Can I Determine the 20 Largest Files on a Computer?"},"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 determine the 20 largest files on a computer?<BR><BR>&#8212; PW<\/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, PW. Having recently returned from a vacation in Europe, the Scripting Guy who writes this column now understands that there are different ways to do things. Some of these would be difficult to adjust to on a regular basis: for example, ordering a soft drink in a restaurant and getting a teeny-tiny little glass with no ice and without free refills still seems like a crime against humanity. Other things &#8211; like getting 6-8 weeks of paid vacation each year &#8211; would be a bit easier to accept. The point is not that any of these are good (crepes smothered in dark chocolate sauce) or bad (pastries filled with &#8211; and we are not making this up &#8211; snails). The point is that it\u2019s OK to do and to try different things.<\/P>\n<P>Which is just a way of rationalizing the fact that, unlike our usual approach, this time we aren\u2019t going to provide you with a solution that\u2019s built into the operating system. Determining the 20 largest files on a computer is theoretically possible using nothing more than WMI; however, the script would be a bit cumbersome to write, and might take hundreds of thousands of years to run. (Which, coincidentally enough, is the same amount of time that elapses between the moment you finish your meal and the moment your Parisian waiter brings you the check.) If WMI represented the only way to find the 20 largest files we\u2019d bite the old bullet and do that. But there\u2019s a much better and much easier way to do this: download, install, and use <A href=\"http:\/\/null\/technet\/scriptcenter\/tools\/logparser\/default.mspx\"><B>Log Parser 2.2<\/B><\/A>.<\/P>\n<P>If you aren\u2019t familiar with Log Parser 2.2, you should take a look at this <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/tales\/sg0105.mspx\"><B><I>Tales from the Script<\/I><\/B><\/A> column. Log Parser is a nifty little utility that, as the name implies, makes it quick and easy to parse plain-text log files. However, Log Parser also makes it quick and easy to parse event logs, the file system, the registry, even Active Directory. After you\u2019ve installed Log Parser, retrieving (and sorting) the 20 largest files on a computer requires just a couple minutes of processing time, and involves a script no more complicated than this:<\/P><PRE class=\"codeSample\">Set objLogParser = CreateObject(&#8220;MSUtil.LogQuery&#8221;)\nSet objInputFormat = CreateObject(&#8220;MSUtil.LogQuery.FileSystemInputFormat&#8221;)\nobjInputFormat.Recurse = -1<\/p>\n<p>Set objOutputFormat = CreateObject(&#8220;MSUtil.LogQuery.NativeOutputFormat&#8221;)\nobjOutputFormat.rtp = -1<\/p>\n<p>strQuery = &#8220;SELECT Top 20 Path, Size FROM &#8216;C:\\*.*, D:\\*.*&#8217; ORDER BY Size DESC&#8221;\nobjLogParser.ExecuteBatch strQuery, objInputFormat, objOutputFormat\n<\/PRE>\n<P>We won\u2019t explain this script in great detail today; for that level of information you should take a look at the <I>Tales from the Script<\/I> column. We will note, however, that the script begins by creating an instance of the <B>MSUtil.LogQuery<\/B> object, then uses this line of code to indicate that we want to work with items in the file system:<\/P><PRE class=\"codeSample\">Set objInputFormat = CreateObject(&#8220;MSUtil.LogQuery.FileSystemInputFormat&#8221;)\n<\/PRE>\n<P>We then set the value of the <B>Recurse<\/B> property to -1; that tells Log Parser that we want to recursively search through all the folders in the specified path. (And, no, we haven\u2019t specified the path yet; we\u2019ll do that in a minute.)<\/P>\n<P>Those initial lines of code set up the input parameters; we then use the next two lines of code to set up the output parameters:<\/P><PRE class=\"codeSample\">Set objOutputFormat = CreateObject(&#8220;MSUtil.LogQuery.NativeOutputFormat&#8221;)\nobjOutputFormat.rtp = -1\n<\/PRE>\n<P>Again, without going into any great detail, the first line tells Log Parser to output data to the command window; setting the <B>rtp<\/B> property to -1 tells Log Parser to write all the data at once, without pausing after each screen and waiting for the user to press a key to continue. With Log Parser you aren\u2019t limited to outputting data to the command window, but &#8211; for now &#8211; this seemed like the easiest and most intuitive approach.<\/P>\n<P>With the input and output parameters taken care of we next create our Log Parser query:<\/P><PRE class=\"codeSample\">strQuery = &#8220;SELECT Top 20 Path, Size FROM &#8216;C:\\*.*, D:\\*.*&#8217; ORDER BY Size DESC&#8221;\n<\/PRE>\n<P>If you have some experience at writing SQL queries this particular line of code should be pretty straightforward. All we\u2019re doing here is asking for the 20 largest files; to do that we request the \u201cTop 20\u201d files, sorted by size in descending order. When we make that request Log Parser will grab <I>all<\/I> the files and sort them in descending order; because we requested only the first (top) 20, however, the only data that will be displayed onscreen will be the 20 largest files. What if we wanted the 50 largest files? Then we\u2019d ask for the Top 50 files:<\/P><PRE class=\"codeSample\">strQuery = &#8220;SELECT Top 50 Path, Size FROM &#8216;C:\\*.*, D:\\*.*&#8217; ORDER BY Size DESC&#8221;\n<\/PRE>\n<P>What if we wanted the 20 <I>smallest<\/I> files? In that case, we\u2019d sort the files in ascending order, which would put the smallest files at the top of the list:<\/P><PRE class=\"codeSample\">strQuery = &#8220;SELECT Top 20 Path, Size FROM &#8216;C:\\*.*, D:\\*.*&#8217; ORDER BY Size ASC&#8221;\n<\/PRE>\n<P>See? We told you it was pretty straightforward.<\/P>\n<P>As you can see, we\u2019re asking only for the file path and the file size; needless to say, we could get other information about the files as well. (See the Log Parser documentation for more information.) Also, note that we need to indicate each of the drives that we want to search, separating the drives by commas:<\/P><PRE class=\"codeSample\">strQuery = &#8220;SELECT Top 20 Path, Size FROM &#8216;C:\\*.*, D:\\*.*&#8217; ORDER BY Size DESC&#8221;\n<\/PRE>\n<P>By the way, <B>*.*<\/B> is a standard file system wildcard meaning \u201call the files, regardless of file name or extension.\u201d If we were interested in only the 20 largest Microsoft Word documents we would modify our query like so:<\/P><PRE class=\"codeSample\">strQuery = &#8220;SELECT Top 20 Path, Size FROM &#8216;C:\\*.doc, D:\\*.doc&#8217; ORDER BY Size DESC&#8221;\n<\/PRE>\n<P>Save the script as a .vbs file and have at it; you\u2019ll be surprised at how quickly you\u2019ll get back information regarding the 20 largest files on the computer.<\/P>\n<P>Although probably not as surprised as we were to discover that a traditional English breakfast includes both baked beans and a grilled tomato. But hey, food is food, right?<\/P>\n<P>Unless, of course, there happens to be snails in it.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I determine the 20 largest files on a computer?&#8212; PW Hey, PW. Having recently returned from a vacation in Europe, the Scripting Guy who writes this column now understands that there are different ways to do things. Some of these would be difficult to adjust to on a regular basis: [&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,98,236,3,12,5],"class_list":["post-69083","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-files","tag-logs-and-monitoring","tag-plain-text-logs","tag-scripting-guy","tag-storage","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I determine the 20 largest files on a computer?&#8212; PW Hey, PW. Having recently returned from a vacation in Europe, the Scripting Guy who writes this column now understands that there are different ways to do things. Some of these would be difficult to adjust to on a regular basis: [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69083","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=69083"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69083\/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=69083"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=69083"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=69083"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}