Reading script and replacing specific line. vbscript

Viewed 75

I'm developing a script that encrypts a users Tacacs-password, and writes this string into another script. My script opens, reads & writes the Tacacs-password into my other script but it doensn't overwrite it.

First run:

strTacacs = "Test1234"

Second run:

strTacacs = "Test1234"strTacacs = "Test1234"

My current script:

'***********Write to auto-logon script************

Const ForReading = 1
Const ForWriting = 2
newline = "strTacacs = " & chr(34) & Tacacs & chr(34)
line = 30


Set objFSO = CreateObject("Scripting.FileSystemObject")

Dim lineCount : lineCount = 0
Dim firstContent : firstContent = ""

Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files
For Each objFile in colFiles
    If LCase(objFSO.GetExtensionName(objFile)) = "vbs" Then
        lineCount = 0
        firstContent = ""
        FileName = objStartFolder & objFile.Name
        Set objStream = objFSO.OpenTextFile(strFile, ForReading)
        Do Until objStream.AtEndOfStream
            lineCount = lineCount + 1
            firstContent = firstContent & objStream.ReadLine & vbCrLf
            'msgbox(firstContent)
            if lineCount = 30 Then 
                firstContent = firstContent & newline
                msgbox(firstContent)
            End if
        Loop  
        Set objStream = objFSO.OpenTextFile(FileName, ForWriting)
        objStream.WriteLine firstContent
        objStream.Close 
    End If  
Next

.

Does anybody know what I'm doing wrong? I'm new in the world of scripting so your help is greatly appreciated!

Thx!

1 Answers

It looks like you are writing both the original line and the new line when you get to line 30. You should only write newline when lineCount is 30 and write the original line otherwise:

Do Until objStream.AtEndOfStream
    
    lineCount = lineCount + 1
    
    If lineCount = 30 Then
        ' Replace line with newline
        firstContent = firstContent & newline & vbCrLf
    Else
        ' Write original line
        firstContent = firstContent & objStream.ReadLine & vbCrLf
    End If
Loop

If you know the password in the original file, you could read the whole file content in one shot using ReadAll method:

Set objStream = objFSO.OpenTextFile(strFile, ForReading)
firstContent = objStream.ReadAll

then use Replace to replace the password, and finally write the content back.

You current line-by-line approach is easier if you don't know the password and simply replace a specific line. You could also check if line starts with strTacacs = which gets you away from hardcoding the line number:

Dim sLine
Dim sPasswordLine
sPasswordLine = "strTacas ="

Do Until objStream.AtEndOfStream

    ' Read line
    sLine = objStream.ReadLine

    If Left(sLine, Len(sPasswordLine)) = sPasswordLine Then
        ' Replace line with newline
        firstContent = firstContent & newline & vbCrLf
    Else
        ' Write original line
        firstContent = firstContent & sLine & vbCrLf
    End If
    
Loop
Related