Export (0) Print
Expand All

Working with Strings

By The Microsoft Scripting Guys

Sesame Script

Welcome to Sesame Script, the column for beginning script writers. The goal of this column is to teach the very basics of Windows scripting for system administration automation. We’ll provide you with the information you’ll need to begin reading and understanding scripts and to start modifying those scripts to suit your own needs. If there’s anything in particular about scripting you’re finding confusing, let us know; you’re probably not alone.

Check the Sesame Script Archives to see past articles.

Knowing Your Right from Your Left

When you were young, did you ever put your shoes on the wrong feet? Have you ever turned right when someone said to turn left? (One of the Scripting Guys has a great story about this; you’ll have to ask him about it later.) Yes, go ahead and admit it, we’ve all done it. But when we get these two directions straight, life is much simpler, isn’t it? You get directly to where you’re going in no time at all, and your feet don’t hurt.

Right and left can be very useful in scripting too. Because of that VBScript provides functions named, appropriately, Right and Left. It also provides a related function named Mid; we didn’t mention it because it didn’t fit in with our story. But Mid is just as useful, and in this article we’ll talk about all three of these functions and how you can use them to work with strings in VBScript.

That’s a Date?

One of the most common ways to use the Right, Left and Mid functions comes when you’re working with dates. For example, suppose you have a log file with a list of dates that look like this:


If you look closely you can see this is just an unformatted date where the month is August (08), the date is the 11th (11) and the year is 2006. And yes, in many locales this is interpreted as November 8th, 2006. But that’s not the point. The point is that the date is a little hard to read like this. Suppose you want to format it to look more like a readable date, something like this:


Using the Right, Left and Mid functions makes this conversion really simple.

How Does It Work?

Let’s step through what these functions do and how to call them. We’ll start with Right, since we can’t go wrong with Right. (Okay, okay, we won’t do that again.) The Right function takes a string and returns a certain number of characters from the right side of that string. Here’s what the definition of the Right function looks like:

string Right(string, integer)

As you can see, this function takes two parameters - a string and an integer - and returns a string. The first parameter is your string, the one you want to select characters from. The second parameter is the number of characters you want to retrieve from the right side of the string. Let’s take another look at an example using the string football:

Character Positions

Suppose we want to extract just the last four characters of this string, or ball. We’d use a script like this:

str = "football"
intCharacters = 4

strNew = Right(str, intCharacters)
Wscript.Echo strNew

First we assign our starting string to the variable str. We then assign the number of characters we want to retrieve from the right side of the string -- in this case 4 -- to the variable intCharacters. Then we run this line of code:

strNew = Right(str, intCharacters)

Here we’re calling the VBScript Right function and passing it our starting string (str) and the number of characters (intCharacters), assigning the output to the variable strNew. We then echo back the value of strNew and this is what we get:


As you can see, we specified that we wanted four characters from the string, starting from the right side. In our string of eight characters, that would be the characters in positions 8, 7, 6, and 5:

Characters Right

Note that, even though we count from the right, the string that is returned maintains the left-to-right format of the original string. That ensures that you don’t end up with something like this:


Note: Could we have reduced the amount of typing and simply written our script like this:

Wscript.Echo Right("football", 4)

Yes, but we broke the script out to make it a little clearer how everything fits together. And using variables is usually helpful anyway: you can give them names that help remind you what they’re for, and then just change out the values assigned to them according to the needs of your script.

No, Your Other Right

That’s great if you want the right side of the string, but what if you want to start at the left? Yes, you’re way ahead of us: just use the VBScript Left function. Here’s what that function looks like:

string Left(string, integer)

Looks familiar doesn’t it? In fact, it’s identical to the Right function, using the exact same parameter types and for exactly the same reasons; the only difference is that, in this case, we’re retrieving characters starting at the left side of the string. Want to get the first 4 characters in the string, in other words, 4 characters starting from the left side of the string?

Characters Left

It’s this simple:

str = "football"
intCharacters = 4

strNew = Left(str, intCharacters)
Wscript.Echo strNew

The only difference between this script and the previous one is that we replace Right with Left. Here’s our output:


Walking the Middle Ground

That’s all fine, but what if we don’t want to start from one end or the other, what if we want to pull characters from the middle of the string? Well, we’re in luck; this is where the Mid function comes in. Here’s how the Mid function is defined:

string Mid(string, integer [, integer])

Whoa, wait a minute - this one looks different. If we compare this to Right and Left it looks like we have parameters for defining a starting string, the number of characters we want to retrieve, and … what’s that last parameter for? Actually the last parameter is the number of characters we want to retrieve and the second parameter is our starting position. Suppose we want to retrieve four characters starting with the character in position 3. Our script would look like this:

str = "football"
intCharacters = 4
intStart = 3

strNew = Mid(str, intStart, intCharacters)
Wscript.Echo strNew

Here we’ve again defined a starting string (“football”) and the number of characters to retrieve (4), but we’ve also defined another variable, intStart, and assigned it the number 3. This is the position in the string where we want to start retrieving characters. We pass these three variables as parameters to the Mid function, and then echo back the string that’s returned:


Hmmm, the output made a lot more sense when we used Left and Right, didn’t it? What’s an otba? (We’re hoping it doesn’t mean anything in some language other than English either, at least nothing bad. If it does let us know before we get into trouble.) Aside from the fact that our example just fell apart at this point, all that really happened here is that we retrieved characters right out of the middle of the string. Let’s look at our string again:

Characters Mid

We took the string football, and, starting with the character in position number 3, we retrieved four characters, the characters in positions 3 through 6.

Yes, we did skip over a minor detail. Take another look at the definition of Mid:

string Mid(string, integer [, integer])

The last parameter, the one where we put in the number of characters to retrieve, has square brackets around it. This means that parameter is optional, so you don’t actually have to tell Mid how many characters to retrieve. What happens if you don’t? Well, in that case Mid simply returns all the characters in the string from the starting position on. In other words, suppose we remove intCharacters from our script, like this:

str = "football"
intStart = 3

strNew = Mid(str, intStart)
Wscript.Echo strNew

In that case our output will look like this:


The only parameters we supplied to the Mid function were the string and the starting point; we left out the parameter that specifies the number of characters. The way Mid dealt with this missing parameter was to give us the string from the starting position (3) to the end of the string. This can come in very handy when you know the starting point but you don’t know how long the string is. For example, you might have a log file where the first eight characters of each line are the date, but you want to retrieve all the information following the date. You could just use the Mid function, pass in the string and a starting position of 9 and leave out the number of characters parameter; in that case, you’d receive the entire string minus the date.

Note: If you didn’t know that square brackets meant the parameter was optional, take a look at the Sesame Script article How to Read an SDK.

Back to Our Date

Speaking of dates, that’s how we started this whole article isn’t it? Since we occasionally like to start what we finish, let’s take another look at that first date we saw at the beginning of the article:


Why don’t you test yourself on this one? Try writing a script that retrieves only the month from this date.

Got it? Cool. Now how about just the year?

Alright, ready for one more? Here’s the tough one: retrieve just the day.

Turns out that wasn’t too difficult after all, was it?

Oh, all right, we’ll go ahead and throw the answers in here, even though we know you figured it out for yourselves.


strDate = "08112006"
intCharacters = 2

strNew = Left(strDate, intCharacters)
Wscript.Echo strNew


strDate = "08112006"
intCharacters = 4

strNew = Right(strDate, intCharacters)
Wscript.Echo strNew


strDate = "08112006"
intCharacters = 2
intStart = 3

strNew = Mid(strDate, intStart, intCharacters)
Wscript.Echo strNew

If you happen to live someplace where this particular date string means November 8 rather than August 11, simply switch the Month and Day scripts above. It’s really that easy.

A Quick Lesson in Dating

Sorry, not that kind of dating; the Scripting Guys wouldn’t be much help there anyway. We mentioned earlier how nice it would be to turn something like 08112006 into 08/11/2006. Using a combination of the functions we’ve been looking at here and string concatenation, this is a pretty simple task. (See the Sesame Script article Special Characters for more on string concatenation.)

strDate = "08112006"
intDayMonthCharacters = 2
intYearCharacters = 4
intStart = 3

strNew = Left(strDate, intDayMonthCharacters)
strNew = strNew & "/"
strNew = strNew & Mid(strDate, intStart, intDayMonthCharacters)
strNew = strNew & "/"
strNew = strNew & Right(strDate, intYearCharacters)

Wscript.Echo strNew

Most of this should look pretty familiar by now. We begin by setting all our variables: we set the date string, the number of characters we’ll be retrieving for the date and the month, the number of characters we’ll be retrieving for the year, and the starting point for when we use the Mid function to retrieve characters from the middle of the string.

Next we call the Left function, passing it the date string and the number of characters to retrieve, and assign the two characters it returns into the variable strNew. At this point strNew looks like this:


We then assign strNew the current value of strNew plus a slash (/):

strNew = strNew & "/"

That gives us a value of strNew like this:


In the next line we use the Mid function to retrieve the two characters starting at position three, and append that value to strNew:

strNew = strNew & Mid(strDate, intStart, intDayMonthCharacters)

So now strNew is equal to:


We once again append a slash to our new string, then use the Right function to retrieve four characters from the right side of our date string and append those characters to strNew:

strNew = strNew & Right(strDate, intYearCharacters)

Finally, we simply echo back the value of our new date string, strNew, and get this:


Right - or Left - to the End

As a special bonus we’re going to throw in one more VBScript function that, along with the Right, Left, and Mid functions, can come in really handy when working with strings. That function is the Len function.

The Len function tells you the length of a string. Here’s the definition:

integer Len(string)

All we have to do is pass in a string and the Len function will return the number of characters that are in that string. For example:

str = "football"

intLen = Len(str)
Wscript.Echo intLen

Here we’ve passed in the string football and echoed back the result, which just happens to be 8.

How would you use this with the functions we’ve already discussed? Well, earlier we gave the example of a string that started with an eight-character date and we wanted the whole string except those first eight characters. For that we simply used the Mid function, passing only the string and starting point as parameters. But suppose you want the whole string except the last eight characters, how would you do that? This would be a pretty sorry example if the answer wasn’t “use the Len function,” so we hope you got that one.

That’s right, using the Len function you can determine the length of the string, subtract eight from the length, then use the Left function to retrieve the beginning of the string. Like this:

str = "Process Ended    08112006"

intLength = Len(str)
intCharacters = intLength - 8
strNew = Left(str, intCharacters)
Wscript.Echo strNew

Here we’ve defined our string, with an eight-character date on the end. We then call the Len function to retrieve the length of the string. In this case our string is 25 characters long, so intLength will be equal to 25. We then subtract 8, the number of characters we want to leave off the end of the string, from the total length of our string (25) giving us a total of 17. Now we simply call the Left function, specifying that we want to retrieve 17 characters starting at the left end of the string.

Additional Resources

We hope you got through that okay without tripping over anything. True story: one of the Scripting Moms once tripped while taking a walk and wound up in the hospital with some broken ribs and internal injuries. It was later discovered she had her shoes on the wrong feet.

Okay, it’s not a true story. The parts about tripping, the hospital, and the injuries are true, but the part about the shoes isn’t - it just fit in so nicely with the rest of this article we had to throw it in. Besides, there really is no explanation as to how this Scripting Mom hurt herself so badly by walking, so we thought it couldn’t hurt to come up with something. Sorry, Mom.

This one is an entirely true story: Less than a year later, Scripting Mom fell off her bicycle. Another trip to the hospital, more broken bones…. We’re currently trying to talk Scripting Mom into some safer activities such as…well…sitting.

Oh, and here are the additional resources:

VBScript Language Reference:

Windows 2000 Scripting Guide:

© 2016 Microsoft