{"id":70783,"date":"2004-12-17T10:14:00","date_gmt":"2004-12-17T10:14:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2004\/12\/17\/how-can-i-connect-to-a-folder-when-theres-an-apostrophe-in-the-folder-name\/"},"modified":"2004-12-17T10:14:00","modified_gmt":"2004-12-17T10:14:00","slug":"how-can-i-connect-to-a-folder-when-theres-an-apostrophe-in-the-folder-name","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-connect-to-a-folder-when-theres-an-apostrophe-in-the-folder-name\/","title":{"rendered":"How Can I Connect to a Folder When There\u2019s an Apostrophe in the Folder 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! I\u2019m using the WMI class Win32_Directory to get information about folders on a computer. It works great, except when there\u2019s an apostrophe in the folder name; then, it doesn\u2019t work at all. How can I use WMI to connect to folders that have apostrophes in their names?<BR><BR>&#8212; JO<\/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, JO. Ah, the apostrophe (also known as the single quote mark). It seems like such a simple little character, and yet it has probably caused script writers more grief than any other character on the keyboard. (Yes, even more than ~.)<\/P>\n<P>The problem is that the apostrophe is a \u201creserved\u201d character in both VBScript and WMI; that means these scripting technologies want to reserve the use of the apostrophe for themselves. That\u2019s fine, except when dealing with folder names like Ken Myer\u2019s folder or people names like O\u2019Donnell or O\u2019Hara. You want to use the apostrophe, because how else can you write the name Ken Myer\u2019s Folder? Meanwhile, VBScript and WMI want exclusive use of the apostrophe for other reasons. As you might expect, that leads to problems.<\/P>\n<P>To explain what we mean, let\u2019s take a look at a script that <I>does<\/I> work. This one connects to the folder C:\\Scripts and tells us whether or not that happens to be a hidden folder:<\/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    Wscript.Echo &#8220;Hidden: &#8221; &amp; objFolder.Hidden\nNext\n<\/PRE>\n<P>Notice in our WQL query that single quote marks are used to indicate the name of the folder we want to connect to:<\/P><PRE class=\"codeSample\">Select * From Win32_Directory Where Name = &#8216;C:\\\\Scripts\n<\/PRE>\n<P>That\u2019s one of the jobs of a single quote mark: it delineates string values within a WQL query.<\/P>\n<P>Now let\u2019s take a look at a script that <I>doesn\u2019t<\/I> work, one that tries to connect to a folder named <B>C:\\Ken Myer\u2019s Folder<\/B>:<\/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 Myer\u2019s Folder&#8221;)<\/p>\n<p>For Each objFolder in colFolders\n    Wscript.Echo &#8220;Hidden: &#8221; &amp; objFolder.Hidden\nNext\n<\/PRE>\n<P>If you run this script, you\u2019ll get an error. That\u2019s because of the apostrophe in <B>Myer\u2019s<\/B>. Because apostrophes are used to delineate the beginning and ending of strings, the script thinks <I>this<\/I> is our WQL query:<\/P><PRE class=\"codeSample\">Select * From Win32_Directory Where Name = &#8216;C:\\\\Ken Myer&#8217;\n<\/PRE>\n<P>See? It hits the apostrophe and thinks the query is complete. It then encounters the rest of the folder name &#8212;<B>\u2019s Folder<\/B> &#8211; and has no idea what to do; after all, that\u2019s not valid VBScript or WMI code. And so the script fails.<\/P>\n<P>So can we work around this problem? Yep. All we need to do is \u201cescape\u201d the apostrophe in <B>Myer\u2019s<\/B>. When you escape a character you essentially tell the script, \u201cLook, the next character coming up is one of your reserved characters, but I don\u2019t want you to use it as a reserved character; I want you to use it- in this case &#8211; as a plain old apostrophe.\u201d To escape a reserved character in a WQL query, we just insert a <B>\\<\/B> in front of that character. In other words:<\/P>\n<P>Select * From Win32_Directory Where Name = &#8216;C:\\\\Ken Myer\\&#8217;s Folder\u2019<\/P>\n<P>Yes, it looks weird, but it works. What if we had a folder name like O\u2019Hara\u2019s Folder, one that has <I>two<\/I> apostrophes? In that case you just need to escape each apostrophe, like so:<\/P><PRE class=\"codeSample\">Select * From Win32_Directory Where Name = &#8216;C:\\\\O\\\u2019Hara\\\u2019s Folder\u2019\n<\/PRE>\n<P>Wouldn\u2019t it have been better if Windows didn\u2019t allow apostrophes in folder names, or if VBScript and WMI used a different character to delineate string values? Sure. But for now \u2026.<\/P>\n<P>Here\u2019s the complete script that connects to C:\\Ken Myer\u2019s Folder and tells us whether or not the folder is hidden:<\/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 Myer\\\u2019s Folder'&#8221;)<\/p>\n<p>For Each objFolder in colFolders\n    Wscript.Echo &#8220;Hidden: &#8221; &amp; objFolder.Hidden\nNext\n<\/PRE>\n<P>Incidentally, if you\u2019re using the FileSystemObject (another way to work with files and folders), you don\u2019t have to worry about this; the apostrophe doesn\u2019t seem to bother the FileSystemObject. For example, if you want to know the size of Ken Myer\u2019s Folder, use this code:<\/P><PRE class=\"codeSample\">Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nSet objFolder = objFSO.GetFolder(&#8220;C:\\Ken Myer\u2019s Folder&#8221;)\nWscript.Echo &#8220;Size: &#8221; &amp; objFolder.Size\n<\/PRE>\n<P>No escaping or other coding tricks required.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I\u2019m using the WMI class Win32_Directory to get information about folders on a computer. It works great, except when there\u2019s an apostrophe in the folder name; then, it doesn\u2019t work at all. How can I use WMI to connect to folders that have apostrophes in their names?&#8212; JO Hey, JO. Ah, the [&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-70783","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! I\u2019m using the WMI class Win32_Directory to get information about folders on a computer. It works great, except when there\u2019s an apostrophe in the folder name; then, it doesn\u2019t work at all. How can I use WMI to connect to folders that have apostrophes in their names?&#8212; JO Hey, JO. Ah, the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70783","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=70783"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70783\/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=70783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}