Avatar of Vicki05
Vicki05
Flag for United States of America asked on

Vbscript to read a specific line in a text file.

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
VB Script

Avatar of undefined
Last Comment
Vicki05

8/22/2022 - Mon
Bill Prew

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
Vicki05

ASKER
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 Prew

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
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Vicki05

ASKER
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 Prew

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


»bp
Vicki05

ASKER
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
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Bill Prew

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
Vicki05

ASKER
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 Prew

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
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Vicki05

ASKER
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 Prew

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
Vicki05

ASKER
Searched phrases are contained within the line please.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Bill Prew

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Vicki05

ASKER
Thanks Bill,

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

ASKER
Thanks Bill,

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