Imports System.Text.RegularExpressions
Imports System.ComponentModel
Public Class Form1
Dim WithEvents backwork As New BackgroundWorker
Dim Status As New StatusStrip
Dim StatusLabel = New ToolStripStatusLabel() With {.Text = "Ready."}
Dim StatusProgressBar As New ToolStripProgressBar() With {.Style = ProgressBarStyle.Marquee, .Visible = False, .MarqueeAnimationSpeed = 1}
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Control.CheckForIllegalCrossThreadCalls = False
Me.Status.Items.Add(StatusProgressBar)
Me.Status.Items.Add(StatusLabel)
Me.Controls.Add(Status)
'----------------------
Me.TextBox1.ReadOnly = True
Me.TextBox2.ReadOnly = True
End Sub
Dim الملف_الرئيسي As String
Dim مرجع_التقسيم As String
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Using o As New OpenFileDialog
o.Filter = "Text files (*.txt)|*.txt"
If o.ShowDialog = Windows.Forms.DialogResult.OK Then
الملف_الرئيسي = o.FileName
TextBox1.Text = IO.Path.GetFileName(o.FileName)
End If
End Using
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Using o As New OpenFileDialog
o.Filter = "Text files (*.txt)|*.txt"
If o.ShowDialog = Windows.Forms.DialogResult.OK Then
مرجع_التقسيم = o.FileName
TextBox2.Text = IO.Path.GetFileName(o.FileName)
Me.Button1.Enabled = False
Me.Button2.Enabled = False
Me.TextBox1.Enabled = False
Me.TextBox2.Enabled = False
Me.TextBox3.Enabled = False
Me.TextBox4.Enabled = False
Me.ListBox1.Items.Clear()
StatusProgressBar.Visible = True
backwork.RunWorkerAsync()
End If
End Using
End Sub
Private Sub backwork_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles backwork.DoWork
Try
Dim sourceLines() As String = IO.File.ReadAllLines(الملف_الرئيسي)
' header ------------------------------
Me.StatusLabel.Text = "header working..."
Dim header(7 - 1) As String
Array.Copy(sourceLines, 0, header, 0, 7)
Me.StatusLabel.Text = "header completed"
' details -----------------------------
Me.StatusLabel.Text = "details working..."
Dim details(sourceLines.Count - 7 - 1) As String
Array.Copy(sourceLines, 7, details, 0, sourceLines.Count - 7)
For i = 0 To details.Length - 1
details(i) = details(i).Substring(9, 4) & " - " & details(i)
Next
Array.Sort(details)
For i = 0 To details.Length - 1
details(i) = details(i).Substring(7)
Next
Me.StatusLabel.Text = "header completed"
'splitfiles ---------------------------------------------
Dim files() As String = IO.File.ReadAllLines(مرجع_التقسيم)
For Each f As String In files
If f.Trim <> "" Then
Dim filename As String = f.Substring(0, 8)
Me.StatusLabel.Text = filename & " working..."
Dim flagStart As Integer = Val(f.Substring(9, 4))
Dim flagEnd As Integer = Val(f.Substring(14, 4))
Dim flagCount As Integer = Val(f.Substring(19))
Dim range = Enumerable.Range(flagStart, flagEnd - flagStart + 1)
Dim RegexPattern As String = "^\w(\d{3})\s\d{4}(" & String.Join("|", range) & ")\s.+"
Dim ms As MatchCollection = Regex.Matches(String.Join(vbNewLine, details), RegexPattern, RegexOptions.Multiline)
Dim resultLines = (From x As Match In ms Select x.Value).ToArray
If flagCount <> ms.Count Then
ListBox1.Items.Add(filename)
End If
Dim resultFile As String = String.Empty
resultFile &= "date: xxxxxxxxx" & vbNewLine
resultFile &= "mois: xxxxxxxxxx " & vbNewLine
resultFile &= "project: xxxxxxxxx" & vbNewLine
resultFile &= "observation: xxxxxxxx" & vbNewLine
resultFile &= "unti: xxxxxxxxxxxxxxx" & vbNewLine
resultFile &= "" & vbNewLine
resultFile &= "" & vbNewLine
resultFile &= String.Join(vbNewLine, resultLines) & vbNewLine
IO.File.WriteAllText(filename & ".txt", resultFile)
Me.StatusLabel.Text = filename & " completed"
End If
Next
Me.StatusLabel.Text = "Successful."
Catch ex As Exception
Me.StatusLabel.Text = "Error."
MsgBox(ex.Message)
End Try
StatusProgressBar.Visible = False
Me.Button1.Enabled = True
Me.Button2.Enabled = True
Me.TextBox1.Enabled = True
Me.TextBox2.Enabled = True
Me.TextBox3.Enabled = True
Me.TextBox4.Enabled = True
End Sub
End Class