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!

Manipulating a String

Status
Not open for further replies.

pparadis

MIS
Dec 5, 2000
22
0
0
US
I need to pull the filename out of a file path.

Example:

string = "C:\MyDocuments\Work\WorkFile.doc"

I just need to pull out WorkFile.doc

Any ideas?

Thanks.
 
pathname = "C:\MyDocuments\Work\WorkFile.doc"
Do
locale = InStr(pathname, "\")
If locale <> 0 Then pathname = Right(pathname, Len(pathname) - locale)
Loop Until locale = 0

It works. But I keep getting the impression I do things the hard way so if anyone has a better solution, feel free.
 
Or try this

Private Sub Command1_Click()
Dim str As String
str = &quot;C:\MyDocuments\Work\WorkFile.Doc&quot;
str = Mid$(str, (InStrRev(str, &quot;\&quot;, , vbBinaryCompare) + 1))
End Sub
 
Using the InStrRev function is a little quicker.

pathname = &quot;C:MyDocumentsWorkWorkFile.doc&quot;
filename = Mid(pathname, InStrRev(pathname, &quot;\&quot;) + 1)
 
Looks like gumbomudder and I had the same idea. He was just a little quicker.
 
Told you there would be an easier way.

But as I understand Mid, you need a string searched, a string to search and the length of the string you want returning. You don't seem to have the string length?
Am I missing something?
 
Duh.

Look in the help file BEFORE you ask stupid questions Kylua.

It defaults to the remainder of the string.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top