Results 1 to 5 of 5

Thread: [RESOLVED] Read files, get highest numeric value from file name.

  1. #1

    Thread Starter
    Addicted Member sRLS's Avatar
    Join Date
    Mar 2010
    Location
    Ladson SC
    Posts
    156

    Resolved [RESOLVED] Read files, get highest numeric value from file name.

    I have a list box populated with file names from a folder, each file name ends with a "_A1" Underscore, Alpha character and a numeric value, the Alpha and Numeric values vary. The Numeric value can vary in length.
    Example File names:
    6081058_E_2020_Part 1 of 2_A99.zmp
    6081058_E_2020_Part 1 of 2_D12.zmp
    I'm able to extract the numeric value in the code below, where I am struggling is how to write code to select the highest numeric value without user interaction and place that value in a text box.

    Code:
        Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
            sLastNumber = ListBox1.Text
            LastNumber.Text = sLastNumber.Substring(sLastNumber.LastIndexOf("_") + 1)
            NextNo.Text = Replace(LastNumber.Text, "D", "") + 1
        End Sub
    This is the code to populate my list box.

    Code:
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            'Set up sMyPath string and variables
            sJobNumber = JobNumber.Text
            sOpNumber = OpNumber.Text
            sPartX1 = PartX1.Text
            sPartX2 = PartX2.Text
            sMyPath = "\\ACCURATENAS\CMM_Vault\CMM - Inspection Results & Zeiss Table Files_Vault\CMM-Inspection-Measuring-Points\" & sJobNumber & "\" & sOpNumber & "\" & "Part " & sPartX1 & " of " & sPartX2 & "\"
            sMsgPath = sJobNumber & "\" & sOpNumber & "\" & "Part " & sPartX1 & " of " & sPartX2
            ListBox1.Items.Clear()
    
            'Check if path exists
            If CheckPathParts(sMyPath) Then
                'MessageBox.Show("The path exists!")
                Try
                    Dim pathToCheck As String = sMyPath
                    If My.Computer.FileSystem.GetFiles(pathToCheck).Count Then
                        For Each file As String In My.Computer.FileSystem.GetFiles(pathToCheck)
                            ListBox1.Items.Add(IO.Path.GetFileNameWithoutExtension(file))
                        Next
                    End If
                    'End If
                Catch ex As Exception ' this section of code doesnt appear to function or catch errors.
                    MsgBox("Message: " & ex.Message & vbNewLine & "StackTrace: " & ex.StackTrace, MsgBoxStyle.Critical, "Error...")
                End Try
            Else
                'MsgBox(sSubDirFailed)
                MsgBox("There are no files in: " & sMsgPath & ": Or the path doesn't exist: " & sSubDirFailed & ". Check to ensure entered data is correct.")
            End If
        End Sub
    Any help, examples or reading materials for a new VB user is appreciated.
    Regards

    Rick
    VS 2022, Office365, Win10
    I became insane, with long bouts of horrible sanity!

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: Read files, get highest numeric value from file name.

    Your use of Substring gets everything after the last _ so you end up with something like "A99.zmp"

    If you want to extract just the numeric value then you need to start one character later (so change +1 to +2), and only take a particular amount of characters, eg:
    Code:
    LastNumber.Text = sLastNumber.Substring(sLastNumber.LastIndexOf("_") + 2, 2)
    The Numeric value can vary in length.
    In order to determine how many characters you want, find the last "_" character and the last "." character, and subtract one from the other.

  3. #3
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Read files, get highest numeric value from file name.

    The following code extract Alpha character and Numeric value whatever its length
    VB.NET Code:
    1. Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
    2.         Dim strFileName As String = IO.Path.GetFileNameWithoutExtension(ListBox1.SelectedItem.ToString)
    3.         Dim i As Integer = strFileName.LastIndexOf("_"c)
    4.  
    5.         TextBox1.Text = strFileName.Substring(i + 1, 1) ' Alpha character
    6.         TextBox2.Text = strFileName.Substring(i + 2) + 1 ' Next numeric value
    7.     End Sub



  4. #4

    Thread Starter
    Addicted Member sRLS's Avatar
    Join Date
    Mar 2010
    Location
    Ladson SC
    Posts
    156

    Re: Read files, get highest numeric value from file name.

    Quote Originally Posted by 4x2y View Post
    The following code extract Alpha character and Numeric value whatever its length
    VB.NET Code:
    1. Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
    2.         Dim strFileName As String = IO.Path.GetFileNameWithoutExtension(ListBox1.SelectedItem.ToString)
    3.         Dim i As Integer = strFileName.LastIndexOf("_"c)
    4.  
    5.         TextBox1.Text = strFileName.Substring(i + 1, 1) ' Alpha character
    6.         TextBox2.Text = strFileName.Substring(i + 2) + 1 ' Next numeric value
    7.     End Sub
    Amazing!
    Your 5 lines of code replaces my 15 line attempt.
    Regards

    Rick
    VS 2022, Office365, Win10
    I became insane, with long bouts of horrible sanity!

  5. #5

    Thread Starter
    Addicted Member sRLS's Avatar
    Join Date
    Mar 2010
    Location
    Ladson SC
    Posts
    156

    Re: Read files, get highest numeric value from file name.

    si_the_geek and 4x2y
    Thank you for your time!
    Last edited by sRLS; Aug 14th, 2017 at 10:28 AM.
    Regards

    Rick
    VS 2022, Office365, Win10
    I became insane, with long bouts of horrible sanity!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width