{"id":70453,"date":"2005-02-11T21:04:00","date_gmt":"2005-02-11T21:04:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/02\/11\/how-can-i-use-a-wildcard-character-to-delete-folders\/"},"modified":"2005-02-11T21:04:00","modified_gmt":"2005-02-11T21:04:00","slug":"how-can-i-use-a-wildcard-character-to-delete-folders","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-use-a-wildcard-character-to-delete-folders\/","title":{"rendered":"How Can I Use a Wildcard Character to Delete Folders?"},"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 delete folders based on a wildcard character? For example, how can I delete all the folders whose name starts with <I>December<\/I>?<BR><BR>&#8212; RR<\/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, RR. Well, that depends. If you\u2019re running Windows XP or Windows Server 2003, you can actually <I>use<\/I> a wildcard character to identify and then delete all the folders whose name starts with <I>December<\/I>. If you\u2019re running Windows 2000, you can achieve the same net result: you can identify and delete all the folders whose name starts with <I>December<\/I>. It\u2019s just that you\u2019ll have to do it in a more clumsy, brute-force fashion.<\/P>\n<P>Let\u2019s start by taking a look at an XP\/2003 script that identifies (but doesn\u2019t yet delete) all the folders whose name starts with <I>December<\/I>:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colFolders = objWMIService.ExecQuery _\n    (&#8220;SELECT * FROM Win32_Directory WHERE FileName LIKE &#8216;December%'&#8221;)<\/p>\n<p>For Each objFolder in colFolders\n    Wscript.Echo objFolder.Name\nNext\n<\/PRE>\n<P>The key to this script lies in the WQL query:<\/P><PRE class=\"codeSample\">SELECT * FROM Win32_Directory WHERE FileName LIKE &#8216;December%&#8217;\n<\/PRE>\n<P>What we\u2019re doing here is selecting all the folders that have a <B>FileName<\/B> like <I>December<\/I>. FileName is the WMI property corresponding to what we would call the folder name. (And, yes, there\u2019s sometimes a difference between the name we use for something and the name WMI uses for that same thing.) For example, if we have a folder with the path C:\\Scripts\\Reports\\December, the FileName is <I>December<\/I>. <\/P>\n<P><B>LIKE \u2018December%\u2019<\/B> simply means, \u201cWhere the FileName starts with <I>December<\/I>.\u201d The percent sign (%) is the wildcard character (equivalent to the * used most other places as the wildcard character). In this case, the wildcard indicates that we want folder names that start with <I>December<\/I>, but we don\u2019t care what &#8211; if anything &#8211; comes after the word <I>December<\/I>. What if wanted to look for folders whose names <I>ended<\/I> with the word <I>December<\/I>? In that case, we\u2019d put the wildcard at the beginning of the string, like so:<\/P><PRE class=\"codeSample\">SELECT * FROM Win32_Directory WHERE FileName LIKE &#8216;%December&#8217;\n<\/PRE>\n<P>And what if we wanted folders whose name simply contained the word <I>December<\/I> (for example, <I>Reports_December_2005<\/I>)? In that case, we\u2019d put a wildcard before and after the string:<\/P><PRE class=\"codeSample\">SELECT * FROM Win32_Directory WHERE FileName LIKE &#8216;%December%&#8217;\n<\/PRE>\n<P>The LIKE operator is available only on Windows XP and Windows 2003. If you want to perform this task on a Windows 2000 computer, you\u2019ll have to retrieve the list of <I>all<\/I> the folders on the machine and then check to see if any of them have a folder name (FileName) starting with <I>December<\/I>. That\u2019s what we do with this script:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colFolders = objWMIService.ExecQuery(&#8220;SELECT * FROM Win32_Directory&#8221;)<\/p>\n<p>For Each objFolder in colFolders\n    If Left(objFolder.FileName, 8) = &#8220;december&#8221; Then\n        Wscript.Echo objFolder.Name\n    End If\nNext\n<\/PRE>\n<P>With this script we get back a collection of all the folders on a computer, and then check each one to see if the first (leftmost) 8 characters happen to be <I>december<\/I>. If they are, we echo the folder name; if not, we move on to the next folder in the collection.<\/P>\n<P>This works just fine, although it does take a bit longer to complete. On a test computer with a couple thousand folders, the XP\/2003 version took around 15 seconds to complete; the Windows 2000 version took over a minute. But at least it does the job.<\/P>\n<P>Now, what about deleting all the folders that you find? Well, to do that, just substitute the <B>Delete<\/B> method for the line of code where we echo the folder name. For example, here\u2019s the XP\/2003 version of a script that both finds and deletes all folders whose name starts with <I>December<\/I>:<\/P><PRE class=\"codeSample\">On Error Resume Next<\/p>\n<p>strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colFolders = objWMIService.ExecQuery _\n    (&#8220;SELECT * FROM Win32_Directory WHERE FileName LIKE &#8216;December%'&#8221;)<\/p>\n<p>For Each objFolder in colFolders\n    objFolder.Delete\nNext\n<\/PRE>\n<P>Needless to say, be careful about running this particular script; after all, it will delete all the folders it finds whose name starts with <I>December<\/I>. In some cases, it might be a good idea to first retrieve a list of all these folders and make sure there aren\u2019t any surprises in there (that is, folders you don\u2019t really want to delete that just happen to have a name starting with <I>December<\/I>) before you start deleting things willy-nilly.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I delete folders based on a wildcard character? For example, how can I delete all the folders whose name starts with December?&#8212; RR Hey, RR. Well, that depends. If you\u2019re running Windows XP or Windows Server 2003, you can actually use a wildcard character to identify and then delete all [&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":[11,3,12,5],"class_list":["post-70453","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-folders","tag-scripting-guy","tag-storage","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I delete folders based on a wildcard character? For example, how can I delete all the folders whose name starts with December?&#8212; RR Hey, RR. Well, that depends. If you\u2019re running Windows XP or Windows Server 2003, you can actually use a wildcard character to identify and then delete all [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70453","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=70453"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70453\/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=70453"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70453"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70453"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}