Solved

CommonDialog Question

Posted on 1998-12-30
5
332 Views
Last Modified: 2010-05-18
-=Visual Basic 5   Win95/98=-

I am using CommonDialog to save a file and this is not a problem at all.  What I wish to do is get the name of the file prior to it going to commonDialog.filename.  This gives the path of where the file will be stored.  Once this filename is recieved it will be displayed in a message box which is also not a problem.  Basically, I wish to have the name of the file and not the path of the file stored in a variable so it may be used later in the program.

Thanks
0
Comment
Question by:groone
5 Comments
 
LVL 8

Expert Comment

by:MikeP090797
Comment Utility
After you get the file path, you can use this to get the file name only:
Public Function GetFileExtention(szFile As String) As String
    GetFileExtention = GetAfterSubstring(szFile, ".")
End Function
Public Function GetFileNameAndExt(szFile As String) As String
    Dim iPos As Integer
    iPos = InstrL(szFile, "\")
    If iPos = 0 Then        'the string is either not a filename, or doesn't contain path
        GetFileNameAndExt = szFile
      Else
        GetFileNameAndExt = Right$(szFile, Len(szFile) - iPos)
    End If
End Function
Public Function GetFilePath(szFile As String) As String
    Dim iPos As Integer
    iPos = InstrL(szFile, "\")
    If iPos = 0 Then
        GetFilePath = szFile
      Else
        GetFilePath = Left$(szFile, iPos)
    End If
End Function
Public Function GetFileName(szFile As String) As String
    GetFileName = GetBeforeSubstring(GetFileNameAndExt(szFile), ".")
End Function
Public Function GetBeforeSubstring(szMainString As String, szSubString As String, Optional iOccurence As Integer = 1, Optional bCaseSensetive As Boolean = False) As String
    Dim iPos As Integer
    iPos = InstrN(szMainString, szSubString, iOccurence, bCaseSensetive)
   
    'Return "" if substring is not found
    If iPos = 0 Then
        GetBeforeSubstring = ""
        Exit Function
    End If
   
    GetBeforeSubstring = Left(szMainString, iPos - 1)
End Function
Public Function InstrL(szMainString As String, szSubString As String, Optional bCaseSensetive As Boolean = False) As Integer
    Dim I As Integer
    Dim nLen As Integer
    Dim nLen1 As Integer
    Dim iComparisonMethod As Integer
    Dim bFlag As Boolean
   
    nLen = Len(szMainString)
    nLen1 = Len(szSubString)
    If nLen = 0 Or nLen1 = 0 Then Exit Function
   
    'Set comparison method
    iComparisonMethod = IIf(bCaseSensetive, 0, 1)
   
    'Look for the first substring from the end
    For I = nLen - nLen1 To 1 Step -1
        If InStr(I, szMainString, szSubString, iComparisonMethod) > 0 Then
            bFlag = True
            Exit For
        End If
    Next I
   
    If Not bFlag Then
        InstrL = 0
        Exit Function
    End If
   
    InstrL = I
End Function
'PURPOSE:   Locates the position of the N occurence of a substring in a string
'ARGUMENTS:
'   IN szMainString:    string to search
'   IN szSubString:     substring to look for
'   IN iOccurrance:     the number of the occurence of the substring to return
'   IN bCaseSensetive   Specifies wheteher the string comparison method is case sensetive
'RETURNS:   The position of the N substring in a string
'
Public Function InstrN(szMainString As String, szSubString As String, Optional iOccurance As Integer = 1, Optional bCaseSensetive As Boolean = False) As Integer
    Dim I As Integer
    Dim iComparisonMethod As Integer
    Dim iPos As Integer
   
    iComparisonMethod = IIf(bCaseSensetive, 0, 1)
    iPos = 1
   
    If iOccurance = 1 Then
        InstrN = InStr(1, szMainString, szSubString, iComparisonMethod)
        Exit Function
    End If
   
   
    For I = 1 To iOccurance 'Look for the substring N times
        iPos = InStr(iPos + 1, szMainString, szSubString, iComparisonMethod)
        If iPos = 0 Then    'If no more found, means that there are less occurences then specified
            InstrN = 0
            Exit Function
        End If
    Next I
   
    InstrN = iPos
End Function

0
 
LVL 3

Expert Comment

by:vmano
Comment Utility
i am not sure about VB5 but VB6.0 has commonDialog.fileTitle property to get the file name with out the path. just check for that property and let me know.

vmano
0
 
LVL 1

Expert Comment

by:eab111098
Comment Utility
if i understand you correctly, you want to separate the filename from the path name. if this is correct then its too bad you aren't using VB6. there is a new command that would be perfect for this - its called InStrRev(). this function searches a string backwards for the first occurance of one string w/in another. this is a long overdue function. but, since you're using vb5, we'll have to search backwards ourselves. the following code will get you there.

dim i as integer
dim filename as string
filename = ""
i = len(inputfilename)
do
  filename = mid$(inputfilename,i,1) & filename
  i = i - 1
loop until chr$(i)="\" or i = 0

this code effectively builds filename until it encounters a "\" character or if there is none, i = 0.  in either case, once you drop out of the do loop, filename will contain just the filename part of the path\filename combination.

enjoy
ed.
0
 
LVL 3

Author Comment

by:groone
Comment Utility
Due to my frustration and inability to pay attention to detail, vmano answered my question the best.  Yep, I'll admit it....I didn't look when I was setting up commonDialog.  

MikeP, although your code gets the job done, it is entirely too long and complicated for what I need, so sadly I must reject your answer and ask vmano to take the points since his suggestion minimizes the amount of code used as well as teaches me to stop, step away, and take a break when I get frustrated.

Eab, thanks for your input.  All input is helpful.  I had already intended to parse the filename but was unsure when to do it, but now with the fileTitle property, I know where to use it.

Thanks guys, and take the points vmano.
0
 
LVL 3

Accepted Solution

by:
vmano earned 100 total points
Comment Utility
OK.here is (my) same comment as answer.
i am not sure about VB5 but VB6.0 has commonDialog.fileTitle property to get the file name with out the path. just check for that property in VB5.0 and let me know.

vmano

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now