{"id":56003,"date":"2008-03-14T00:16:00","date_gmt":"2008-03-14T00:16:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2008\/03\/14\/hey-scripting-guy-how-can-i-find-the-number-and-size-of-all-the-files-recently-modified-or-added-to-a-computer\/"},"modified":"2008-03-14T00:16:00","modified_gmt":"2008-03-14T00:16:00","slug":"hey-scripting-guy-how-can-i-find-the-number-and-size-of-all-the-files-recently-modified-or-added-to-a-computer","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-find-the-number-and-size-of-all-the-files-recently-modified-or-added-to-a-computer\/","title":{"rendered":"Hey, Scripting Guy! How Can I Find the Number (and Size) of All the Files Recently Modified or Added to a Computer?"},"content":{"rendered":"<p><img decoding=\"async\" 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\" \/> <\/p>\n<p>Hey, Scripting Guy! I&#8217;m looking for a script that can search a file server and list the number and total size of files that have been recently modified or added to the server. Any help would be appreciated!<\/p>\n<p>&#8212; KT<\/p>\n<p><img decoding=\"async\" height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\" \/><img decoding=\"async\" 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 decoding=\"async\" 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> <\/p>\n<p>Hey, KT. Before we launch into today\u2019s column we thought we should tell you that the rumor is true: Scripting Guy Dean Tsaltas is actually a real, live Sasquatch, the fabled \u201cBigfoot\u201d monster of the Pacific Northwest. Dean has been trying to keep his true identity a secret for many years, but \u2013 oh, we see: you meant the <i>other<\/i> rumor. Well, the other rumor happens to be true, too: the special edition Winter Scripting Games <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/funzone\/games\/games08\/prizes.mspx\"><b>bobbleheads<\/b><\/a> have arrived, and are ready to be mailed out to 250 lucky winners.<\/p>\n<table class=\"dataTable\" id=\"EFD\" 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>. That thing about Dean being a real, live Sasquatch? That was just \u2026 a mistake \u2026. Yeah, a mistake: Dean just doesn\u2019t shave as often as he should. But just because he doesn\u2019t shave very often that doesn\u2019t <a href=\"http:\/\/www.pantheon.org\/articles\/s\/sasquatch.html\" target=\"_blank\"><b>make him a Sasquatch<\/b><\/a>. And neither does the fact that he is 11-feet 2-inches tall with long arms, an ape-like face with a flat nose, and thick hairy fur. Doesn\u2019t make him a Sasquatch at all.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>Anyway, the bobbleheads have arrived, which actually took us by surprise: after all, they weren\u2019t due until the middle of March. <\/p>\n<table class=\"dataTable\" id=\"EXD\" 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>. OK, granted, it <i>is<\/i> the middle of March. But last year the bobbleheads were supposed to arrive in the middle of February, and we didn\u2019t get them until April. As a result, we weren\u2019t ready for 14 huge boxes of bobbleheads to arrive. If you could see Scripting Guy Greg Stemp\u2019s office right now we would be amazed, because you can barely even get <i>in<\/i> Scripting Guy Greg Stemp\u2019s office right now.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>All of that means we can now go ahead and draw names and pick the winners of the bobbleheads, along with the winners of all the other <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/funzone\/games\/games08\/prizes.mspx\"><b>great prizes<\/b><\/a> being given away as part of the <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/funzone\/games\/games08.mspx\"><b>2008 Winter Scripting Games<\/b><\/a>. Sometime today we\u2019ll start sending out emails notifying people of what they have won, if anything. But be patient: between the bobbleheads, the Certificates of Excellence, and all the other prizes we have <i>hundreds<\/i> of emails to send out. That will likely take us a few days.<\/p>\n<p>And, unfortunately, Dean won\u2019t be able to help us with those emails. He called earlier today and said he would be stuck on the <a href=\"http:\/\/www.mdvaden.com\/collings_mountain.shtml\" target=\"_blank\"><b>Collings Mountain Trail<\/b><\/a> in southern Oregon for awhile. And no, he didn\u2019t say why.<\/p>\n<p>So what are you supposed to do while you wait to hear if you won a Scripting Games prize? Nothing; you should simply stare at your email inbox and wait until a message from the Scripting Guys arrives (assuming you\u2019ve won something; if not no message will arrive and, well, you could be staring at your inbox for a while).<\/p>\n<p>Or, if you find that a bit too boring, you could read the rest of this column, and find out how to get the total number (and total size) of all the files created or modified on a computer since a specified date.<\/p>\n<p>And no, we have no suggestions as to what you can do if you find this column a bit too boring, too. At that point, you\u2019re on your own.<\/p>\n<p>Let\u2019s start by taking a look at a script that returns a collection of all the files that have been added to a computer since March 12, 2008:<\/p>\n<pre class=\"codeSample\">strMonth = \"03\"\nstrDay = \"12\"\nstrYear = \"2008\"\n\nstrDate = strYear &amp; strMonth &amp; strDay &amp; \"000000.000000+000\"\n\nintNumberOfFiles = 0\nintTotalSize = 0\n\nstrComputer = \".\"\n\nSet objWMIService = GetObject(\"winmgmts:\\\\\" &amp; strComputer &amp; \"\\root\\cimv2\")\n\nSet colFiles = objWMIService.ExecQuery _\n    (\"Select * From CIM_DataFile Where CreationDate &gt;= '\" &amp; strDate &amp; \"'\")\n\nFor Each objFile in colFiles\n    intNumberOfFiles = intNumberOfFiles + 1\n    intTotalSize = intTotalSize + objFile.FileSize\nNext\n\nWscript.Echo \"Total files added: \" &amp; intNumberOfFiles\nWscript.Echo \"Total size of the new files: \" &amp; intTotalSize\n<\/pre>\n<p>As it turns out, it\u2019s fairly easy \u2013 though a bit time-consuming \u2013 to retrieve a collection of <i>all<\/i> the files found on all the drives of a computer; that\u2019s because each file is a member of the WMI class <b>CIM_DataFile<\/b>. The tricky part is working with the creation and last modified dates of these files; that\u2019s because WMI uses the Universal Time Coordinate (UTC) format for storing date-time values. What does that mean? That means that a date like March 12, 2008 looks like this:<\/p>\n<pre class=\"codeSample\">20080312000000.000000+000\n<\/pre>\n<p>Believe it or not, there <i>is<\/i> method to that madness. The <b>2008<\/b> at the beginning of the string represents the year; the <b>03<\/b> represents the month (note the leading zero); and the <b>12<\/b> represents the day. (For more information on the UTC format, see the <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/guide\/sas_wmi_fvwp.mspx\"><b>Microsoft Windows 2000 Scripting Guide<\/b><\/a>.) What we need to do before we do anything else is take a date like March 12, 2008 and convert it to UTC format.<\/p>\n<p>Which is exactly what we do with this block of code:<\/p>\n<pre class=\"codeSample\">strMonth = \"03\"\nstrDay = \"12\"\nstrYear = \"2008\"\n\nstrDate = strYear &amp; strMonth &amp; strDay &amp; \"000000.000000+000\"\n<\/pre>\n<p>All we\u2019re doing here is assigning the month, day, and year to the variables strMonth, strDay, and strYear. (Note, again, the leading zeroes that must be used for any month or day less than 10. Also note that the year <i>must<\/i> include four digits.) Once we\u2019ve done that we then use this line of code to combine these values and assign the resulting string to a variable named strDate:<\/p>\n<pre class=\"codeSample\">strDate = strYear &amp; strMonth &amp; strDay &amp; \"000000.000000+000\"\n<\/pre>\n<p>And what will strDate be equal to? You got it:<\/p>\n<pre class=\"codeSample\">20080312000000.000000+000\n<\/pre>\n<p>As soon as we\u2019ve taken care of the date the rest is easy. To begin with, we assign the value 0 to the variables intNumberOfFiles and intTotalSize; we\u2019ll talk about these two variables in a few minutes. From there we proceed to connect to the WMI service on the local computer. Can we run this script against a remote computer? Of course we can; all we have to do is assign the name of that remote machine to the variable strComputer:<\/p>\n<pre class=\"codeSample\">strComputer = \"atl-fs-001\"\n<\/pre>\n<p>After connecting to the WMI service we use this line of code to return a collection of all the files on the computer that have a <b>CreationDate<\/b> greater than or equal to the variable strDate:<\/p>\n<pre class=\"codeSample\">Set colFiles = objWMIService.ExecQuery _\n    (\"Select * From CIM_DataFile Where CreationDate &gt;= '\" &amp; strDate &amp; \"'\")\n<\/pre>\n<p>Keep in mind that, by default, WMI is going to search your entire computer (that is, each and every drive) and track down each and every file with the specified CreationDate. What does that mean? That means that, depending on the size of your disk drives and depending on the number of files found on your computer, this script could (and no doubt will) take several minutes to complete.<\/p>\n<table class=\"dataTable\" id=\"E1G\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p><b>Note<\/b>. Can you speed things up at all? No, not if you need to find all the files on all the drives. If you\u2019re interested in the files found on only a particular drive, however, you could add a Where clause limiting the returned data to that drive. For example, this query searches for new files only on drive D:<\/p>\n<pre class=\"codeSample\">(\"Select * From CIM_DataFile Where CreationDate &gt;= '\" &amp; strDate &amp; \"' AND Drive = 'D:'\")\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>Sooner or later, the script <i>will<\/i> complete. (Again, you might have to be patient here.) When it does, we\u2019ll have a collection of all the files on the computer that have the specified CreationDate. So how do we determine the number, and the total size, of those files? Here\u2019s one way:<\/p>\n<pre class=\"codeSample\">For Each objFile in colFiles\n    intNumberOfFiles = intNumberOfFiles + 1\n    intTotalSize = intTotalSize + objFile.FileSize\nNext\n<\/pre>\n<p>What we\u2019ve done here is set up a For Each loop to loop through each of the files in our collection. For each of those files, we do two things. First, we increment the value of intNumberOfFiles by 1. As you might have guessed, we\u2019ll use this variable to keep track of the number of files found in the collection. And yes, alternatively, we could have simply echoed back the value of the collection\u2019s <b>Count<\/b> property; that will also tell us the number f files that were found. However, we decided to count the files one-by-one simply because we had to use a For Each loop to loop through the collection anyway.<\/p>\n<p><i>Why<\/i> did we need to use a For Each loop to loop through the collection? Because the only way we can determine the total size of all the new files is to add the size of each individual file. That\u2019s what we do in this line of code, where we take the value of the file\u2019s <b>FileSize<\/b> property and add it to the variable intTotalSize:<\/p>\n<pre class=\"codeSample\">intTotalSize = intTotalSize + objFile.FileSize\n<\/pre>\n<p>And then it\u2019s back to the top of the loop, where we repeat this process with the next file in the collection. Once we\u2019ve finished looping through the set of files we echo back the value of the variables intNumberOfFiles and intTotalSize:<\/p>\n<pre class=\"codeSample\">Wscript.Echo \"Total files added: \" &amp; intNumberOfFiles\nWscript.Echo \"Total size of the new files: \" &amp; intTotalSize\n<\/pre>\n<p>That, in turn, should result in output similar to this:<\/p>\n<pre class=\"codeSample\">Total files added: 42\nTotal size of the new files: 69559\n<\/pre>\n<p>Again, this is not the fastest script in the world (not even close), but it <i>is<\/i> thorough. And it will bring back the information you\u2019re looking for.<\/p>\n<p>That takes care of new files that have been added to the computer; now, what about files that have been modified since March 12, 2008? To add that capability, tack this code on to the bottom of the script:<\/p>\n<pre class=\"codeSample\">intModifiedFiles = 0\nintModifiedSize = 0\n\nSet colFiles = objWMIService.ExecQuery _\n    (\"Select * From CIM_DataFile Where LastModified &gt;= '\" &amp; strDate &amp; \"'\")\n\nFor Each objFile in colFiles\n    intModifiedFiles = intModifiedFiles + 1\n    intModifiedSize = intModifiedSize + objFile.FileSize\nNext\n\nWscript.Echo \"Total files modified: \" &amp; (intModifiedFiles \u2013 intNumberOfFiles)\nWscript.Echo \"Total size of the modified files: \" &amp; (intModifiedSize \u2013 intTotalSize)\n<\/pre>\n<p>As you can see, this is quite similar to the code for finding new files. For starters, we assign the value 0 to two new variables, intModifiedFiles and intModifiedSize. We then use this line of code to retrieve a collection of all the files where the value of the <b>LastModified<\/b> property is greater than or equal to strDate:<\/p>\n<pre class=\"codeSample\">Set colFiles = objWMIService.ExecQuery _\n    (\"Select * From CIM_DataFile Where LastModified &gt;= '\" &amp; strDate &amp; \"'\")\n<\/pre>\n<p>After we\u2019ve done that, we set up a For Each loop to calculate the total number of files (intModifiedFiles) and the total size of those files (intModifiedSize). That brings us to these two lines of code:<\/p>\n<pre class=\"codeSample\">Wscript.Echo \"Total files modified: \" &amp; (intModifiedFiles \u2013 intNumberOfFiles)\nWscript.Echo \"Total size of the modified files: \" &amp; (intModifiedSize \u2013 intTotalSize)\n<\/pre>\n<p>Here we\u2019re echoing back the number and size of the all the files that have been modified since March 12, 2008. Notice, however, that we aren\u2019t simply echoing back the value of intModifiedFiles in order to determine the total number of files; instead, we\u2019re echoing back the value of intModifiedFiles minus intNumberOfFiles:<\/p>\n<pre class=\"codeSample\">(intModifiedFiles \u2013 intNumberOfFiles)\n<\/pre>\n<p>Why do we do that? Well, as it turns out, a file that is created on March 12, 2008 doesn\u2019t just get a creation date of March 12, 2008; it also gets a last-modified date of March 12, 2008. That means this one file will get counted twice: it gets counted as a new file <i>and<\/i> as a modified file. In order to guard against this, we subtract the new files from the modified files. For example, suppose we find 10 new files and 25 modified files. Those 25 modified files will include the new files. To get a more accurate count of the modified files, we subtract the 10 new files from the 25 modified files.<\/p>\n<p>From there we do the same thing for the file size. And then that, as they say, is that.<\/p>\n<p>Really.<\/p>\n<p>That should do it, KT; let us know if you have any problems. Oh, and while you\u2019re at it, please let us know if you see Dean anywhere; we\u2019re starting to get a little worried about him. Thank you for your help.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I&#8217;m looking for a script that can search a file server and list the number and total size of files that have been recently modified or added to the server. Any help would be appreciated! &#8212; KT Hey, KT. Before we launch into today\u2019s column we thought we should tell you that [&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-56003","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&#8217;m looking for a script that can search a file server and list the number and total size of files that have been recently modified or added to the server. Any help would be appreciated! &#8212; KT Hey, KT. Before we launch into today\u2019s column we thought we should tell you that [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/56003","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=56003"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/56003\/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=56003"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=56003"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=56003"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}