Implement delete a folder with an apostrophe in the name with VBS _VBS

Source: Internet
Author: User
Tags reserved
Ask:
Hello, Scripting Guy! How do I delete a folder with an apostrophe in the name?
-JH
For:
Hello, JH. Oh, yes, apostrophe: The main source of disaster for each script creator. An apostrophe may look like a simple small character, but don't let its appearance confuse you: an apostrophe (or single quote) might be the most lethal character on the keyboard. Whether you're using Active Directory, a database, or a file system, an apostrophe can bring your script to a full disaster. As Bart Simpson once described Hershey? Milk Dud Candy, the apostrophe is also "sweet outside the poison."
Note: No, Milk Dud is not really a poisonous drug.
The problem with the apostrophe is that it is a "reserved" character, which is a special character of VBScript. For example, suppose you want to delete a folder C:\Scripts. The following script will perform this action:
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
Errresults = Objfolder.delete
Next
If you look closely at the Where clause, you will find that the apostrophe (or single quotation mark) is used to represent a string value: ' C:\\scripts '. That's why we have a problem with a folder with an apostrophe in the name (for example, Ken ' s Scripts). If we try to run the following script, do you guess what will happen?
StrComputer = "."
Set objWMIService = GetObject ("winmgmts:\\" & StrComputer & "\root\cimv2")
Set colfolders = objWMIService. _
ExecQuery ("select * from win32_directory Where Name = ' c:\\ken ' Scripts '")
For each objfolder in Colfolders
Errresults = Objfolder.delete
Next
We're not going to put you in a tight state of waiting: This script is bound to fail. Why? Well, take a look at the WHERE clause:
Where Name = ' c:\\ken ' Scripts '
Because the apostrophe marks the start and end of the string in the WHERE clause, VBScript considers the folder name: ' C:\\ken '. This would have been fine, but it's just too tight. The string also has a string of characters (S Scripts ') behind it. VBScript does not know what these messy data represent, so it just gives up and doesn't try anything.
Yes, we know. But that's how VBScript works. Because the apostrophe is a reserved character, used (and for other purposes) to mark the beginning and end of the string in the WHERE clause. VBScript just doesn't know what we're talking about.
No, it's definitely not a joke. Most unfortunate people who talk to the Scripting Guys. That's a simple question!
So, is there a way to solve this problem? There must be. You may know that each time you use a backslash (\) in the WHERE clause (for example, in a file path), you must place another \ to "escape" the backslash; that's why our file paths resemble c:\\scripts rather than C:\Scripts. We have to do this because \ is also a reserved character, and the only way to instruct VBScript to use it as it is is to escape it (just use another \ as its beginning).
Hey, wait a minute: If you just use another \ As a start, you can use reserved characters \, or you can use reserved characters such as single quotes, by using \ as its beginning.
You know, it really magically worked:
StrComputer = "."
Set objWMIService = GetObject ("winmgmts:\\" & StrComputer & "\root\cimv2")
Set colfolders = objWMIService. _
ExecQuery ("select * from win32_directory Where Name = ' c:\\ken\ ' Scripts '")
For each objfolder in Colfolders
Errresults = Objfolder.delete
Next
Here's the answer, JH. Notice how we put \ in front of the apostrophe, so we get the structure that looks like this: ' c:\\ken\ ' Scripts '. Before any apostrophe in the folder name is preceded by a \, the script will work correctly. Yes, it's a little weird, but it does work.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.