Vbscript to read a specific line in a text file.

Vicki05
Vicki05 used Ask the Experts™
on
Hi Friends,

I am looking for a vbscript that will read a specific line in a file and then copy file 1 or file 2 to replace it depending on the statement on that line? I need to replace the file that is being read with a new one with the same statement on that line. Can someone please help?

Thanks,
Vicki
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
So there are three files involved, right?

And you want to read file1, look at a specific line (is that located by line number, or some other way?).

And then depending on some or all of the content on that line in file1, you want to replace that line with something from either file2 or file3?

What gets selected from file2 / file3, is it the same line number, the whole file, etc?

And do you want to overwrite the original file1 when done, or create a new file4?

Can you please provide samples of the three files, and the expected result of the script.


»bp

Author

Commented:
Hi Bill,

I need to replace a text file and need to see what statement is on that line. If it has this is a cat, then I need to replace it with a file that is on the CD with that same statement. If it says this is a dog, then I replace it from the CD as well. I just need to overwrite the files and not to create a new one. The file name in all the case would be the same. I would copy it from different folder from the CD depending what is the statement.

If line statement is This is a cat, then copy file 1 from folder 1 or if the statement is this is a dog, then copy file from folder 2 and replace it on the local drive. Make sure there is no read only permission.


The statement that I am check would be on line 14
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
Okay, if I understand you correctly this should do it.  Take a look and see if it makes sense, and then give it a test, seemed to work as expected here.

' Constants for filesystem object
Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2
 
' Input file to be checked and possibly replaced
strFile = "B:\EE\EE29071385\myfile.txt"

' Line number in file to check
intLine = 14

' Search array, with phrase to match on line and file to copy when match found
arrReplace = Array(Array("this is a cat", "B:\EE\EE29071385\cat\myfile.txt"), _
                   Array("this is a dog", "B:\EE\EE29071385\dog\myfile.txt"))

' Creaet filesystem object
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Read input file into memory
Set objFile = objFSO.OpenTextFile(strFile, ForReading, False, TriStateUseDefault)
strData = objFile.ReadAll
objFile.Close

' Split into temp array at line breaks
arrData = Split(strData, vbCrLf)

' If less lines than the one we need to check, exit now
If UBound(arrData) < (intLine-1) Then
    Wscript.Quit
End If

' Check each search phrase and if match found copy corresponding file
For Each arrMatch In arrReplace
    If LCase(arrMatch(0)) = LCase(arrData(intLine-1)) Then
        objFSO.CopyFile arrMatch(1), strFile
    End If
Next

Open in new window


»bp
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Author

Commented:
Hi Bill,

It does not seem to be working. The file is not getting replaced. I do see any changes to the file. Please advice?
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
Please post your updated VBS script, along with the input file you are testing with.


»bp

Author

Commented:
Hi Bill,

Here is the files zipped. It works with the file name that you gave. As soon as I use my file, it does not work. It is a 7z file, I changed the extension to txt to upload
Test2.txt
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
Right, the current logic matches the entire line, not just a subset of it.  Did you want to match if a line contains the search text, rather than if a line equals the search text?


»bp

Author

Commented:
I am not really understanding what you are asking, but If you think that will be the solution? Also the same statement is in the script below. I need the script to look at that line only and decide and to replace with the file that has true or false statement depending.
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
I'm concerned we may not be on the same page.

My current script, based on what was in the question to that point, does the following.  If this isn't what you wanted please clarify carefully what needs to be different.

  • Reads entire file1 and looks for a line that completely matches one of two search phrases
  • If search phrase1 matches a line in file 1, then the complete file2 is copied over file1
  • If search phrase2 matches a line in file 1, then the complete file3 is copied over file1


»bp

Author

Commented:
The file is the same, it is only that statement. Below in the script it has that statement repeated. If the statement is true then do this and if it false do that.  I do need the file replaced depending on the statement that is on line 14 or 15. You are correct. I need file 1 replaced depending on the statement by file 2 or 3.
Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
You still haven't answered if you want to match the whole line with the search phrases, or just see if the search phrases are contained within the line?

Your test didn't work because your search terms were:

"xxxxxxxxxxxxxxxxxx = True"
"xxxxxxxxxxxxxxxxxx = False"

but the line itself is:

vbTab & "xxxxxxxxxxxxxxxxxx = True"

since I am doing a full line match, and since the existing line has a <TAB> character in the first position, no match was found.

So either the search terms have to be changed to include a <TAB>, or we adjust the VBS to do a partial content match of the search phrase in the existing line.  Which would you like?


»bp

Author

Commented:
Searched phrases are contained within the line please.
Test your restores, not your backups...
Top Expert 2016
Commented:
Okay, give this a try please.

' Constants for filesystem object
Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2
 
' Input file to be checked and possibly replaced
strFile = "C:\Test2\Test1\Input Folder\File Currently Used\Testfile for Check_V1.sct"

' Line number in file to check
intLine = 15

' Search array, with phrase to match on line and file to copy when match found
arrReplace = Array(Array("xxxxxxxxxxxxxxxxxx = True", "C:\Test2\Test1\Input Folder\True\Testfile for Check_V1.sct"), _
                   Array("xxxxxxxxxxxxxxxxxx = False", "C:\Test2\Test1\Input Folder\False\Testfile for Check_V1.sct"))
						

' Creaet filesystem object
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Read input file into memory
Set objFile = objFSO.OpenTextFile(strFile, ForReading, False, TriStateUseDefault)
strData = objFile.ReadAll
objFile.Close

' Split into temp array at line breaks
arrData = Split(strData, vbCrLf)

' If less lines than the one we need to check, exit now
If UBound(arrData) < (intLine-1) Then
    Wscript.Quit
End If

' Check each search phrase and if match found copy corresponding file
For Each arrMatch In arrReplace
    If InStr(1, arrData(intLine-1), arrMatch(0), vbTextCompare) > 0 Then
        objFSO.CopyFile arrMatch(1), strFile
    End If
Next

MsgBox "Done"

Open in new window


»bp

Author

Commented:
Thanks Bill,

I will test and let you know if I run into any issues.

Author

Commented:
Thanks Bill,

Your solution worked like a charm. Lets hope when it works when its burnt to a CD.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial