UpAllNite
asked on
Easy...Path\Filename
Is there a way..I know there is...to separate a path\filename.ext into three variables. One containing the path, the other containing the filename, and the last containing the extension.
For example - c:\windows\system\my.ocx would be:
var1 = "c:\windows\system"
var2 = "my"
var3 = "ocx" or ".ocx"
Thanks in advance
For example - c:\windows\system\my.ocx would be:
var1 = "c:\windows\system"
var2 = "my"
var3 = "ocx" or ".ocx"
Thanks in advance
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
They both worked...Thank You. I just have one question, since they both worked, why the variations in code. Meaning, why does one accomplish the task in 17 lines (hmt) why the latter (swilt) take considerable more. Is this just diffrent styles, or is there another significant reason?
Thanks in advance
Thanks in advance
well you could say that its 2 methods of esolving now it all depends on time versus ammmount of code.
you choose
you choose
I tried to make a generic procedure (nGetPos) for searching backwards, and make all of the 3 main functions separate so that they may be called individually. From my point of view it is the reuse of the code.
Option Explicit
Private Sub Command1_Click()
Dim sFile As String
sFile = Trim$(Text1.Text)
MsgBox "Path = " & sGetFilePath(sFile) & vbCr & _
"Filename = " & sGetFileName(sFile) & vbCr & _
"Extension = " & sGetFileExt(sFile)
End Sub
Private Function sGetFilePath(ByVal sFile As String) As String
Dim p As Integer
p = nGetPos(sFile, "\")
If p > 0 Then sFile = Left$(sFile, p - 1) Else sFile = ""
sGetFilePath = sFile
End Function
Private Function sGetFileName(ByVal sFile As String) As String
Dim p As Integer
p = nGetPos(sFile, "\")
If p > 0 Then sFile = Mid$(sFile, p + 1)
' Now we have the filename without the path
p = nGetPos(sFile, ".")
If p > 0 Then sFile = Left$(sFile, p - 1)
sGetFileName = sFile
End Function
Private Function sGetFileExt(ByVal sFile As String) As String
Dim p As Integer
p = nGetPos(sFile, ".")
If p > 0 Then sFile = Mid$(sFile, p + 1) Else sFile = ""
sGetFileExt = sFile
End Function
Private Function nGetPos(ByVal sText As String, ByVal sChar As String) As Integer
' Search backwards for first sChar
' Returns -1 if not found
Dim i As Integer, p As Integer
p = -1
If sText <> "" Then
For i = Len(sText) To 1 Step -1
If Mid$(sText, i, 1) = sChar Then
p = i
Exit For
End If
Next i
End If
nGetPos = p
End Function