{"id":68033,"date":"2006-02-03T14:36:00","date_gmt":"2006-02-03T14:36:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/02\/03\/how-can-i-search-for-multiple-file-extensions-in-a-single-wql-query\/"},"modified":"2006-02-03T14:36:00","modified_gmt":"2006-02-03T14:36:00","slug":"how-can-i-search-for-multiple-file-extensions-in-a-single-wql-query","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-search-for-multiple-file-extensions-in-a-single-wql-query\/","title":{"rendered":"How Can I Search for Multiple File Extensions in a Single WQL Query?"},"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 search for multiple file extensions in a single WQL query?<BR><BR>&#8212; AH<\/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, AH. If we understand your question correctly, you want to know if it\u2019s true that the Seattle Seahawks are playing in the Super Bowl this Sunday. Yes, it <I>is<\/I> true: our very own Seahawks will be taking on \u2026 well, some second-place team. This is actually a great time for sports fans to be living in Seattle: the Seahawks are in the Super Bowl; the University of Washington men\u2019s basketball team is ranked 15<SUP>th<\/SUP> in the country; the UW\u2019s women\u2019s volleyball team won the national championship; the Seattle Sonics &#8211;<\/P>\n<P>Um, did we mention that the women\u2019s volleyball team won the national championship?<\/P>\n<P>Actually, upon further review we suspect that maybe you\u2019d like to know how to search for multiple file extensions in a single WQL query. Well, then why didn\u2019t you say so:<\/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.ExecQuery _\n    (&#8220;Select * from CIM_Datafile Where Extension = &#8216;mp3&#8217; OR Extension = &#8216;wma'&#8221;)<\/p>\n<p>For Each objFile in colFiles\n    Wscript.Echo &#8220;Name: &#8221; &amp; objFile.Name\nNext\n<\/PRE>\n<P>As you can see, this is about as simple a WMI script as you can get. We start out by connecting to the WMI service on the local computer. We then issue a query that looks for files of two types: those with the file extension <B>mp3<\/B> and those with the file extension <B>wma<\/B>. After retrieving the files we then set up a For Each loop to walk through the collection and echo back the name of each file.<\/P>\n<P>There are two important things to note here. First, the name of the WMI property is <B>Extension<\/B> (and no, we don\u2019t know why they didn\u2019t call it FileExtension). Second, keep in mind that we are searching for files with the extension <B>mp3<\/B>; we are <I>not<\/I> searching for files with the extension <B>.mp3<\/B>. Make sure you leave the dot out; otherwise your script won\u2019t return any data.<\/P>\n<TABLE id=\"EYD\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. One other thing: running queries against the entire file system can take a long time, something that depends on the number of files on your machine. That doesn\u2019t mean you can\u2019t or shouldn\u2019t do it; we just wanted to remind everyone that a script like this could easily take several minutes (or more) to complete. <I>(Editor\u2019s Note: <\/I>Now<I> he tells us.)<\/I><\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>So what\u2019s the secret to searching for multiple file extensions? Well, think about your task: you want a list of files that have the file extension mp3 <I>or<\/I> that have the file extension wma. When you write the task out like this the key word is the word <I>or<\/I>; that\u2019s also true when you write out the WQL query. Let\u2019s take a look at the query again:<\/P><PRE class=\"codeSample\">Set colFiles = objWMIService.ExecQuery _\n    (&#8220;Select * from CIM_Datafile Where Extension = &#8216;mp3&#8217; OR Extension = &#8216;wma'&#8221;)\n<\/PRE>\n<P>There you have it: <B>Where Extension = &#8216;mp3&#8217; <\/B><B><I>OR<\/I><\/B><B> Extension = &#8216;wma&#8217;<\/B>. The key is remembering that you want file extension mp3 or you want file extension wma. Often-times people make the mistake of thinking, \u201cI want all the files that have the file extension mp3 and I want all the files that have the file extension wma.\u201d As a result, they end up writing a query like this:<\/P><PRE class=\"codeSample\">Set colFiles = objWMIService.ExecQuery _\n    (&#8220;Select * from CIM_Datafile Where Extension = &#8216;mp3&#8217; AND Extension = &#8216;wma'&#8221;)\n<\/PRE>\n<P>We don\u2019t have any idea what files you have stored on your computer, but we already know how many files this query will find: 0. Why? Because what we\u2019re saying here is this: find all the files that have the file extension mp3 <I>and that also have<\/I> the file extension wma. Needless to say, there aren\u2019t going to be many files with two file extensions. Don\u2019t get AND and OR mixed-up. Use OR when you want to expand the search, AND when you want to contract the search.<\/P>\n<P>Note, too that you need to specify the property each time: <B>Extension = &#8216;mp3&#8217; OR Extension = &#8216;wma&#8217;<\/B>. If you try to take shortcuts your script won\u2019t work: <B>Extension = &#8216;mp3&#8217; OR &#8216;wma&#8217;<\/B>.<\/P>\n<P>Finally, we should point out that you can use the OR operator with things other than file extensions. For example, here\u2019s a query that returns a list of all the services that are either stopped or paused:<\/P><PRE class=\"codeSample\">Set colFiles = objWMIService.ExecQuery _\n    (&#8220;Select * from Win32_Service Where State = &#8216;Stopped&#8217; OR State = &#8216;Paused'&#8221;)\n<\/PRE>\n<P>Likewise you can include more than just two OR clauses. Also interested in files with the extension wav? Then just add it to the query:<\/P><PRE class=\"codeSample\">Set colFiles = objWMIService.ExecQuery _\n    (&#8220;Select * from CIM_Datafile Where Extension = &#8216;mp3&#8217; OR Extension = &#8216;wma&#8217; OR Extension = &#8216;wav'&#8221;)\n<\/PRE>\n<P>Oh, and one other thing: Go Seahawks!<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I search for multiple file extensions in a single WQL query?&#8212; AH Hey, AH. If we understand your question correctly, you want to know if it\u2019s true that the Seattle Seahawks are playing in the Super Bowl this Sunday. Yes, it is true: our very own Seahawks will be taking [&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-68033","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! How can I search for multiple file extensions in a single WQL query?&#8212; AH Hey, AH. If we understand your question correctly, you want to know if it\u2019s true that the Seattle Seahawks are playing in the Super Bowl this Sunday. Yes, it is true: our very own Seahawks will be taking [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68033","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=68033"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68033\/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=68033"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68033"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68033"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}