Suppression d'une chaine de caractères dans un fichier

cs_maya77 Messages postés 36 Date d'inscription vendredi 13 novembre 2009 Statut Membre Dernière intervention 18 juin 2011 - 9 mai 2011 à 15:03
cs_maya77 Messages postés 36 Date d'inscription vendredi 13 novembre 2009 Statut Membre Dernière intervention 18 juin 2011 - 9 mai 2011 à 20:58
bonjour

je voudrais supprimer une chaîne de caractères en début de fichier jusqu'à une certaine chaîne de caractère.
exemple
<doc>

.......
.......


.....

enfaîte je voudrais supprimer tout ce qui est avant le

merci

4 réponses

cs_cheyenne Messages postés 693 Date d'inscription samedi 18 mai 2002 Statut Membre Dernière intervention 17 avril 2017 2
9 mai 2011 à 15:32
Bonjour,

Passes par Instr pour rechercher la position de et Mid$ ou Right$ pour récupérer ce qui est après , ou bien tu fais un Split sur .

Cheyenne
0
foliv57 Messages postés 420 Date d'inscription vendredi 17 novembre 2006 Statut Membre Dernière intervention 15 juillet 2014 9
9 mai 2011 à 16:05
Bonjour,

Tout dépend de la taille du fichier à traiter. Si le fichier est petit, on peut tout lire en une seul fois, trouver et écrire tout le reste en une seul fois à nouveau.

Si le fichier à traiter est de taille important, vous pouvez utiliser ce genre de méthode
Imports System.IO

Private Sub DeleteAllBefore(ByVal filePath As String, ByVal searchString As String)

        'Création d'un chemin vers un fichier temporaire (dans le même dossier que le fichier source)
        Dim tempPath As String = Path.Combine(Path.GetDirectoryName(filePath), Path.GetFileName(Path.GetTempFileName))
        Dim ligne As String = String.Empty
        Dim finded As Boolean = False

        'Création d'un objet qui va lire dans le fichier source (reader) et d'un objet qui va écrire dans
        'le fichier temporaire (writer)
        Using reader As New StreamReader(filePath), writer As New StreamWriter(tempPath)

            'Lit dans le fichier source tant que la ligne recherchée n'est pas trouvée
            'et que la fin de fichier n'est pas atteinte
            While ((Not finded) AndAlso (Not reader.EndOfStream))
                ligne = reader.ReadLine()
                finded (ligne.Trim.ToLower() searchString.Trim.ToLower())
            End While

            'Transfert toutes les lignes restantes dans le fichier temporaire
            While (Not reader.EndOfStream)
                writer.WriteLine(ligne)
                ligne = reader.ReadLine()
            End While

        End Using

        'Si la ligne recherchée est trouvée, on remplace la source par le fichier temporaire
        If finded Then
            File.Copy(tempPath, filePath, True)
        Else
            Throw New ApplicationException(String.Format("La chaine {0} n'a pas pu être trouvée dans le fichier {1}", searchString, filePath))
        End If

        'Suppression du fichier temporaire
        File.Delete(tempPath)

End Sub


Utilisation
DeleteAllBefore("C:\MonFichier.txt", "")
0
cs_ShayW Messages postés 3253 Date d'inscription jeudi 26 novembre 2009 Statut Membre Dernière intervention 3 décembre 2019 57
9 mai 2011 à 16:34
Salut

une autre proposition

Private Sub readfile()
        Dim path As String

        Dim found As Boolean
        path = "e:\test.txt"
        Try
            listitem = System.IO.File.ReadAllLines(path).ToList
        Catch ex As Exception

        End Try
        Dim listmodifi As New List(Of String)
        found = False
        For Each element In listitem
            If found Then
                listmodifi.Add(element)
            End If
            If element.ToString.Contains("") Then
                found = True
            End If
        Next
        'change le path si tu ne veux pas écrire dans le meme fichier

        Try
            System.IO.File.WriteAllLines(path, listmodifi.ToArray)
        Catch ex As Exception

        End Try
        

    End Sub
0
cs_maya77 Messages postés 36 Date d'inscription vendredi 13 novembre 2009 Statut Membre Dernière intervention 18 juin 2011
9 mai 2011 à 20:58
merci beaucoup pour vos réponses, je vais essayer de suite vos solutions.

maya
0
Rejoignez-nous