{"id":63603,"date":"2007-11-13T00:35:00","date_gmt":"2007-11-13T00:35:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/11\/13\/hey-scripting-guy-how-can-i-extract-a-user-name-and-drive-name-from-a-string-value\/"},"modified":"2007-11-13T00:35:00","modified_gmt":"2007-11-13T00:35:00","slug":"hey-scripting-guy-how-can-i-extract-a-user-name-and-drive-name-from-a-string-value","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-extract-a-user-name-and-drive-name-from-a-string-value\/","title":{"rendered":"Hey, Scripting Guy! How Can I Extract a User Name and Drive Name From a String Value?"},"content":{"rendered":"<p><H2><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> <\/H2>\n<P>Hey, Scripting Guy! We currently have user home directory information stored in this format: <I>kenmyer on &#8216;atl-fs-001\\vol&#8217; (U:)<\/I>. How I can extract the user name and drive letter and change the format to this: <I>U:\\kenmyer<\/I>?<BR><BR>&#8212; AR<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, AR. Well, it\u2019s Monday, day 1 of the <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/teched07\/default.mspx\"><B>TechEd IT Forum<\/B><\/A>, 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. <\/P>\n<TABLE id=\"EID\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Scripting Guys tip<\/B>. Don\u2019t actually shake a stick at Barcelona\u2019s night clubs, restaurants, or tapas bars. That\u2019s not very nice.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Today is an especially exciting day for the Scripting Guys, because they don\u2019t 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\u00f2tic? The hustle and bustle of La Rambla? The sublime joy of the Joan Mir\u00f3 museum? Well, if there\u2019s time. Before they can get to any of that, however, they\u2019re going to do what any first-time visitor to Barcelona dreams of doing: write a script that can convert a string value like <I>kenmyer on &#8216;atl-fs-001\\vol&#8217; (U:)<\/I> into a string value like <I>U:\\kenmyer<\/I>. <\/P>\n<P>You know, a script just like this one:<\/P><PRE class=\"codeSample\">strValue = &#8220;kenmyer on &#8216;atl-fs-001\\vol&#8217; (U:)&#8221;<\/p>\n<p>arrValues = Split(strValue, &#8221; &#8220;)<\/p>\n<p>strName = arrValues(0)<\/p>\n<p>intDrive = Ubound(arrValues)\nstrDrive = arrValues(intDrive)\nstrDrive = Mid(strDrive, 2, 2)<\/p>\n<p>strNewValue = strDrive &amp; &#8220;\\&#8221; &amp; strName\nWscript.Echo strNewValue\n<\/PRE>\n<P>OK. If we understood your question correctly, AR, you have a series of string values similar to this:<\/P><PRE class=\"codeSample\">kenmyer on &#8216;atl-fs-001\\vol&#8217; (U:)\n<\/PRE>\n<P>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\u2019s home directory:<\/P><PRE class=\"codeSample\">U:\\kenmyer\n<\/PRE>\n<P>We won\u2019t talk about how you assign a user a home directory; after all, we have a <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/dec05\/hey1202.mspx\"><B>previous column<\/B><\/A> that explains that in more detail. However, we will see what we can do about transforming a string like <I>kenmyer on &#8216;atl-fs-001\\vol&#8217; (U:)<\/I> into a string like <I>U:\\kenmyer<\/I>.<\/P>\n<P>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 (<B>Split<\/B>) this string value into an array, specifying that we want to create a new array item each time we encounter a blank space (\u201c \u201c):<\/P><PRE class=\"codeSample\">arrValues = Split(strValue, &#8221; &#8220;)\n<\/PRE>\n<P>What\u2019s the purpose of that? Well, we want the first \u201cword\u201d in our string; that\u2019s the user name and the name we\u2019re 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:<\/P><PRE class=\"codeSample\">kenmyer \non \n&#8216;atl-fs-001\\vol&#8217; \n(U:)\n<\/PRE>\n<P>Oh, yes, and there <I>is<\/I> an added bonus: the drive letter (<B>U:<\/B>) can now be found in the last item in the array. All we have to do is get rid of the parentheses and we\u2019ll be in business.<\/P>\n<P>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\u2019s what we do with this line of code:<\/P><PRE class=\"codeSample\">strName = arrValues(0)\n<\/PRE>\n<P>Our next step is to determine the index number of the <I>last<\/I> item in the array; that\u2019s something we can do by calling the <B>Ubound<\/B> function:<\/P><PRE class=\"codeSample\">intDrive = Ubound(arrValues)\n<\/PRE>\n<P>Once we have the index number we can retrieve the value of the last item in the array by using this line of code:<\/P><PRE class=\"codeSample\">strDrive = arrValues(intDrive)\n<\/PRE>\n<P>As we noted earlier, that last item has a value that looks like this:<\/P><PRE class=\"codeSample\">(U:)\n<\/PRE>\n<P>How are we going to get rid of those parentheses? Well, we\u2019re going to do that by calling the <B>Mid<\/B> 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:<\/P><PRE class=\"codeSample\">strDrive = Mid(strDrive, 2, 2)\n<\/PRE>\n<P>And what will strDrive be equal to after that? You got it:<\/P><PRE class=\"codeSample\">U:\n<\/PRE>\n<P>All we have to do now is combine the drive letter, a \u201c\\\u201d character, and the user name, and then echo back the results. And that\u2019s exactly what we do in these two lines of code:<\/P><PRE class=\"codeSample\">strNewValue = strDrive &amp; &#8220;\\&#8221; &amp; strName\nWscript.Echo strNewValue\n<\/PRE>\n<P>And there you have it. That was <I>way<\/I> more exciting than the Parc d&#8217;Atraccions amusement park, eh?<\/P>\n<P>Incidentally, this isn\u2019t just an exciting day for the Scripting Guys; it\u2019s 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 <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/winpsh\/pshell2.mspx\"><B>Windows PowerShell 2.0<\/B><\/A>, including an updated version of the graphical Windows PowerShell help file. Is that as cool as a visit to Palau G\u00fcell? Well, maybe not. But it\u2019s an awfully close second.<\/P>\n<P>But we have to cut today\u2019s column short; after all, we don\u2019t want to waste a minute of our free day in Barcelona. We\u2019ve got <I>lots<\/I> more scripts to write!<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! We currently have user home directory information stored in this format: kenmyer on &#8216;atl-fs-001\\vol&#8217; (U:). How I can extract the user name and drive letter and change the format to this: U:\\kenmyer?&#8212; AR Hey, AR. Well, it\u2019s Monday, day 1 of the TechEd IT Forum, and the Scripting Guys have arrived in [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[3,4,21,5],"class_list":["post-63603","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-string-manipulation","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! We currently have user home directory information stored in this format: kenmyer on &#8216;atl-fs-001\\vol&#8217; (U:). How I can extract the user name and drive letter and change the format to this: U:\\kenmyer?&#8212; AR Hey, AR. Well, it\u2019s Monday, day 1 of the TechEd IT Forum, and the Scripting Guys have arrived in [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/63603","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=63603"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/63603\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=63603"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=63603"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=63603"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}