Link to home
Start Free TrialLog in
Avatar of Steve Williams
Steve WilliamsFlag for United States of America

asked on

How do I return the next Alpha Character when a specific text string is found?

I have about 20-30k .nc files (text files with ".nc" suffix) which are machine code for our CNC turning centers.

Here is a sample of a .nc file

% 
:4082(C-408-2MA-REV. D-OP10 BND 51S2 1.812" 12L14 05/22/08)

Open in new window


And another Sample:

%
O1(CNB-48-2S)
$1
(CNB-48-2S-REV.K)

Open in new window


 I have a Windows form application written in Visual Basic.Net that our operators use to search by part number to find all existing .nc files for whatever part number they are searching for. This found data is then is stored in a datatable which in turn populates a datgridview.

Here is a screenshot of the app with a search and found data:

User generated image

I need to read the 1st few lines of each .nc file find the string "Rev" and then return the next Alpha character following the "Rev" String. Not all .nc files are structured the same. Some have special characters after the "Rev: String some do not.

The sample above has a period, space, then the Alpha Character "D". following the "Rev" String. I need to know how to find and then add the "D" string to the data table so it will be shown in the datagridview on each search.

I'm not very hip on String manipulation any help would be greatly appreciated.

Here is the Code for the Windows Form:

Imports System.IO

Public Class frmCNCPro
    Private Sub CNCPro_Load(sender As Object, e As EventArgs) Handles Me.Load
        Show()
        Me.DoubleBuffered = True
        txtBrowseCNC.Text = GVar.CNCdDir
        txtSearchCNC.Text = frmCPS.txtPartNum.Text
        txtSearchCNC.Focus()
    End Sub
    Private Function Builder() As DataTable
        '############################################################################################
        '# This function starts by searching thru the CNC programs Directory and finds .NC or PPF   #
        '# files depending on the selection from the two radio buttons. Once the data is is parsed  #
        '# it Is stored in a datatable!                                                                                                               #
        GVar.CNCsDir = txtBrowseCNC.Text
        Dim _dtNC As DataTable = New DataTable("FoundNCFiles")
        _dtNC.Columns.Add("FullName")
        _dtNC.Columns.Add("FileName")
        _dtNC.Columns.Add("Folder")
        _dtNC.Columns.Add("CreationTime", GetType(Date))
        _dtNC.Columns.Add("LastWriteTime", GetType(Date))

        Dim ext_nc As String = ".NC"
        'Loop thru each file in the directory and subdirectories                                                    
        Dim di As New DirectoryInfo(GVar.CNCsDir)
        Dim ex_nc As String = "*.NC"
        Dim ex_ppf As String = "*.ppf"
        Dim extUsed As String = ""
        If rbNC_files.Checked = True Then extUsed = ex_nc
        If rbPPF_files.Checked = True Then extUsed = ex_ppf
        For Each ncFile As FileInfo In di.GetFiles(extUsed, SearchOption.AllDirectories)
            Dim _drNC As DataRow = _dtNC.NewRow
            Dim foStr = Path.GetDirectoryName(ncFile.FullName)
            Dim split = foStr.Split("\").LastOrDefault
            _drNC("FullName") = ncFile.FullName
            _drNC("FileName") = ncFile.Name
            _drNC("Folder") = split
            _drNC("CreationTime") = ncFile.CreationTime
            _drNC("LastWriteTime") = ncFile.LastWriteTime
            'add the new row
            _dtNC.Rows.Add(_drNC)
        Next
        Return _dtNC
        '#                                                                                         #
        '###########################################################################################
    End Function

    Public Sub DisplayBuilderData()
        '############################################################################################
        '# This Subroutine uses the data from the Builder() Function and populates a datagridview!  #

        Dim dTableNC As New DataTable
        Dim bsNC As New BindingSource

        dTableNC = Builder()
        bsNC.DataSource = dTableNC
        dgvCNC.DataSource = bsNC
        dgvCNC.Columns(0).Visible = False
        dgvCNC.Columns(1).Width = 250
        dgvCNC.Columns(2).Width = 182
        dgvCNC.Columns(3).Width = 182
        dgvCNC.Columns(4).Width = 182

        bsNC.Filter = String.Format("[FileName] Like '{0}%'", txtSearchCNC.Text)
        '#                                                                                         #
        '###########################################################################################
    End Sub
    Private Sub btnBrowseCNC_Click(sender As Object, e As EventArgs) Handles btnBrowseCNC.Click
        If (fbdCNC.ShowDialog() = DialogResult.OK) Then
            txtBrowseCNC.Text = fbdCNC.SelectedPath
        End If
    End Sub

    Private Sub frmCNCPro_Closing(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        WindowState = FormWindowState.Minimized
        Hide()
        Visible = False
        e.Cancel = True
    End Sub

    Private Sub txtSearchCNC_TextChanged(sender As Object, e As EventArgs) Handles txtSearchCNC.TextChanged, rbNC_files.Click, rbPPF_files.Click
        Timer1.Interval = 1000 'ms
        Timer1.Start()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Timer1.Stop()
        DisplayBuilderData()
    End Sub

    Private Sub dgvCNC_CellContentDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvCNC.CellContentDoubleClick
        If e.ColumnIndex = 1 Then
            Process.Start(dgvCNC.Rows(e.RowIndex).Cells(0).Value.ToString())
        End If
    End Sub

    Private Sub btnCountNC_Click(sender As Object, e As EventArgs) Handles btnCountNC.Click
        ''This Sub-Routine displays the total number of dwf files in the Carter Prints folder on our server.
        Dim dirInfo As New System.IO.DirectoryInfo(GVar.CNCdDir)
        Dim i_NC As Integer
        Dim i_ppf As Integer
        i_NC = dirInfo.GetFiles("*.NC", SearchOption.AllDirectories).Count
        i_ppf = dirInfo.GetFiles("*.ppf", SearchOption.AllDirectories).Count
        MessageBox.Show("Total number of .NC files is " & i_NC & "! And total number of .ppf files is " & i_ppf & "!", "File Count Dialog")
    End Sub
End Class

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Jeff Darling
Jeff Darling
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Steve Williams

ASKER

Both of your solutions helped me get to a correct solution. This gave me a good base to start from and eventually worked into a functional application.

Much appreciation to both.