tags:

views:

81

answers:

2

Using VB6

Code.

Dim posn As Integer, i As Integer
    Dim fName As String
    posn = 0
    For i = 1 To Len(flname)
        If (Mid(flname, i, 1) = "\") Then posn = i
    Next i
    fName = Right(flname, Len(flname) - posn)
    posn = InStr(fName, ".")
        If posn <> 0 Then
           fName = Left(fName, posn - 1)
       End If
    GetFileName = fName



FileName: Clockings8.mis06042009 120442.fin

But it is showing a filename is “Clockings8”. It should show “Clockings8.mis06042009 120442”

How to modify a code?

Need vb6 code Help

+2  A: 

It is a bit cleaner to use the Scripting.FileSystemObject component. Try:

Dim fso as New Scripting.FileSystemObject
GetFileName = fso.GetBaseName(fname)

The reason why your code stops short is that InStr works from the beginning of the string to the end and stops wherever it finds a match. The filename "Clockings8.mis06042009 120442.fin" contains two periods. For this reason, you should use InStrRev instead to start the search from the end of the string.

David Andres
+1  A: 

Going with FileSystemObject's GetBaseName like David suggests is a good idea. If you can't or don't want to (and there are reasons why you might not want to) work with the FileSystemObject there's a simple solution: Remove all characters from a filename string starting with the last dot in the name.

Here's what I mean:

Dim fn As String
fn = "Clockings8.mis06042009 120442.fin"

Dim idx As Integer
idx = InStrRev(fn, ".")
GetFileName = Mid(fn, 1, idx - 1)

If your filename does not have an extenstion but has a dot somewhere in the filename string, then this method will return bad results.

Jay Riggs