Script Tips

Scripting to the Core

In the next series of columns, we'll cover some core scripting techniques. First up: concatenation.

For the next few columns, I’m going to focus on some core techniques that are often confusing for both new and experienced scripters, and I’ll try and give you some tips for making them easier.

This time, I want to focus on concatenation. Let’s say you need to display a phrase like The domain name is “domain” from within a script. Those double quotes can make it hard, because VBScript uses those as a string delimiter. In other words, you can’t just run WScript.Echo "The domain name is "domain"" because VBScript will get confused. There are two easy ways to get it right.

First, type exactly what you want displayed into your script:

The domain name is "domain"

Then, add double quotes to the beginning and end of the phrase, marking the phrase as a string literal:

"The domain name is "domain""

Next, double-up any double quotes apart from the first and last one:

"The domain name is ""domain"""

Looks confusing, but works great. An alternative is to use Chr(34), which produces a double quotation mark, and concatenate it with your other string literals. Start with:

The domain name is "domain"

And convert to:

"The domain name is " & Chr(34) & "domain" & Chr(34)

Everywhere you see Chr(34), VBScript will spit out a double quotation mark. Again, might look a bit confusing, but practice makes perfect, and these two techniques will help make dealing with his confusing issue just a bit easier.

About the Author

Don Jones is a multiple-year recipient of Microsoft’s MVP Award, and is Curriculum Director for IT Pro Content for video training company Pluralsight. Don is also a co-founder and President of PowerShell.org, a community dedicated to Microsoft’s Windows PowerShell technology. Don has more than two decades of experience in the IT industry, and specializes in the Microsoft business technology platform. He’s the author of more than 50 technology books, an accomplished IT journalist, and a sought-after speaker and instructor at conferences worldwide. Reach Don on Twitter at @concentratedDon, or on Facebook at Facebook.com/ConcentratedDon.

comments powered by Disqus
Most   Popular