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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Split a string at the last delimiter

Status
Not open for further replies.

tulsantide

Technical User
Jun 1, 2015
21
US
Hi,

I'm trying to split a string at the last delimiter in a vbscript and the following is not working. Please let me know if I'm doing anything wrong.

fname = "C:\Users\userid\Downloads\scripts\test.xlsx"
myarray=Split(fname, "\$",2)
msgbox myarray(0)

I'm expecting the output to be C:\Users\userid\Downloads\scripts

Thanks
 
Hi,

Code:
fname = "C:\Users\userid\Downloads\scripts\test.xlsx"
myarray=Split(fname, "\")
msgbox myarray(UBound(myarray))

BTW, the Split() function has only 2 arguments.

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
Well, that would give the file name, not the path.

The FileSystem Object can to this without string manipulation:

Code:
fname = "C:\Users\userid\Downloads\scripts\test.xlsx"
Set fso = CreateObject("Scripting.FileSystemObject")
msgbox fso.GetParentFolderName(fname)
 
Given you seem to be playing with file and path names, it might make more sense to use the FileSystemObject:

Code:
[blue]    With CreateObject("scripting.filesystemobject")
        MsgBox .GetParentFolderName("C:\Users\userid\Downloads\scripts\test.xlsx")
    End With[/blue]


 
How did I miss that???

Sorry! [blush]

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top