Imports System
Imports System.IO

Public Class usrCntlMSE_KonvertCSV
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Try

            Dim fd As New OpenFileDialog
            fd.Filter = "CSV Files (*.csv)|*.csv"
            fd.Multiselect = False
            If fd.ShowDialog = DialogResult.OK Then
                Dim CSVFileName = fd.FileName
                If CSVFileName <> "" Then
                    Dim FIO = New System.IO.FileInfo(CSVFileName)

                    Using reader As StreamReader = New StreamReader(CSVFileName, True)
                        Using writer As StreamWriter = New StreamWriter(CSVFileName.Replace(".csv", "_konvertiert.csv"), False)

                            Dim a As String

                            Dim cnt = 0
                            Do
                                a = reader.ReadLine
                                If a IsNot Nothing AndAlso a.Contains(";") Then

                                    Dim split = a.Split(";")

                                    '======================================================================
                                    'Konvertierung RG-NR Format:
                                    '======================================================================
                                    If split.Count > 42 Then
                                        If cnt > 0 Then split(2) = split(42)
                                        split(42) = Nothing
                                    End If

                                    '======================================================================
                                    'HU-GO CardNr; Leistung 22
                                    '======================================================================
                                    If split.Count > 16 And cnt > 0 Then
                                        If split(15) = "22" Then 'HU-GO
                                            If split(14).Contains("Charge - HU-GO SIM card fee '") Then 'HU-GO
                                                If split(14).Contains("'") Then
                                                    Dim splitHuGo = split(14).Split("'") '--> Bezeichnung / Card Nummer auslesen
                                                    If splitHuGo.Count >= 2 Then
                                                        split(17) = "'" & splitHuGo(1) & "'" '--> Card Nummer in Spalte
                                                        split(14) = splitHuGo(0).Trim '--> Bezeichnung in Spalte
                                                    End If
                                                End If
                                            End If
                                        End If

                                    End If
                                    Dim Ergebnis As String = String.Join(";", split)
                                    writer.WriteLine(Ergebnis)

                                    cnt += 1
                                End If
                            Loop Until a Is Nothing
                            MsgBox("Datei erfolgreich konvertiert!")
                        End Using
                    End Using
                    Try
                        'Versuche die originale Datei mit der konvertierten Datei zu ersetzen
                        System.IO.File.Delete(CSVFileName)
                        System.IO.File.Move(CSVFileName.Replace(".csv", "_konvertiert.csv"), CSVFileName)
                    Catch ex As Exception

                    End Try
                End If

            End If
        Catch ex As Exception
            MsgBox(ex.Message & ex.StackTrace)
        Finally
        End Try
    End Sub
End Class

