{"id":71223,"date":"2004-10-14T07:25:00","date_gmt":"2004-10-14T07:25:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2004\/10\/14\/how-can-i-determine-if-my-users-have-certain-files-on-their-computers\/"},"modified":"2004-10-14T07:25:00","modified_gmt":"2004-10-14T07:25:00","slug":"how-can-i-determine-if-my-users-have-certain-files-on-their-computers","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-determine-if-my-users-have-certain-files-on-their-computers\/","title":{"rendered":"How Can I Determine if My Users Have Certain Files on Their Computers?"},"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! Is it possible to search a computer for .MP3 files or other files my users aren\u2019t supposed to have?<BR><BR>&#8212; AK<\/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, AK. Once again it\u2019s WMI to the rescue. Using the CIM_DataFile class it\u2019s easy to search a computer for specific file types. Want to know if your users have any .MP3 files lying around? Then just use a script similar to this:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject(&#8220;winmgmts:&#8221; _\n    &amp; &#8220;{impersonationLevel=impersonate}!\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\nSet colFiles = objWMIService.ExecQuery _\n    (&#8220;Select * from CIM_DataFile where Extension = &#8216;mp3&#8242;&#8221;)\nFor Each objFile in colFiles\n    Wscript.Echo objFile.Name \nNext\n<\/PRE>\n<P>As you can see, this is a pretty standard WMI script: we\u2019re just looking for all instances of the CIM_DataFile class that have an Extension of MP3 (note that you don\u2019t include the period; it\u2019s <B>MP3<\/B> and not <B>.MP3<\/B>). A very simple little script, and it runs reasonably fast: on a 2.39 GHz laptop, with 512 MB of RAM and 30 gigabytes of hard disk space, we got back a list of all the .MP3 files in less than 30 seconds.<\/P>\n<P>It\u2019s even possible to search for more than one file type with a single query. For example, this script searches for .WMA files as well as .MP3 files:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject(&#8220;winmgmts:&#8221; _\n    &amp; &#8220;{impersonationLevel=impersonate}!\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\nSet colFiles = objWMIService.ExecQuery _\n    (&#8220;Select * from CIM_DataFile where Extension = &#8216;mp3&#8217; OR Extension = &#8216;wma'&#8221;)\nFor Each objFile in colFiles\n    Wscript.Echo objFile.Name \nNext\n<\/PRE>\n<P>But that\u2019s not the cool thing. You said these are files your users are not allowed to have; we\u2019re guessing that means you\u2019d like them removed from their computers. The preceding script won\u2019t do that; it just reports back the names of all the .MP3 and .WMA found on a computer. It\u2019s still up to you to contact each user and ask him or her to delete the offending files. That\u2019s a lot of work on your part, and you then have to rely on the users deleting the files (without accidentally deleting something else in the process). So why not let the script save you all that trouble? This version not only tracks down all the .MP3 and .WMA files on a computer, it also deletes the files as they are found:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject(&#8220;winmgmts:&#8221; _\n    &amp; &#8220;{impersonationLevel=impersonate}!\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\nSet colFiles = objWMIService.ExecQuery _\n    (&#8220;Select * from CIM_DataFile where Extension = &#8216;mp3&#8217; OR Extension = &#8216;wma'&#8221;)\nFor Each objFile in colFiles\n   objFile.Delete \nNext\n<\/PRE>\n<P>And remember, this works on remote computers just as quickly and just as easily as it does on the local computer. Just change the value of the variable strComputer to the name of the remote computer and have at it.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! Is it possible to search a computer for .MP3 files or other files my users aren\u2019t supposed to have?&#8212; AK Hey, AK. Once again it\u2019s WMI to the rescue. Using the CIM_DataFile class it\u2019s easy to search a computer for specific file types. Want to know if your users have any .MP3 [&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,4,12,5,6],"class_list":["post-71223","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-files","tag-scripting-guy","tag-scripting-techniques","tag-storage","tag-vbscript","tag-wmi"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! Is it possible to search a computer for .MP3 files or other files my users aren\u2019t supposed to have?&#8212; AK Hey, AK. Once again it\u2019s WMI to the rescue. Using the CIM_DataFile class it\u2019s easy to search a computer for specific file types. Want to know if your users have any .MP3 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71223","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=71223"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71223\/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=71223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=71223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=71223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}