{"id":68703,"date":"2005-10-20T14:33:00","date_gmt":"2005-10-20T14:33:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/10\/20\/how-can-i-verify-that-a-folder-has-files-in-it-and-if-so-delete-those-files\/"},"modified":"2005-10-20T14:33:00","modified_gmt":"2005-10-20T14:33:00","slug":"how-can-i-verify-that-a-folder-has-files-in-it-and-if-so-delete-those-files","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-verify-that-a-folder-has-files-in-it-and-if-so-delete-those-files\/","title":{"rendered":"How Can I Verify That a Folder Has Files in It and, If So, Delete Those Files?"},"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 verify that a certain folder (C:\\TestLog) has files in it and, if so, delete all those files?<BR><BR>&#8212; DC<\/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, DC. Before we begin we should point out that &#8211; when using WMI &#8211; it doesn\u2019t matter whether or not a folder has any files in it: you can still tell WMI to delete all the files in that folder. If no such files exist that won\u2019t phase WMI in the least. Why are we telling you this? Just to point out that, if your only concern is deleting files, then you don\u2019t have to worry about verifying that the folder actually <I>has<\/I> files. Instead, just delete away.<\/P>\n<P>Of course, you might have other reasons for wanting to know whether or not a folder has any files in it. With that in mind, we came up with this script:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colFileList = objWMIService.ExecQuery _\n    (&#8220;ASSOCIATORS OF {Win32_Directory.Name=&#8217;C:\\TestLog&#8217;} Where &#8221; _\n        &amp; &#8220;ResultClass = CIM_DataFile&#8221;)<\/p>\n<p>If colFileList.Count = 0 Then\n    Wscript.Echo &#8220;There were no files in the folder.&#8221;\nElse\n    Wscript.Echo &#8220;There were &#8221; &amp; colFileList.Count &amp; &#8221; files in the folder.&#8221;<\/p>\n<p>    For Each objFile In colFileList\n        objFile.Delete\n    Next\nEnd If\n<\/PRE>\n<P>The script begins by connecting to the WMI service on the local computer. If you\u2019re wondering why we chose to use WMI to carry out this task (as opposed to, say, the FileSystemObject) here\u2019s one reason: although this script is designed to work against the local computer we can easily modify it to work against <I>any<\/I> computer where we have local Administrator rights. All we have to do is change the value of the variable strComputer from a dot (WMI-speak for the local computer) to the name of the remote machine. You can\u2019t do that with the FileSystemObject, at least not as easily.<\/P>\n<P>After connecting to the WMI service we then use this line of code to retrieve a collection of all the files in the folder C:\\TestLog:<\/P><PRE class=\"codeSample\">Set colFileList = objWMIService.ExecQuery _\n    (&#8220;ASSOCIATORS OF {Win32_Directory.Name=&#8217;C:\\TestLog&#8217;} Where &#8221; _\n        &amp; &#8220;ResultClass = CIM_DataFile&#8221;)\n<\/PRE>\n<P>And, yes, <B>Associators Of<\/B> queries are kind of funny-looking, but don\u2019t let that bother you. All you really need to know is this: working with any other folder (for example, D:\\Logfiles) is as easy as replacing the path C:\\TestLog with the path D:\\Logfiles:<\/P><PRE class=\"codeSample\">Set colFileList = objWMIService.ExecQuery _\n    (&#8220;ASSOCIATORS OF {Win32_Directory.Name=&#8217;D:\\Logfiles&#8217;} Where &#8221; _\n        &amp; &#8220;ResultClass = CIM_DataFile&#8221;)\n<\/PRE>\n<P>Of course, that\u2019s all well and good, but how do we know whether or not C:\\TestLog has any files in it? Well, our query returns a collection consisting, in this case, of all the files (instances of the <B>CIM_DataFile<\/B> class) found in the folder C:\\TestLog. As it turns out, WMI collections have a property named <B>Count<\/B> which returns the number of items in the collection. We can determine whether or not the folder has any files in it just by checking to see if the Count is equal to 0:<\/P><PRE class=\"codeSample\">If colFileList.Count = 0 Then\n<\/PRE>\n<P>If the Count <I>is<\/I> equal to 0 that means there were no files in the folder; in that case, we echo a message saying that no files were in the folder. If the Count is <I>not<\/I> equal to 0 then we do two things. First, we use this line of code to report back the number of files that <I>were<\/I> in the folder:<\/P><PRE class=\"codeSample\">Wscript.Echo &#8220;There were &#8221; &amp; colFileList.Count &amp; &#8221; files in the folder.&#8221;\n<\/PRE>\n<P>Second, we set up a For Each loop to walk through the collection of files. For each file in the collection we call the <B>Delete<\/B> method and delete that file:<\/P><PRE class=\"codeSample\">For Each objFile In colFileList\n    objFile.Delete\nNext\n<\/PRE>\n<P>When we\u2019ve finished with the For Each loop all the files will be gone, and everyone will live happily ever after. (Except, of course, for those poor files we just wiped out. Hopefully they\u2019ve all gone to a better place.)<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I verify that a certain folder (C:\\TestLog) has files in it and, if so, delete all those files?&#8212; DC Hey, DC. Before we begin we should point out that &#8211; when using WMI &#8211; it doesn\u2019t matter whether or not a folder has any files in it: you can still [&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":[715,38,11,3,12,5],"class_list":["post-68703","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-associators-of","tag-files","tag-folders","tag-scripting-guy","tag-storage","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I verify that a certain folder (C:\\TestLog) has files in it and, if so, delete all those files?&#8212; DC Hey, DC. Before we begin we should point out that &#8211; when using WMI &#8211; it doesn\u2019t matter whether or not a folder has any files in it: you can still [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68703","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=68703"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68703\/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=68703"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68703"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68703"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}