Hey, Scripting Guy! How Can I Extract a User Name and Drive Name From a String Value?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! We currently have user home directory information stored in this format: kenmyer on ‘atl-fs-001\vol’ (U:). How I can extract the user name and drive letter and change the format to this: U:\kenmyer?

— AR

SpacerHey, Scripting Guy! AnswerScript Center

Hey, AR. Well, it’s Monday, day 1 of the TechEd IT Forum, and the Scripting Guys have arrived in Barcelona, Spain. Barcelona is one of the most vibrant and exciting cities in the world, featuring unique architecture; high-culture venues such as the Gran Teatre del Liceu opera theatre and the Picasso Museum; even world-class sports team such as FC Barcelona, two-time winner of the Union of European Football Associations Champions League. Barcelona has a little bit of everything, ranging from the Torre Agbar skyscraper (essentially a 474-foot tall cucumber with windows) to more night clubs, restaurants and tapas bars than you can shake a stick at.

Scripting Guys tip. Don’t actually shake a stick at Barcelona’s night clubs, restaurants, or tapas bars. That’s not very nice.

Today is an especially exciting day for the Scripting Guys, because they don’t have to be at the conference until later this evening. That means that they have the entire day to do whatever they want here in beautiful, dynamic Barcelona. So what will it be? The medieval delights of the Barri Gòtic? The hustle and bustle of La Rambla? The sublime joy of the Joan Miró museum? Well, if there’s time. Before they can get to any of that, however, they’re going to do what any first-time visitor to Barcelona dreams of doing: write a script that can convert a string value like kenmyer on ‘atl-fs-001\vol’ (U:) into a string value like U:\kenmyer.

You know, a script just like this one:

strValue = “kenmyer on ‘atl-fs-001\vol’ (U:)”

arrValues = Split(strValue, ” “)

strName = arrValues(0)

intDrive = Ubound(arrValues) strDrive = arrValues(intDrive) strDrive = Mid(strDrive, 2, 2)

strNewValue = strDrive & “\” & strName Wscript.Echo strNewValue

OK. If we understood your question correctly, AR, you have a series of string values similar to this:

kenmyer on ‘atl-fs-001\vol’ (U:)

What you want to do is convert that string to something along the lines of this; in turn, you can then assign this revised value as the user’s home directory:

U:\kenmyer

We won’t talk about how you assign a user a home directory; after all, we have a previous column that explains that in more detail. However, we will see what we can do about transforming a string like kenmyer on ‘atl-fs-001\vol’ (U:) into a string like U:\kenmyer.

To that end, the first thing we do is assign the starting string to a variable named strValue. We then use this line of code to convert (Split) this string value into an array, specifying that we want to create a new array item each time we encounter a blank space (“ “):

arrValues = Split(strValue, ” “)

What’s the purpose of that? Well, we want the first “word” in our string; that’s the user name and the name we’re going to give our folder. So how do we isolate the first word in the string? The easiest way to do that is to split the string on the blank space, giving us an array that contains the following items:

kenmyer 
on 
‘atl-fs-001\vol’ 
(U:)

Oh, yes, and there is an added bonus: the drive letter (U:) can now be found in the last item in the array. All we have to do is get rid of the parentheses and we’ll be in business.

To that end, the first thing we do is grab the value of the first item in the array (which always has an index number of 0) and store it in a variable named strName. That’s what we do with this line of code:

strName = arrValues(0)

Our next step is to determine the index number of the last item in the array; that’s something we can do by calling the Ubound function:

intDrive = Ubound(arrValues)

Once we have the index number we can retrieve the value of the last item in the array by using this line of code:

strDrive = arrValues(intDrive)

As we noted earlier, that last item has a value that looks like this:

(U:)

How are we going to get rid of those parentheses? Well, we’re going to do that by calling the Mid function, and telling Mid to start at character position 2 and grab a total of 2 characters (in other words, characters 2 and 3), assigning those two characters to the variable strDrive:

strDrive = Mid(strDrive, 2, 2)

And what will strDrive be equal to after that? You got it:

U:

All we have to do now is combine the drive letter, a “\” character, and the user name, and then echo back the results. And that’s exactly what we do in these two lines of code:

strNewValue = strDrive & “\” & strName
Wscript.Echo strNewValue

And there you have it. That was way more exciting than the Parc d’Atraccions amusement park, eh?

Incidentally, this isn’t just an exciting day for the Scripting Guys; it’s an exciting day for visitors to the Script Center as well. As part of the fun and festivities surrounding TechEd IT Forum, each day this week the Script Center will be unveiling new articles and new downloads. And we decided to start the week off with a bang: today we have several articles on Windows PowerShell 2.0, including an updated version of the graphical Windows PowerShell help file. Is that as cool as a visit to Palau Güell? Well, maybe not. But it’s an awfully close second.

But we have to cut today’s column short; after all, we don’t want to waste a minute of our free day in Barcelona. We’ve got lots more scripts to write!

0 comments

Discussion is closed.

Feedback usabilla icon