Supprimer text dans fichier txt (dotnet)

La Team a résolu l'affaire... Mais ça peut toujours servir !
Avatar du membre
Nash
Administrateur du site
Administrateur du site
Messages : 4403
Enregistré le : sam. 6 nov. 2010 14:19
Localisation : 89
    unknown unknown

Re: Supprimer text dans fichier txt (dotnet)

Message par Nash »

nonnnnn justement j'essayer de raisonner :)
Dim richprov As New RichTextBox
richprov.Text = IO.File.ReadAllText("d:\test.txt")

Dim str = ParseBetween((richprov.Text), "david", "david")
For Each occ In str
richprov.Text = richprov.Text.Replace("david" & occ & "david", "")
Next
Dim myWriter As StreamWriter
Dim myStream As FileStream
myStream = New FileStream(("d:\test.txt"), FileMode.Create)
myWriter = New StreamWriter(myStream)
myWriter.Write(Replace(richprov.Text, Chr(10), Environment.NewLine), RichTextBoxStreamType.PlainText)
myWriter.Close()
myStream.Close()
1) copie du texte dans une textbox (car je n'arrivais pas a parcourir le texte du fichier autrement lol)
2)lecture ligne par ligne et recuperation des lignes entre les deux lignes "david"
3)pour chaque occurrence trouvée , remplacement des lignes "david inclus" par rien ("")
4)ouverture d'un streamwriter ,effacement et ré- ecriture des lignes restantes de la richtextbox dans le fichier
5)fermeture de tout ca

c'est le raisonnement que je m'etais a peu pres dis avant de trouver cette facon ! Pour une fois que j'etais content de moi :P

mais je note et test l'autre facon cité plus haut ;)
Image
• Ryzen 9 5900x
• B550 AORUS PRO 
• RTX 3060ti phoenix 
• thermaltake core p8 tg
• 1 x SSD 500GB   PCIe4 M.2 SEA
• 32 G DDR4 3600 corsaire
• Corsaire 1000w full modulaire passive
• MasterLiquid ML240R RGB
•1 to stockage NVME.M2 PCIe
Avatar du membre
Nash
Administrateur du site
Administrateur du site
Messages : 4403
Enregistré le : sam. 6 nov. 2010 14:19
Localisation : 89
    unknown unknown

Re: Supprimer text dans fichier txt (dotnet)

Message par Nash »

Dim occur = "david"
Dim fPath = "C:\Users\jm\Desktop\test.txt"
Dim lineCount%
Dim originalLines = New Dictionary(Of Integer, String)

Using sr = New StreamReader(fPath)
Dim line = sr.ReadLine()
While line IsNot Nothing
lineCount += 1
originalLines.Add(lineCount, line)
line = sr.ReadLine()
End While
sr.Close()
End Using

Dim FoundedOccur As Collections.Generic.IEnumerable(Of Collections.Generic.KeyValuePair(Of Integer, String)) = originalLines.ToList.Where(Function(i) i.Value.Contains("david"))

Dim minLine% = FoundedOccur.Aggregate(Function(j, k) If(j.Key < k.Key, j, k)).Key
Dim maxLine% = FoundedOccur.Aggregate(Function(j, k) If(j.Key > k.Key, j, k)).Key

If minLine < maxLine Then
Using sw As New StreamWriter(fPath)
For Each l In originalLines
If l.Key < minLine OrElse l.Key > maxLine Then
sw.Write(l.Value & vbNewLine)
End If
Next
sw.Close()
End Using
End If
Nikel , tout marche comme prevue ;)

encore un code sauvegardé dans mon dossier "dotnet" :P

merci sh@rp



Résolu

Macgrath
Image
• Ryzen 9 5900x
• B550 AORUS PRO 
• RTX 3060ti phoenix 
• thermaltake core p8 tg
• 1 x SSD 500GB   PCIe4 M.2 SEA
• 32 G DDR4 3600 corsaire
• Corsaire 1000w full modulaire passive
• MasterLiquid ML240R RGB
•1 to stockage NVME.M2 PCIe
Verrouillé