jeudi 9 août 2018

How do I check for a duplicated text name and replace it with a new value if found using vbscript?

    Set myFile = objFSO.OpenTextFile("savedhosts.txt", ForReading, True)
    Set myTemp = objFSO.OpenTextFile("savedhosts.txt" & ".tmp", ForWriting, True)
    Do Until myFile.AtEndofStream
        myLine = myFile.ReadLine
        'msgbox "hostname = " & hostname
        If InStr(myLine, hostname) Then
            MsgBox "found hostname = " & hostname & " in the savedhosts.txt file"
            myLine = hostname & ";" & hostname_IP_value
            myTemp.WriteLine myLine
            myFile.Close
            myTemp.Close
            objFSO.DeleteFile("savedhosts.txt")
            objFSO.MoveFile "savedhosts.txt" & ".tmp", "savedhosts.txt" 
        Exit Sub

        Else
            Set objTextFile = objFSO.OpenTextFile("savedhosts.txt", ForAppending, True)
            'MsgBox "hostname & ; & hostname_IP_value = " & hostname & ";" & hostname_IP_value
            objTextFile.WriteLine(hostname_value & ";" & hostname_IP_value)
            objTextFile.Close
            document.getElementById("hostname").value=hostname
        Exit Sub    
    End If
    Loop

Even when inputting identical hostname values the script doesn't capture the condition to display the

MsgBox "found hostname = " & hostname & " in the savedhosts.txt file"

Instead it just appends it to the txt file, resulting in 2 text names with different values, which is not desirable!

Aucun commentaire:

Enregistrer un commentaire