Extract filename

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Extract filename

Post by D Willett »

Ive been trying to extract the filename with the following. The problem I'm having is with our recent extension update 2 & 3 characters ).
The filename has two varients:

L:\MMpdf\ConsoleFiles\98754\98754-01.jpg
(Extracted Result should be: 98754)

or

L:\MMpdf\ConsoleFiles\987540\987540-001.jpg
(Extracted Result should be: 987540)

The character causing the difficulty is the "-"

Code: Select all

Private Sub Command2_Click()
Dim posn As Integer, i As Integer
    Dim fName As String
    Dim GetFilename As String
    posn = 0
    For i = 1 To Len(txtJob)
        If (Mid(txtJob, i, 1) = "-") Then posn = i
    Next i
    fName = Right(txtJob, Len(txtJob) - posn)
    posn = InStr(fName, ".")
        If posn <> 0 Then
           fName = Left(fName, posn - 1)
       End If
    GetFilename = fName
    MsgBox fName
End Sub
Cheers ...

Dave.

User avatar
HansV
Administrator
Posts: 79365
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Extract filename

Post by HansV »

Try this:

Code: Select all

Private Sub Command2_Click()
    Dim pos1 As Integer, pos2 As Integer
    Dim fName As String
    ' Get position of -
    pos1 = InStrRev(txtJob, "-")
    ' Get position of \
    pos2 = InStrRev(txtJob, "\", pos1 - 1)
    ' Extract part between \ and -
    fName = Mid(txtJob, pos2 + 1, pos1 - pos2 - 1)
    MsgBox fName
End Sub
Best wishes,
Hans

D Willett
SilverLounger
Posts: 1728
Joined: 25 Jan 2010, 08:34
Location: Stoke on Trent - Staffordshire - England

Re: Extract filename

Post by D Willett »

Does what it says on the tin !!

Thanks again Hans.

Regards
Cheers ...

Dave.