[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

vbscript - manipulating strings

Posted on 2007-10-17
3
Medium Priority
?
355 Views
Last Modified: 2010-04-24
Hello I'm writing a script and I've appeard to come to a screeching halt.  What I am trying to do is get the path to a folder (which I can do without an issue) then add a character in front of the last folder in the path.  So let's say my original folder path is this:

\\(servername)\SMSPackages\test

I need to append the characters zzz_ in front of the last folder in the path, like so

\\(servername)\SMSPackages\zzz_test

I've tried doing this by starting left to right, but all of the characters on the left get removed, so I just end up with the path:  zzz_test.  And I didn't want to start from the right to left, because this script should work for any folder in this path, and they all have different names so they could have a different amount of characters.  I'm sure there has to be a way to do this I just can't seem to figure this out.  Thanks.

-Dan
0
Comment
Question by:dan29
3 Comments
 
LVL 38

Accepted Solution

by:
Shift-3 earned 1000 total points
ID: 20094815
This might not be the most efficient way to do this, but it works.

Use the Split function to divide the string into an array with backslashes as delimiters.
http://www.devguru.com/technologies/vbscript/quickref/split.html

Use UBound to determine the last element in the array.
http://www.devguru.com/Technologies/vbscript/quickref/ubound.html

Use a For...Next loop to append each element up to the last one.

Then append zzz_ and the last element.


So your code could look like this:

strPath = "\\(servername)\SMSPackages\test"
arrPath = Split(strPath,"\")
intLimit = UBound(arrPath)
intParent = intLimit - 1

For i = 0 to intParent
      strResult = strResult & arrPath(i) & "\"
Next

strResult = strResult & "zzz_" & arrPath(intLimit)
Wscript.Echo strResult
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 20097143
Hi, the other way you can do this is to the use the InStrRev function to return the position of the last "\" in the path:

strPath = "\\hostname\SMSPackages\test"
strCharacters = "zzz_"
' Get the path without the last bit
strPathOnly = Left(strPath, InStrRev(strPath, "\"))
' Then get the last bit
strFolder = Mid(strPath, InStrRev(strPath, "\") + 1)
' Put it all together
strFullPath = strPathOnly & strCharacters & strFolder


Of course, that can all be placed on one messy line:
strPath = "\\hostname\SMSPackages\test"
strCharacters = "zzz_"
strFullPath = Left(strPath, InStrRev(strPath, "\")) & strCharacters & Mid(strPath, InStrRev(strPath, "\") + 1)

Regards,

Rob.
0
 

Author Comment

by:dan29
ID: 20102674
Thanks for the help guys.  Shift-3 yours worked so I will use yours as the solution.  But thank you Rob for the alternative solution and I will try this as well.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question