Solved

CommonDialog Question

Posted on 1998-12-30
5
362 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
[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
5 Comments
 
LVL 8

Expert Comment

by:MikeP090797
ID: 1453247
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
ID: 1453248
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
ID: 1453249
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
ID: 1453250
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
ID: 1453251
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

705 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