{"id":56053,"date":"2008-03-06T23:59:00","date_gmt":"2008-03-06T23:59:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2008\/03\/06\/hey-scripting-guy-how-can-i-write-a-message-to-all-the-empty-files-in-a-folder\/"},"modified":"2008-03-06T23:59:00","modified_gmt":"2008-03-06T23:59:00","slug":"hey-scripting-guy-how-can-i-write-a-message-to-all-the-empty-files-in-a-folder","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-write-a-message-to-all-the-empty-files-in-a-folder\/","title":{"rendered":"Hey, Scripting Guy! How Can I Write a Message to All the Empty Files in a Folder?"},"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 need to search a certain directory for files with a size of 0 KB, then replace the non-existent text in those files with an error message like this one: &#8220;File did not save correctly.&#8221; I have been searching for the answer myself and can&#8217;t seem to grasp it. Can you help me on this one?<BR><BR>&#8212; CE<\/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, CE. Have you ever heard the expression \u201cOne man\u2019s meat is another man\u2019s poison?\u201d Well, the idea behind that saying is that what might be good for one person (meat) might not be good for another person (poison).<\/P>\n<TABLE class=\"dataTable\" id=\"E2C\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P><B>Note<\/B>. Unless you\u2019re a vegetarian, in which case meat isn\u2019t good for you, either. We\u2019re assuming that vegetarians won\u2019t eat poison, although, come to think of it, many vegetarians <I>will<\/I> eat okra. As near as we can tell, there isn\u2019t a whole heck of a lot of difference between poison and okra.<\/P>\n<P>Other than the fact that the poison probably tastes better. (To be honest, it couldn\u2019t taste much <I>worse<\/I>.)<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>So what does all this have to do with scripting? Well, to CE this was a bad question; he couldn\u2019t find an answer to it anywhere. To the Scripting Guy who writes this column, however, this was a <I>good<\/I> question; still a bit bedraggled after the close of the <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/funzone\/games\/games08\/news.mspx\"><B>2008 Winter Scripting Games<\/B><\/A>, he was hoping for a question he could answer with a minimal amount of effort. As the old saying goes, one man\u2019s meat is another man\u2019s way of doing his job without having to actually do much of anything.<\/P>\n<P>So how much effort <I>is<\/I> required to carry out this task? Like we said, not much at all:<\/P><PRE class=\"codeSample\">Const ForWriting = 2<\/p>\n<p>Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nSet objFolder = objFSO.GetFolder(&#8220;C:\\Logs&#8221;)<\/p>\n<p>Set colItems = objFolder.Files<\/p>\n<p>For Each objItem in colItems\n    If objItem.Size = 0 Then\n        Set objFile = objFSO.OpenTextFile(objItem.Path, ForWriting)\n        objFile.WriteLine &#8220;File did not save correctly.&#8221;\n        objFile.Close\n    End If\nNext\n<\/PRE>\n<P>See? We told you that there isn\u2019t much meat here. Or, if you happen to be another man, there isn\u2019t much poison here.<\/P>\n<TABLE class=\"dataTable\" id=\"EAE\" 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>. You see, because one man\u2019s meat is another man\u2019s \u2013 well, never mind.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>We start out by defining a constant named ForWriting and setting the value to 2; we\u2019ll need this constant when we write the message <I>File did not save correctly<\/I> to each empty file. After defining the constant we create an instance of the <B>Scripting.FileSystemObject<\/B> object, then use the <B>GetFolder<\/B> method to bind to the folder C:\\Logs:<\/P><PRE class=\"codeSample\">Set objFolder = objFSO.GetFolder(&#8220;C:\\Logs&#8221;)\n<\/PRE>\n<P>As soon as we make the connection we can retrieve a collection of all the files found in C:\\Logs. We do that by creating an object reference (colItems) to the folder\u2019s <B>Files<\/B> property:<\/P><PRE class=\"codeSample\">Set colItems = objFolder.Files\n<\/PRE>\n<P>We now have a collection of all the files found in C:\\Logs. That\u2019s great. The only question is: what exactly are we going to <I>do<\/I> with this collection?<\/P>\n<P>Well, to begin with, we\u2019re going to set up a For Each loop to walk us through each and every file in the collection. Inside that loop, the first thing we\u2019re going to do is check to see if the file <B>Size<\/B> is equal to 0 bytes:<\/P><PRE class=\"codeSample\">If objItem.Size = 0 Then\n<\/PRE>\n<P>If this line of code returns True that means that we have an empty file on our hands. With that in mind, we again turn to the FileSystemObject, this time using the object to open the file for writing:<\/P><PRE class=\"codeSample\">Set objFile = objFSO.OpenTextFile(objItem.Path, ForWriting)\n<\/PRE>\n<TABLE class=\"dataTable\" id=\"EKF\" 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>. How did the FileSystemObject know which file we wanted to open? Well, the FileSystemObject pretty much knows <I>everything<\/I>. But, just in case, we passed it the value of the file\u2019s <B>Path<\/B> property. As the name implies, the Path property is going to be a value similar to this: C:\\Logs\\Test.txt. Given that information, how could the FileSystemObject <I>not<\/I> know which file to open?<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>After we open the file we then use the <B>WriteLine<\/B> method to replace the empty file with the message <I>File did not save correctly<\/I>:<\/P><PRE class=\"codeSample\">objFile.WriteLine &#8220;File did not save correctly.&#8221;\n<\/PRE>\n<P>Once we\u2019ve done that we close the file, then loop back to the top of the loop and repeat the process with the next item in the collection.<\/P>\n<P>All in all, pretty darn easy.<\/P>\n<P>Of course, there <I>is<\/I> one drawback to the script: it will work on only the local computer. What if you need to perform this task against a remote machine? <\/P>\n<P>To tell you the truth, that\u2019s a bit of a problem; that\u2019s because the FileSystemObject isn\u2019t designed to work against remote computers. Nevertheless, there is a workaround: we can use WMI to retrieve a collection of all the files in a remote folder, then use UNC paths and administrative shares to coerce the FileSystemObject into writing our message to any empty files in that folder. Is this a hack of some kind? Sort of. But it works, <I>provided<\/I> that you use administrative shares:<\/P><PRE class=\"codeSample\">Const ForWriting = 2<\/p>\n<p>Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)<\/p>\n<p>strComputer = &#8220;gizmonic&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colItems = objWMIService.ExecQuery _\n    (&#8220;ASSOCIATORS OF {Win32_Directory.Name=&#8217;C:\\Temp&#8217;} Where &#8221; _\n        &amp; &#8220;ResultClass = CIM_DataFile&#8221;)<\/p>\n<p>For Each objItem In colItems\n    If objItem.FileSize = 0 Then       \n        strFilePath = &#8220;\\\\&#8221; &amp; strComputer &amp; &#8220;\\C$\\Temp\\&#8221; &amp; _\n            objItem.FileName &amp; &#8220;.&#8221; &amp; objItem.Extension\n        Set objFile = objFSO.OpenTextFile(strFilePath, ForWriting)\n        objFile.WriteLine &#8220;File did not save correctly.&#8221;\n        objFile.Close\n    End If\nNext\n<\/PRE>\n<P>We aren\u2019t going to explain this script in any detail today. For more information about working with text files on remote computers, see our column on, well, <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/oct05\/hey1021.mspx\"><B>working with text files on remote computers<\/B><\/A>. That will also explain what we mean by administrative shares. <\/P>\n<P>That should do it, CE. And that should just about do it for the 2008 Winter Scripting Games as well. Remember, if you see a problem with your score or if you want to resubmit an entry please do it now. (Yes, we know: technically, the deadline for inquiries\/resubmissions has passed. But, hey, when\u2019s the last time the Scripting Guys cared about technicalities?) Let us know soon. On Monday. March 10<SUP>th<\/SUP> we\u2019re going to start drawing for prizes, and we\u2019ll start notifying both prize winners and\/or people who qualified for a Certificate of Excellence. We don\u2019t want anyone to be left out.<\/P>\n<P>And no, not even all those people who wrote in to complain about <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/funzone\/games\/games08\/news.mspx\"><B>Event 5<\/B><\/A> in the Beginners Division. One man\u2019s solution to a Scripting Games event is another man\u2019s okra. <\/P>\n<P>Or something like that. As the Scripting Editor will attest, the Scripting Guy who writes this column doesn\u2019t always pay much attention when other people say things.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I need to search a certain directory for files with a size of 0 KB, then replace the non-existent text in those files with an error message like this one: &#8220;File did not save correctly.&#8221; I have been searching for the answer myself and can&#8217;t seem to grasp it. Can you help [&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,11,3,4,12,14,5],"class_list":["post-56053","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-files","tag-folders","tag-scripting-guy","tag-scripting-techniques","tag-storage","tag-text-files","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I need to search a certain directory for files with a size of 0 KB, then replace the non-existent text in those files with an error message like this one: &#8220;File did not save correctly.&#8221; I have been searching for the answer myself and can&#8217;t seem to grasp it. Can you help [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/56053","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=56053"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/56053\/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=56053"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=56053"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=56053"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}