{"id":70653,"date":"2005-01-14T09:21:00","date_gmt":"2005-01-14T09:21:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/01\/14\/how-can-i-tell-if-a-folder-has-any-files-with-a-specific-file-extension\/"},"modified":"2005-01-14T09:21:00","modified_gmt":"2005-01-14T09:21:00","slug":"how-can-i-tell-if-a-folder-has-any-files-with-a-specific-file-extension","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-tell-if-a-folder-has-any-files-with-a-specific-file-extension\/","title":{"rendered":"How Can I Tell if a Folder Has Any Files with a Specific File Extension?"},"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 whether or not any files with a specific file extension exist in a folder?<BR><BR>&#8212; GM<\/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, GM. This is actually a pretty easy thing to do. All we have to do is write a WMI query that includes the path of the folder we want to check as well as the file extension we\u2019re checking for. For example, this script retrieves a collection of all the .txt files found in the C:\\Scripts folder:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colFiles = objWMIService. _\n    ExecQuery(&#8220;SELECT * FROM CIM_DataFile WHERE Path = &#8216;\\\\Scripts\\\\&#8217; &#8221; &amp; _\n        &#8220;AND Drive = &#8216;C:&#8217; AND Extension = &#8216;txt'&#8221;)<\/p>\n<p>Wscript.Echo &#8220;Number of .txt files found: &#8221; &amp; colFiles.Count\n<\/PRE>\n<P>If you look closely at this script (and you guys do look closely at all this stuff, don\u2019t you?) you might notice a couple things. First, we didn\u2019t specify the path as C:\\Scripts; instead, we separated the drive (<B>C:<\/B>) from the folder (<B>Scripts<\/B>). Why did we do that? Well, because &#8211; for better or worse &#8211; that\u2019s how the CIM_DataFile class works; what we think of as the file path is divvied up between the drive and the folders. The Drive parameter is actually optional here; we could leave it out <I>unless<\/I> we also have a folder named D:\\Scripts. If that was the case, then the Drive parameter becomes mandatory; leave it out, and the query returns all the files found in a folder with the path Scripts. That would include D:\\Scripts (and E:\\Scripts and F:\\Scripts and \u2026) as well as C:\\Scripts.<\/P>\n<P>Second, you might note that we used double <B>\\\\<\/B>\u2019s to surround the Path; thus we have <B>\\\\Scripts\\\\<\/B> rather than \\Scripts\\ (i.e., C:\\Scripts). Why? Again, that\u2019s just the way WMI works; any time you include a file path in a WHERE clause you need to use two \\\\\u2019s. What if we wanted to check the folder C:\\Documents and Settings\\Ken Myer\\Desktop\\Work Files to see if <I>it<\/I> had any .txt files in it? In that case, our WHERE clause would look like this:<\/P><PRE class=\"codeSample\">WHERE Path = &#8216;\\\\Documents and Settings\\\\Ken Myer\\\\Desktop\\\\Work Files\\\\\n<\/PRE>\n<P>Note, too the two \\\\\u2019s at the end of the Path. Those are important; don\u2019t leave them out. (Well, unless you don\u2019t care whether or not the script actually works.) And as long as we\u2019re busy noting things, note that we don\u2019t include the period when specifying the file extension; it\u2019s just <B>txt<\/B>, not <B>.txt<\/B>.<\/P>\n<P>After we get our collection back, all we have to do is echo the value of the collection\u2019s <B>Count<\/B> property. This will tell us how many .txt files were found. If the Count is 0, we didn\u2019t find any; if the Count is 7, we found 7. If &#8211; well, you can figure out the rest yourself. <\/P>\n<P>See, we told you this was easy. Hey, every once in a while, we actually <I>do<\/I> know what we\u2019re talking about!<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I verify whether or not any files with a specific file extension exist in a folder?&#8212; GM Hey, GM. This is actually a pretty easy thing to do. All we have to do is write a WMI query that includes the path of the folder we want to check as [&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,5],"class_list":["post-70653","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-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I verify whether or not any files with a specific file extension exist in a folder?&#8212; GM Hey, GM. This is actually a pretty easy thing to do. All we have to do is write a WMI query that includes the path of the folder we want to check as [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70653","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=70653"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70653\/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=70653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}