{"id":68883,"date":"2005-09-26T16:41:00","date_gmt":"2005-09-26T16:41:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/09\/26\/how-can-i-delete-specific-files-in-a-specific-folder\/"},"modified":"2005-09-26T16:41:00","modified_gmt":"2005-09-26T16:41:00","slug":"how-can-i-delete-specific-files-in-a-specific-folder","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-delete-specific-files-in-a-specific-folder\/","title":{"rendered":"How Can I Delete Specific Files in a Specific Folder?"},"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 access the files in the folder T:\\Act and delete any files that have the string \u201ccurrent\u201d somewhere in the file name?<BR><BR>&#8212; SC<\/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, SC. We have to admit, the notion of deleting anything current has a certain appeal, doesn\u2019t it? Well, not for the Scripting Guys, of course; after all, our lives are so perfect there isn\u2019t anything we\u2019d want to get rid of. OK, maybe car payments. And mortgages. And that dog two houses down who still barks like crazy any time he sees us. (It\u2019s been <I>10 years<\/I>, dog; surely you must be used to us by now.) <\/P>\n<P>But that\u2019s about it. Maybe &#8211; no, <I>definitely<\/I> &#8211; the old guy around the corner who\u2019s always outside without his shirt on. Oh, and &#8211; well, maybe we better focus on deleting all the files that have the string <I>current<\/I> somewhere in the file name:<\/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;T:\\Act&#8217;} Where &#8221; _\n        &amp; &#8220;ResultClass = CIM_DataFile&#8221;)<\/p>\n<p>For Each objFile In colFileList\n    If InStr(objFile.FileName, &#8220;current&#8221;) Then\n        objFile.Delete\n    End If\nNext\n<\/PRE>\n<P>This script starts out &#8211; as many of our WMI scripts do &#8211; by binding to the WMI service on the local computer. (Of course, like most WMI scripts, this one can also be run against a remote computer.) We then use this query to return a collection of all the files in the folder T:\\Act:<\/P><PRE class=\"codeSample\">Set colFileList = objWMIService.ExecQuery _\n    (&#8220;ASSOCIATORS OF {Win32_Directory.Name=&#8217;T:\\Act&#8217;} Where &#8221; _\n        &amp; &#8220;ResultClass = CIM_DataFile&#8221;)\n<\/PRE>\n<P>Next we set up a For Each loop in order to walk through the collection of files. Inside that loop we use the <B>InStr<\/B> function to determine whether or not the string <I>current<\/I> can be found in the file name:<\/P><PRE class=\"codeSample\">If InStr(objFile.FileName, &#8220;current&#8221;) Then\n<\/PRE>\n<P>If InStr is True that means that the string can, indeed, be found within the file name. In that case, we use a single line of code to delete the file:<\/P><PRE class=\"codeSample\">objFile.Delete\n<\/PRE>\n<P>Yep: it\u2019s that easy. We then loop around and repeat the process with the next file in the collection. By the time we\u2019re done any file in T:\\Act that had the string <I>current<\/I> in its file name will have gone to that great hard disk in the sky.<\/P>\n<P>Or, to put it in terms that actually make sense, those files will have been deleted.<\/P>\n<P>Now if we could just do the same thing with that dumb orange car &#8211; it doesn\u2019t even have a hood! &#8211; that the neighbors have parked outside the house. And those blackberry bushes that keep trying to take over the yard. And \u2026.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I access the files in the folder T:\\Act and delete any files that have the string \u201ccurrent\u201d somewhere in the file name?&#8212; SC Hey, SC. We have to admit, the notion of deleting anything current has a certain appeal, doesn\u2019t it? Well, not for the Scripting Guys, of course; after [&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-68883","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 access the files in the folder T:\\Act and delete any files that have the string \u201ccurrent\u201d somewhere in the file name?&#8212; SC Hey, SC. We have to admit, the notion of deleting anything current has a certain appeal, doesn\u2019t it? Well, not for the Scripting Guys, of course; after [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68883","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=68883"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68883\/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=68883"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68883"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68883"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}