How Can I Connect to a Folder When There’s an Apostrophe in the Folder Name?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! I’m using the WMI class Win32_Directory to get information about folders on a computer. It works great, except when there’s an apostrophe in the folder name; then, it doesn’t work at all. How can I use WMI to connect to folders that have apostrophes in their names?

— JO

SpacerHey, Scripting Guy! AnswerScript Center

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 ~.)

The problem is that the apostrophe is a “reserved” character in both VBScript and WMI; that means these scripting technologies want to reserve the use of the apostrophe for themselves. That’s fine, except when dealing with folder names like Ken Myer’s folder or people names like O’Donnell or O’Hara. You want to use the apostrophe, because how else can you write the name Ken Myer’s Folder? Meanwhile, VBScript and WMI want exclusive use of the apostrophe for other reasons. As you might expect, that leads to problems.

To explain what we mean, let’s take a look at a script that does work. This one connects to the folder C:\Scripts and tells us whether or not that happens to be a hidden folder:

strComputer = “.”
Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)

Set colFolders = objWMIService. _ ExecQuery(“Select * From Win32_Directory Where Name = ‘C:\\Scripts'”)

For Each objFolder in colFolders Wscript.Echo “Hidden: ” & objFolder.Hidden Next

Notice in our WQL query that single quote marks are used to indicate the name of the folder we want to connect to:

Select * From Win32_Directory Where Name = ‘C:\\Scripts

That’s one of the jobs of a single quote mark: it delineates string values within a WQL query.

Now let’s take a look at a script that doesn’t work, one that tries to connect to a folder named C:\Ken Myer’s Folder:

strComputer = “.”
Set objWMIService = GetObject(“winmgmts!\\” & strComputer & “\root\cimv2”)

Set colFolders = objWMIService. _ ExecQuery(“Select * From Win32_Directory Where Name = ‘C:\\Ken Myer’s Folder”)

For Each objFolder in colFolders Wscript.Echo “Hidden: ” & objFolder.Hidden Next

If you run this script, you’ll get an error. That’s because of the apostrophe in Myer’s. Because apostrophes are used to delineate the beginning and ending of strings, the script thinks this is our WQL query:

Select * From Win32_Directory Where Name = ‘C:\\Ken Myer’

See? It hits the apostrophe and thinks the query is complete. It then encounters the rest of the folder name —’s Folder – and has no idea what to do; after all, that’s not valid VBScript or WMI code. And so the script fails.

So can we work around this problem? Yep. All we need to do is “escape” the apostrophe in Myer’s. When you escape a character you essentially tell the script, “Look, the next character coming up is one of your reserved characters, but I don’t want you to use it as a reserved character; I want you to use it- in this case – as a plain old apostrophe.” To escape a reserved character in a WQL query, we just insert a \ in front of that character. In other words:

Select * From Win32_Directory Where Name = ‘C:\\Ken Myer\’s Folder’

Yes, it looks weird, but it works. What if we had a folder name like O’Hara’s Folder, one that has two apostrophes? In that case you just need to escape each apostrophe, like so:

Select * From Win32_Directory Where Name = ‘C:\\O\’Hara\’s Folder’

Wouldn’t it have been better if Windows didn’t allow apostrophes in folder names, or if VBScript and WMI used a different character to delineate string values? Sure. But for now ….

Here’s the complete script that connects to C:\Ken Myer’s Folder and tells us whether or not the folder is hidden:

strComputer = “.”
Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)

Set colFolders = objWMIService. _ ExecQuery(“Select * From Win32_Directory Where Name = ‘C:\\Ken Myer\’s Folder'”)

For Each objFolder in colFolders Wscript.Echo “Hidden: ” & objFolder.Hidden Next

Incidentally, if you’re using the FileSystemObject (another way to work with files and folders), you don’t have to worry about this; the apostrophe doesn’t seem to bother the FileSystemObject. For example, if you want to know the size of Ken Myer’s Folder, use this code:

Set objFSO = CreateObject(“Scripting.FileSystemObject”)
Set objFolder = objFSO.GetFolder(“C:\Ken Myer’s Folder”)
Wscript.Echo “Size: ” & objFolder.Size

No escaping or other coding tricks required.

0 comments

Discussion is closed.

Feedback usabilla icon