Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

cropping directory name 1

Status
Not open for further replies.

M626

Programmer
Mar 13, 2002
299
I have a variable that i am getting from a dir box. I would like to crop the variable so it's only the last folder listed.

Example:
Var1 = "C:\Temp\test1\aol"

I would like to make

Var1 = "aol"
 
Here are two ways of doing it:

Code:
Option Explicit
Dim Var1 As String
Dim aryTemp() As String

Private Sub Command1_Click()
Var1 = "C:\Temp\test1\aol"
MsgBox Mid$(Var1, (InStrRev(Var1, "\") + 1))
Erase aryTemp
aryTemp = Split(Var1, "\")
MsgBox aryTemp(UBound(aryTemp))
End Sub

Swi
 
Split the string into an array by the '\' then set Var1 to the upper bound of the array. However, if the string ends with '\' then you will need to test for that.

Maybe this world is another planet’s Hell.
Aldous Huxley

eyes_015.gif___1129027102664
eyes_015.gif___1129027102664
 
CreateObject("Scripting.FileSystemObject").GetBaseName(var1)
 
Nice one strongm. You love the one liners. :)

Swi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top