{"id":66993,"date":"2006-06-30T19:46:00","date_gmt":"2006-06-30T19:46:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/06\/30\/how-can-i-locate-all-the-instances-of-a-particular-file-on-a-computer\/"},"modified":"2006-06-30T19:46:00","modified_gmt":"2006-06-30T19:46:00","slug":"how-can-i-locate-all-the-instances-of-a-particular-file-on-a-computer","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-locate-all-the-instances-of-a-particular-file-on-a-computer\/","title":{"rendered":"How Can I Locate All the Instances of a Particular File on a Computer?"},"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 locate all the instances of a particular file (like budget.xls) on a computer?<BR><BR>&#8212; MN<\/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=\"TechNet Script Center\" border=\"0\" alt=\"TechNet 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, MN. Did you know that former heavyweight boxing champion George Foreman has 10 children, 5 sons and 5 daughters? It\u2019s true. But that\u2019s not the best part; the best part is that all 5 of his sons are named George Edward Foreman. (And, just for good measure, one of his daughters is named Georgette.) Apparently that\u2019s the sort of thing that can happen when you get hit in the head over and over again.<\/P>\n<P>Of course, at least George (the original George) can use boxing as an excuse. By contrast, the Scripting Guy who writes this column also tends to use the same name over and over again, and he\u2019s <I>never<\/I> been hit in the head. (Though many people have been tempted to do so.) For example, when he tackled this question he used <I>Test.vbs<\/I> as the file name to look for. The end result: he found 36 instances of Test.vbs, including:<\/P><PRE class=\"codeSample\">c:\\scripts\\test.vbs\nd:\\itunes\\test.vbs\nd:\\keep these\\101 things\\test.vbs\nd:\\keep these\\2003 stuff\\test.vbs\nd:\\keep these\\adsi security\\backup\\test.vbs\nd:\\keep these\\adsi security\\test.vbs\nd:\\keep these\\agent\\test.vbs\nd:\\keep these\\database webcast\\test.vbs\nd:\\keep these\\hta helpomatic\\test.vbs\nd:\\keep these\\miscellaneous\\scripting guide\\wmi presentation\\test.vbs\nd:\\keep these\\miscellaneous\\test.vbs\n<\/PRE>\n<P>Etc., etc., etc.<\/P>\n<TABLE id=\"ECD\" 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>. Yes, it <I>is<\/I> a good thing that the Scripting Son is an only child.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>In other words, at least some of us are very familiar with the concept (problem?) of having multiple files with the same name, all of these files scattered every-which-way on a computer. We won\u2019t spend any time debating whether that\u2019s a good thing or a bad thing (primarily because that\u2019s a debate we can\u2019t imagine winning). Instead, we\u2019ll spend our time talking about the following 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 colFiles = objWMIService.ExecQuery _\n    (&#8220;Select * From CIM_DataFile Where FileName = &#8216;test&#8217; and Extension = &#8216;vbs'&#8221;)<\/p>\n<p>For Each objFile in colFiles\n    Wscript.Echo objFile.Name\nNext\n<\/PRE>\n<P>We hope you weren\u2019t expecting a really complicated script here. For one thing, we don\u2019t <I>need<\/I> a very complicated script; this is actually a pretty easy problem to solve. For another, remember that this Scripting Guy has given 36 different files the name Test.vbs. As you might expect, \u201ccomplicated\u201d isn\u2019t a word he\u2019s too terribly familiar with.<\/P>\n<P>The script starts out by connecting to the WMI service on the local computer. Could we perform this same search on a remote computer? You bet we could; just assign the name of that computer to the variable strComputer:<\/P><PRE class=\"codeSample\">strComputer = &#8220;atl-fs-01&#8221;\n<\/PRE>\n<P>After connecting to the WMI service we then issue the following query:<\/P><PRE class=\"codeSample\">Set colFiles = objWMIService.ExecQuery _\n    (&#8220;Select * From CIM_DataFile Where FileName = &#8216;test&#8217; and Extension = &#8216;vbs'&#8221;)\n<\/PRE>\n<P>As you can see, we\u2019re querying the <B>CIM_DataFile<\/B> class, which just happens to be the WMI class responsible for managing all the files on a computer. Of course, we aren\u2019t interested in <I>all<\/I> the files on the computer, just those named Test.vbs. That\u2019s why we tack on a Where clause that includes two stipulations:<\/P>\n<TABLE border=\"0\" cellSpacing=\"0\" cellPadding=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>All the files returned by the query must have a <B>FileName<\/B> equal to <I>test<\/I>; the FileName property is simply the name of the file <I>minus<\/I> the file extension. That\u2019s an important consideration: many people who work with the CIM_DataFile class assume that the FileName is the \u201ccomplete\u201d file name (e.g., Test.vbs). It\u2019s not. And don\u2019t use the <B>Name<\/B> property in your query, either. We\u2019ll explain what the Name property is for in just a minute.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>All the files returned by the query must have an <B>Extension<\/B> equal to <I>vbs<\/I>. Again, be careful here: without thinking most people include the period when specifying the file extension (e.g., <I>.vbs<\/I>). Don\u2019t do that.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<TABLE id=\"E6E\" 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>. OK, don\u2019t do that unless it doesn\u2019t matter to you whether or not your query returns the expected information. If you\u2019re running the script just for the pure joy of running a script, well, feel free to specify any file extension you want. But if you\u2019d really like the script the find all instances of Test.vbs, leave out the period.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>You\u2019re right: CIM_DataFile <I>can<\/I> be a little tricky to work with, can\u2019t it? About all we can say is that you should always wear gloves and safety goggles before using CIM_DataFile in a script. Oh, and check out the <A href=\"http:\/\/null\/technet\/scriptcenter\/guide\/sas_fil_scfm.mspx\" target=\"_blank\"><B>Microsoft Windows 2000 Scripting Guide<\/B><\/A> if you need information about what the various properties <I>really<\/I> mean. <\/P>\n<P>After our query finishes executing (a process that could take a minute or two, depending on the size of your disk drives and on the number of files stored on those drives) we set up a For Each loop to walk through the items in the collection, echoing back the Name of each file:<\/P><PRE class=\"codeSample\">For Each objFile in colFiles\n    Wscript.Echo objFile.Name\nNext\n<\/PRE>\n<P>And, yes, it probably <I>would<\/I> have been better if this property was called Path rather than Name; that\u2019s because the Name really <I>is<\/I> the full path to the file (e.g., c:\\scripts\\test.vbs). But with CIM_DataFile, you need to expect the unexpected.<\/P>\n<P>There you go, MN; that should help you track down all the files named Test.vbs. And if you\u2019re looking for all the people in the world named George, we\u2019d recommend starting at George Foreman\u2019s house. You ought to be able to get at least half of them right there.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I locate all the instances of a particular file (like budget.xls) on a computer?&#8212; MN Hey, MN. Did you know that former heavyweight boxing champion George Foreman has 10 children, 5 sons and 5 daughters? It\u2019s true. But that\u2019s not the best part; the best part is that all 5 [&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-66993","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 locate all the instances of a particular file (like budget.xls) on a computer?&#8212; MN Hey, MN. Did you know that former heavyweight boxing champion George Foreman has 10 children, 5 sons and 5 daughters? It\u2019s true. But that\u2019s not the best part; the best part is that all 5 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66993","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=66993"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66993\/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=66993"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66993"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66993"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}