-
Aug 12th, 2017, 07:32 AM
#1
Thread Starter
Addicted Member
[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!
-
Aug 12th, 2017, 08:05 AM
#2
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.
-
Aug 12th, 2017, 04:21 PM
#3
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:
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
Dim strFileName As String = IO.Path.GetFileNameWithoutExtension(ListBox1.SelectedItem.ToString)
Dim i As Integer = strFileName.LastIndexOf("_"c)
TextBox1.Text = strFileName.Substring(i + 1, 1) ' Alpha character
TextBox2.Text = strFileName.Substring(i + 2) + 1 ' Next numeric value
End Sub
-
Aug 14th, 2017, 06:32 AM
#4
Thread Starter
Addicted Member
Re: Read files, get highest numeric value from file name.
Originally Posted by 4x2y
The following code extract Alpha character and Numeric value whatever its length
VB.NET Code:
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged Dim strFileName As String = IO.Path.GetFileNameWithoutExtension(ListBox1.SelectedItem.ToString) Dim i As Integer = strFileName.LastIndexOf("_"c) TextBox1.Text = strFileName.Substring(i + 1, 1) ' Alpha character TextBox2.Text = strFileName.Substring(i + 2) + 1 ' Next numeric value 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!
-
Aug 14th, 2017, 06:33 AM
#5
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|