• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 376
  • Last Modified:

CommonDialog Question

-=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
groone
Asked:
groone
1 Solution
 
MikeP090797Commented:
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
 
vmanoCommented:
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
 
eab111098Commented:
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
 
grooneAuthor Commented:
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
 
vmanoCommented:
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now