Modifier deux lignes successives d'un fichier texte

pierrotm777 Messages postés 24 Date d'inscription vendredi 10 octobre 2008 Statut Membre Dernière intervention 27 mai 2013 - 22 mai 2013 à 17:56
pierrotm777 Messages postés 24 Date d'inscription vendredi 10 octobre 2008 Statut Membre Dernière intervention 27 mai 2013 - 27 mai 2013 à 12:27
Bonjour,
J'ai un fichier texte du type:
LST08154368974||Festnetz
ICO$SKINPATH$include\contacts\userpics\Kai.jpg
LST+4915155666777554||CarHandy
ICOC:\Dokumente und Einstellungen\E39TOURING\Eigene Dateien\RideRunner\Skins\Sample Skin\include\contacts\userpics\0_default.png
LST74123||Torsten
ICOC:\Dokumente und Einstellungen\E39TOURING\Eigene Dateien\RideRunner\Skins\Sample Skin\include\contacts\userpics\0_default.png
LST64123||Peter
ICOC:\Dokumente und Einstellungen\E39TOURING\Eigene Dateien\RideRunner\Skins\Sample Skin\include\contacts\userpics\0_default.png
LST64189||Heidi
ICOC:\Dokumente und Einstellungen\E39TOURING\Eigene Dateien\RideRunner\Skins\Sample Skin\include\contacts\userpics\0_default.png
LST147258||Petra
ICOC:\Dokumente und Einstellungen\E39TOURING\Eigene Dateien\RideRunner\Skins\Sample Skin\include\contacts\userpics\0_default.png
LST55555||Anne
ICOC:\Dokumente und Einstellungen\E39TOURING\Eigene Dateien\RideRunner\Skins\Sample Skin\include\Contacts\userpics\Anne.Jpg
LST789||Mandy
ICOC:\Dokumente und Einstellungen\E39TOURING\Eigene Dateien\RideRunner\Skins\Sample Skin\include\Contacts\userpics\Mandy.Jpg
LST963||Lisa
ICOC:\Dokumente und Einstellungen\E39TOURING\Eigene Dateien\RideRunner\Skins\Sample Skin\include\Contacts\userpics\Lisa.Jpg
LST0815||Hampelmann
ICO$SKINPATH$include\Contacts\userpics\Peter.Jpg


Le fichier est donc une liste de contacts téléphonique, (première ligne) et leur image (deuxième ligne)
Je voudrait pouvoir modifier une paire de ligne précise dont je connaitrait à l'avance le numéro de ligne !

J'ai donc créer un code vb6 mais qui me pose problème:
Public Sub ModifyCustomList(ByVal CustomList As String, ByVal StringToMofify As String, ByVal CLText As String, CLDescription As String, CLImg As String)
    Const ForReading 1: Const ForWriting 2
    Dim objFSO, objFile, allLines, arrLines
    GiveLineNumberForString CustomList, StringToMofify ' return variable ReturnedLineNumberForString
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile(CustomList, ForReading, True, -1)
    allLines = objFile.ReadAll
    arrLines = Split(allLines, vbCrLf)
    arrLines(ReturnedLineNumberForString) = "LST" & CLText & "||" & CLDescription
    arrLines(ReturnedLineNumberForString + 1) = "ICO" & CLImg
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile(CustomList, ForWriting, True, -1)
    objFile.Write (allLines(ReturnedLineNumberForString))
    objFile.Write (allLines(ReturnedLineNumberForString + 1))
    objFile.Close

End Sub


La function qui me retourne le numéro de la première ligne est la suivante:
Retourne la variable 'ReturnedLineNumberForString'
Public Sub GiveLineNumberForString(ByVal FileName As String, ByVal strToSearch As String) 'As Integer
    Dim strTextLine As String
    Dim l As Long
    'ReturnedLineNumberForString = 0
    
    Open FileName For Input As #1
    Do While Not EOF(1)
       l = l + 1
       Line Input #1, strTextLine
       If InStr(strTextLine, strToSearch) Then
           'MsgBox strToSearch & " is on line " & l
           ReturnedLineNumberForString = l
       End If
    Loop
    'Return
    Close #1
End Sub

Quelle est mon erreur svp !

7 réponses

ucfoutu Messages postés 18038 Date d'inscription lundi 7 décembre 2009 Statut Modérateur Dernière intervention 11 avril 2018 211
22 mai 2013 à 18:03
Bonjour,
La première gaucherie (pas erreur) est sans aucun doute de te servir à la fois de l'instruction Open de VB (ça, c'est très bien) et de FSO (pourquoi diable ? et pourquoi alourdir ainsi ?)
Outre la lourdeur inévitablement ainsi ajoutée, tu prends des risques ! VBS (et donc FSO dans la foulée) peut avoir été inhibé sur certaines machines (je l'ai inhibé sur la mienne, par exemple !) pour des raisons de sécutité.


________________________
Réponse exacte ? => "REPONSE ACCEPTEE" facilitera les recherches.
Pas d'aide en ligne installée ? => ne comptez pas sur moi pour simplement répéter son contenu. Je n'interviendrai que si nécessité de la compléter.
0
pierrotm777 Messages postés 24 Date d'inscription vendredi 10 octobre 2008 Statut Membre Dernière intervention 27 mai 2013
22 mai 2013 à 18:13
J'ai modifié mon code et j'obtient un résultat mais pas sur les bonnes lignes !
Public Sub ModifyCustomList(ByVal CustomList As String, ByVal StringToMofify As String, ByVal CLText As String, CLDescription As String, CLImg As String)
    Const ForReading 1: Const ForWriting 2
    Dim objFSO, objFile, allLines, arrLines, x
    GiveLineNumberForString CustomList, StringToMofify
    'RRSDK.SetUserVar "StringToMofify", StringToMofify & " --> " & ReturnedLineNumberForString
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile(CustomList, ForReading, True, -1)
    'http://forums.aspfree.com/code-bank-54/classic-asp-vbscript-write-into-specific-line-text-file-92390.html
    allLines = objFile.ReadAll
    arrLines = Split(allLines, vbCrLf)
    
    arrLines(ReturnedLineNumberForString) = "LST" & CLText & "||" & CLDescription & vbCrLf
    arrLines(ReturnedLineNumberForString + 1) = "ICO" & CLImg & vbCrLf
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile(CustomList, ForWriting, True, -1)
    For x = 1 To UBound(arrLines)
    If x = ReturnedLineNumberForString Then
        objFile.Write (arrLines(ReturnedLineNumberForString))
    End If
    If x = ReturnedLineNumberForString + 1 Then
        objFile.Write (arrLines(ReturnedLineNumberForString + 1))
    End If
    If x < ReturnedLineNumberForString Or x > ReturnedLineNumberForString + 1 Then
        objFile.Write (arrLines(x) & vbCrLf)
    End If
    Next
    objFile.Close

End Sub
0
pierrotm777 Messages postés 24 Date d'inscription vendredi 10 octobre 2008 Statut Membre Dernière intervention 27 mai 2013
22 mai 2013 à 18:14
Merci de votre réponse , je connais mieux vb.net que vb6 et j'ai trouvé ce code que j'ai un peu adapté !
0
pierrotm777 Messages postés 24 Date d'inscription vendredi 10 octobre 2008 Statut Membre Dernière intervention 27 mai 2013
22 mai 2013 à 18:17
Sous VB.NET, mon code était le suivant !
    Private Sub DeleteCustomList(ByVal CustomList As String, ByVal StringToDelete As String)
        Dim lines As New List(Of String)(IO.File.ReadAllLines(CustomList))
        'Remove the line to delete, e.g.
        lines.RemoveAt(GiveLineNumberForString(CustomList, StringToDelete))
        lines.RemoveAt(GiveLineNumberForString(CustomList, StringToDelete))
        IO.File.WriteAllLines(CustomList, lines.ToArray(), Encoding.Unicode)
    End Sub

    Private Sub ModifyCustomList(ByVal CustomList As String, ByVal StringToMofify As String, ByVal CLText As String, CLDescription As String, CLImg As String)
        'SDK.SetUserVar("StringToMofify", StringToMofify & " --> " & GiveLineNumberForString(CustomList, StringToMofify))

        Dim allLines = File.ReadAllLines(CustomList)

        allLines(GiveLineNumberForString(CustomList, StringToMofify)) = "LST" & CLText & "||" & CLDescription
        allLines(GiveLineNumberForString(CustomList, StringToMofify) + 1) = "ICO" & CLImg
        File.WriteAllLines(CustomList, allLines, Encoding.Unicode)

    End Sub


    Public Function GiveLineNumberForString(ByVal FileName As String, ByVal someString As String) As Integer
        Dim lines As String() = File.ReadAllLines(FileName)
        Dim found As Integer '= -1
        For i As Integer = 1 To lines.Length
            If lines(i).Contains(someString) Then
                found = i
                Exit For
            End If
        Next
        Return found
    End Function
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
ucfoutu Messages postés 18038 Date d'inscription lundi 7 décembre 2009 Statut Modérateur Dernière intervention 11 avril 2018 211
22 mai 2013 à 18:18
Ce ne sont pourtant pas les exemples d'utilisation de l'instruction Open (elle seule) qui manquent sur ce forum (qui dispose, je te le rappelle, d'un moteur de recherche !).
Entre les codes déposés et les discussions ouvertes à ce sujet ===>> ce n'est pas une foule d'exemples, qui s'y trouvent, mais une véritable nuée.


________________________
Réponse exacte ? => "REPONSE ACCEPTEE" facilitera les recherches.
Pas d'aide en ligne installée ? => ne comptez pas sur moi pour simplement répéter son contenu. Je n'interviendrai que si nécessité de la compléter.
0
pierrotm777 Messages postés 24 Date d'inscription vendredi 10 octobre 2008 Statut Membre Dernière intervention 27 mai 2013
22 mai 2013 à 18:19
Je dois préciser que mon fichier texte est au format UNICODE et doit le rester !
0
pierrotm777 Messages postés 24 Date d'inscription vendredi 10 octobre 2008 Statut Membre Dernière intervention 27 mai 2013
27 mai 2013 à 12:27
Bien, j'ai simplifié mon code mais un peu d'aide me ferait du bien:
Public Sub ModifyCustomList(ByVal CustomList As String, ByVal StringToMofify As String, ByVal CLText As String, CLDescription As String, CLImg As String)
    Dim strPath() As String
    Dim lngIndex As Long
    strPath() = Split(CLImg, "")
    lngIndex = UBound(strPath)
    CLImg = "$SKINPATH$include\Contacts\userpics" & strPath(lngIndex)
    
    Const ForReading 1: Const ForWriting 2
    Dim objFSO, objFile, allLines, arrLines, x, strLine, strNewContents
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile(CustomList, ForReading, True, -1)
    allLines = objFile.ReadAll
    arrLines = Split(allLines, vbCrLf)
    arrLines(3) = "LST" & CLText & "||" & CLDescription
    arrLines(3 + 1) = "ICO" & CLImg
    objFile.Close
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile(CustomList, ForWriting, True, -1)
    RRSDK.SetUserVar "LN", UBound(arrLines)
    For x = 0 To UBound(arrLines)
        objFile.Write (arrLines(x) & vbCrLf)
    Next
    objFile.Close
    
End Sub

Dans mon exemple, j'essaie d'effacer les lignes 3 et 4 mais les 2 nouvelles lignes
sont écrites entre les deux anciennes qui ne sont pas remplacées !
Quelle est mon erreur svp.
Je rappel que mon fichier doit rester au format UNICODE !!!

Merci de votre aide
0
Rejoignez-nous