Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Easy...Path\Filename

Posted on 1998-07-06
5
Medium Priority
?
214 Views
Last Modified: 2010-05-03
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
0
Comment
Question by:UpAllNite
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 

Accepted Solution

by:
hmt earned 80 total points
ID: 1465014
hi
well you could run trhough the string backwards and then detect the . and the \
for instance here is your function

    Dim x As Long
    Dim ext As String
    Dim pos_ext As Long
    Dim path As String
    Dim file As String
    Dim p As String
   
    p = "c:\windows\system\Awview32.dll"
    For x = Len(p) To 1 Step -1
        If Mid(p, x, 1) = "." Then
            ext = Mid(p, x, x)
            pos_ext = x
        ElseIf Mid(p, x, 1) = "\" Then
            file = Mid(p, x + 1, (pos_ext - x) - 1)
            path = Mid(p, 1, x)
            Exit For
        End If
    Next
0
 
LVL 2

Expert Comment

by:swilt
ID: 1465015
Put a text box and command button on a form with this 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


0
 

Author Comment

by:UpAllNite
ID: 1465016
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
0
 

Expert Comment

by:hmt
ID: 1465017
well you could say that its 2 methods of esolving now it all depends on time versus ammmount of code.
you choose
0
 
LVL 2

Expert Comment

by:swilt
ID: 1465018
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.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question