If you want to edit a specific line in a file through VBScript you can use something like below. This reads the original file line by line and writes it out to a temporary file. If the original line meets the condition you want, make the changes required. At the end, close both files, delete the original and move the temp file.
Const ForReading=1
Const ForWriting=2
Set objFSO = CreateObject("Scripting.FileSystemObject")
folder = "C:\Program Files\Vendor\"
filePath = folder & "file.txt"
Set myFile = objFSO.OpenTextFile(filePath, ForReading, True)
Set myTemp= objFSO.OpenTextFile(filePath & ".tmp", ForWriting, True)
Do While Not myFile.AtEndofStream
myLine = myFile.ReadLine
If InStr(myLine, "Variable=") Then
myLine = "Variable="&whatever
End If
myTemp.WriteLine myLine
Loop
myFile.Close
myTemp.Close
objFSO.DeleteFile(filePath)
objFSO.MoveFile filePath&".tmp", filePath
Thanks for the post. Just what I was after for a little project I am working on at work.
No problem, that’s the whole purpose of this blog.
Can you explain from where objFSO and dmqFile appeared?
And why the loop?
Thanks
Sorry my noobie questions
Sure. I’ve updated the original post and basically objFSO is required to access the file system and dmqFile was a variable name from my original script that I didn’t rename – my mistake.
The loop exists in order to check each line in turn. Depending on what you’re trying to do you could read the entire file into a variable and then interrogate it, or read the file one character at a time, but reading a file line by line is a fairly standard method, is quite quick and memory efficient.
Thanks,
helped me out with a dodgy file access script I was kludging together!