mercredi 23 octobre 2013

Importer un fichier délimité par des séparateurs en VB.

Importer un fichier délimité par des séparateurs en VB.

Voici deux exemples pour récupérer des valeurs séparées par des caractères. Le premier affiche la chaîne de caractères à gauche du séparateur puis affiche celui à droite. Le deuxième exemple fait la même chose mais accepte les séparateurs à plus d’un caractère.
Ces 2 exemples sont parfaits pour sauvegarder des fichiers textes contenant des valeurs associés à des noms ou des clefs. Les séparateurs ou délimiteurs sont souvent représenté par des symboles comme le égal (=) ou par des espaces. D’autre fois, les délimiteurs ont plus d’un caractère. Par exemple : « => »
Si vous aimez ces exemples, soyer libre de partager ou de télécharger le projet exemple en Visual basic ici : SampleTextwithSeperator





    ''' <summary>
    ''' This function contains 3 examples
    '''
    ''' this example is useful if you want to get a value base from a name
    ''' like a parameters file.
    '''
    ''' feel free to share this example.
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub test()
        Dim aString As String = ""
        Dim aSeperator As String = ""
        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        'Exemple : read all the file , line by line, until End Of Stream
        MsgBox("You need to select a text file, for each lines, it will display the line")
        Using oOpenFileDialog As New OpenFileDialog()
            oOpenFileDialog.Title = "Exemple : read all the file until End Of Stream"
            oOpenFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
            oOpenFileDialog.FilterIndex = 1
            oOpenFileDialog.InitialDirectory = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop)
            oOpenFileDialog.Multiselect = False
            If oOpenFileDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
                If IO.File.Exists(oOpenFileDialog.FileName) Then
                    Using oStreamReader As New IO.StreamReader(oOpenFileDialog.FileName)
                        Do
                            'read all the file until End Of Stream
                            aString = oStreamReader.ReadLine
                            MsgBox(aString)
                        Loop Until oStreamReader.EndOfStream = True
                    End Using
                End If
            End If
        End Using

        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        'Exemple : read all the file , line by line, until End Of Stream
        'but if seperated by a SPACE, then display the two values separately
        'it could be any other char
        MsgBox("You need to select a text file, for each lines, it will display each value seperated by a SPACE")
        Using oOpenFileDialog As New OpenFileDialog()
            oOpenFileDialog.Title = "Exemple : read all the file until End Of Stream, ***SEPERATED BY A CHAR***"
            oOpenFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
            oOpenFileDialog.FilterIndex = 1
            oOpenFileDialog.InitialDirectory = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop)
            oOpenFileDialog.Multiselect = False
            If oOpenFileDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
                If IO.File.Exists(oOpenFileDialog.FileName) Then
                    Using oStreamReader As New IO.StreamReader(oOpenFileDialog.FileName)
                        Do
                            'read all the file until End Of Stream
                            aString = oStreamReader.ReadLine
                            MsgBox(aString.Split(" ")(0))
                            MsgBox(aString.Split(" ")(1))
                        Loop Until oStreamReader.EndOfStream = True
                    End Using
                End If
            End If
        End Using


        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        'Exemple : read all the file , line by line, until End Of Stream
        'but if seperated by a STRING = "<>", then display the two values separately
        'the string could be any length.
        MsgBox("You need to select a text file, for each lines, it will display each value seperated by any String (2 char and more), for exemple : '==' ")
        Using oOpenFileDialog As New OpenFileDialog()
            oOpenFileDialog.Title = "Exemple : read all the file until End Of Stream"
            oOpenFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
            oOpenFileDialog.FilterIndex = 1
            oOpenFileDialog.InitialDirectory = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop)
            oOpenFileDialog.Multiselect = False
            If oOpenFileDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
                If IO.File.Exists(oOpenFileDialog.FileName) Then
                    Do
                        aSeperator = InputBox("What will be the seperator to detect inside each lines?", "please enter a sperator")
                    Loop Until aSeperator IsNot Nothing And aSeperator.Length > 0

                    Using oStreamReader As New IO.StreamReader(oOpenFileDialog.FileName)
                        Do
                            'read all the file until End Of Stream
                            aString = oStreamReader.ReadLine
                            If aString.IndexOf(aSeperator) > 0 Then 'important to check if the seperator exist inside before using the SPLIT method
                                MsgBox(aString.Split(New String() {aSeperator}, System.StringSplitOptions.None)(0))
                                MsgBox(aString.Split(New String() {aSeperator}, System.StringSplitOptions.None)(1))
                            End If

                        Loop Until oStreamReader.EndOfStream = True
                    End Using
                End If
            End If
        End Using
    End Sub






This email is free from viruses and malware because avast! Antivirus protection is active.

Aucun commentaire:

Enregistrer un commentaire