vbscript - manipulating strings

Posted on 2007-10-17
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:


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


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.

Question by:dan29
    LVL 38

    Accepted Solution

    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.

    Use UBound to determine the last element in the array.

    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) & "\"

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

    Expert Comment

    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)



    Author Comment

    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.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    This article will show, step by step, how to integrate R code into a R Sweave document
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    730 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now