{"id":67753,"date":"2006-03-15T20:13:00","date_gmt":"2006-03-15T20:13:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/03\/15\/how-can-i-delete-a-folder-that-has-an-apostrophe-in-the-name\/"},"modified":"2006-03-15T20:13:00","modified_gmt":"2006-03-15T20:13:00","slug":"how-can-i-delete-a-folder-that-has-an-apostrophe-in-the-name","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-delete-a-folder-that-has-an-apostrophe-in-the-name\/","title":{"rendered":"How Can I Delete a Folder That Has an Apostrophe in the Name?"},"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 a folder that has an apostrophe in the name?<BR><BR>&#8212; JH<\/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, JH. Ah, yes, the apostrophe: every script writer\u2019s arch-nemesis. The apostrophe seems like such a simple little character, but don\u2019t let its appearance fool you: the apostrophe (or the single quote) is probably the most deadly character to be found on the keyboard. Whether you\u2019re working with Active Directory, a database, or the file system, a single apostrophe can wreak total havoc on your scripts. As Bart Simpson once said of Hershey\u00ae\u2019s Milk Duds, the apostrophe is \u201csweet on the outside, but poison on the inside.\u201d<\/P>\n<TABLE id=\"EYC\" 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>. No, Milk Duds don\u2019t really have poison on the inside.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>The problem with the apostrophe is that it\u2019s a \u201creserved\u201d character, a character that VBScript uses for its own purposes. For example, suppose you want to delete the folder C:\\Scripts. Here\u2019s a script that will do just that:<\/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. _\n    ExecQuery(&#8220;Select * From Win32_Directory Where Name = &#8216;C:\\\\Scripts'&#8221;)<\/p>\n<p>For Each objFolder in colFolders\n    errResults = objFolder.Delete\nNext\n<\/PRE>\n<P>If you look closely at the Where clause, you\u2019ll see that the apostrophe (or the single quote) is used to delineate a string value: <B>\u2018C:\\\\Scripts\u2019<\/B>. That\u2019s why we run into problems with a folder that has an apostrophe in its name (say, <B>Ken\u2019s Scripts<\/B>). What do you suppose happens when we try running a script like this one:<\/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. _\n    ExecQuery(&#8220;Select * From Win32_Directory Where Name = &#8216;C:\\\\Ken&#8217;s Scripts'&#8221;)<\/p>\n<p>For Each objFolder in colFolders\n    errResults = objFolder.Delete\nNext\n<\/PRE>\n<P>We won\u2019t keep you in suspense: the script will flat-out fail. Why? Well, look at the Where clause:<\/P><PRE class=\"codeSample\">Where Name = &#8216;c:\\\\Ken&#8217;s Scripts&#8217;\n<\/PRE>\n<P>Because the apostrophe marks the beginning and ending of a string in a Where clause, VBScript thinks the name of the folder is this: <B>\u2018C:\\\\Ken\u2019<\/B>. That would be fine, except then we have a bunch of other characters (<B>s Scripts\u2019<\/B>) following the end of the string. VBScript has no idea what that gibberish is supposed to mean, and so it simply gives up without even trying.<\/P>\n<P>Yes, we know. But that\u2019s just the way VBScript works. Because the apostrophe is a reserved character used to (among other things) mark the beginning and end of strings in a Where clause, VBScript simply has no idea what we\u2019re talking about.<\/P>\n<P>And no, no jokes about how that\u2019s like most of the people who\u2019ve had the misfortune of talking to the Scripting Guys. That one\u2019s too easy!<\/P>\n<P>So is there a way to work around this problem? You bet there is. As you probably know, any time we use a backslash (\\) in a Where clause (like in a file path) we need to \u201cescape\u201d that backslash by putting another \\ in front of it; that\u2019s why we have a file path like C:<B>\\\\<\/B>Scripts instead of C:\\Scripts. We have to do that because the \\ is another reserved character; the only way to tell VBScript to use the \\ as-is is by escaping it (which just means prefacing it with another \\). <\/P>\n<P>Hey, wait a minute: if you can use a reserved character like the \\ simply by prefacing it with another \\, maybe you could use a reserved character like the single quote by prefacing <I>it<\/I> with a \\. <\/P>\n<P>You know, that\u2019s just crazy enough to work:<\/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. _\n    ExecQuery(&#8220;Select * from Win32_Directory Where Name = &#8216;C:\\\\Ken\\&#8217;s Scripts'&#8221;)<\/p>\n<p>For Each objFolder in colFolders\n    errResults = objFolder.Delete\nNext\n<\/PRE>\n<P>And there\u2019s your answer right there, JH. Notice how we put a \\ right before our apostrophe; that gives us a construction that looks like this: <B>\u2018C:\\\\Ken\\\u2019s Scripts\u2019<\/B>. Put a \\ before any apostrophes in the folder name and the script will work just fine. Granted, it looks a little odd, but it does the trick.<\/P>\n<P>Just like a Milk Dud.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I delete a folder that has an apostrophe in the name?&#8212; JH Hey, JH. Ah, yes, the apostrophe: every script writer\u2019s arch-nemesis. The apostrophe seems like such a simple little character, but don\u2019t let its appearance fool you: the apostrophe (or the single quote) is probably the most deadly character [&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,4,12,5,6],"class_list":["post-67753","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-folders","tag-scripting-guy","tag-scripting-techniques","tag-storage","tag-vbscript","tag-wmi"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I delete a folder that has an apostrophe in the name?&#8212; JH Hey, JH. Ah, yes, the apostrophe: every script writer\u2019s arch-nemesis. The apostrophe seems like such a simple little character, but don\u2019t let its appearance fool you: the apostrophe (or the single quote) is probably the most deadly character [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67753","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=67753"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67753\/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=67753"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=67753"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=67753"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}