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

Filepath Manipulation 1

Status
Not open for further replies.

cwalshe

Programmer
Joined
May 28, 2001
Messages
84
Location
IE
Hi there,

Say I have a few file paths:
c:\my docs\mystuff\me.doc
a:\abc.exe
c:\desktop\my folder\my thingy.xls

How can I manipulate each of these string to return to me just the file name and extension. Basically I want to code a line or two that will find the first occurnece of a "\" fom the right and give me everything to the right of it.

Thx,

Cormac.
 
Try these in your immediate window.
[tt]
? mid("c:\my docs\mystuff\me.doc", instrrev("c:\my docs\mystuff\me.doc", "\"))
? mid("a:\abc.exe",instrrev("a:\abc.exe", "\"))
? mid("c:\desktop\my folder\my thingy.xls", instrrev("c:\desktop\my folder\my thingy.xls", "\"))
[/tt]

Wil Mead
wmead@optonline.net

 
Thats almost it except that it returns the "\" as well so instead of me.doc, its returning \me.doc.

Any ideas...
 
This should take care of your problem:

Dim i As Integer
Dim size As Integer
Dim Fname As String
filespec = "A:\test\test.dat"
size = Len(filespec)


For i = size To 1 Step -1


If Mid$(filespec, i, 1) Like "[\:]" Then
Fname = Right(filespec, size - i)
Exit For
End If
Next i
 
Hi,
str = "c:\desktop\my folder\my thingy.xls"
arr() = Split(str, "\")
intA = UBound(arr)
Debug.Print arr(intA)
 
Just one more question,

Say if I had a string that looked like this:

a:\\my.doc

It is possible to check for the extra occurence of the "\" and then get rid of it.

Thx....
 
I believe my code should take care of that.
 
Ok, I don't mean to be annoying or anything but I am hoping to keep the start of the string.

So if I have:

a:\\my.doc

I'd like it to return

a:\my.doc

Basicaly get rid of the duplicate occurence (if it exists) of "\"

Thx....
 
Hi,

str = "a:\\my.doc"
str = Replace(str, "\\", "\")

Jon
 
I am a little confused on what you need. If all you want is the drive letter and file name this should work.

Dim i As Integer
Dim size As Integer
Dim path As String
Dim Fname As String
Dim FinalFile As String
filespec = "a:\test\\test2\\\\\test.dat"
size = Len(filespec)
If Mid(filespec, 2, 2) = ":\" Then
path = Mid(filespec, 1, 3)
Else
MsgBox ("Incorrect naming convention!"), vbCritical
Exit Sub
End If

For i = size To 1 Step -1
If Mid$(filespec, i, 1) Like "[\:]" Then
Fname = Right(filespec, size - i)
Exit For
End If
Next i

FinalFile = path & Fname

If all you want to do is repace double "\" then:

Dim FileFile as String

FileFile = "a:\test\test2\\test.dat

FinalFile = Replace(FinalFile, "\\", "\")
 
The only catch here is that the file has to actually exist.

Function GetLocalFileName(byval strFilename as string) _
as string
dim fso as Scripting.FileSystemObject
dim f as Scripting.File

set fso = new Scripting.FileSystemObject
if fso.FileExists(strFilename) then
set f = fso.GetFile(strFilename)
GetLocalFileName = f.Name
endif
set f = nothing
set fso = nothing
end function

You have to include Microsoft Scripting Runtime (scrrun.dll) in your references.

scarfhead
 
Was there a problem with simply adding 1 to
[tt]InstrRev(string, FindString)[/tt]
?

Wil Mead
wmead@optonline.net

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top