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
Vicki05Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewCommented:
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
0
Vicki05Author 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
0
Bill PrewCommented:
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
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Vicki05Author 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?
0
Bill PrewCommented:
Please post your updated VBS script, along with the input file you are testing with.


»bp
0
Vicki05Author 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
0
Bill PrewCommented:
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
0
Vicki05Author 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.
0
Bill PrewCommented:
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
0
Vicki05Author 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.
0
Bill PrewCommented:
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
0
Vicki05Author Commented:
Searched phrases are contained within the line please.
0
Bill PrewCommented:
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
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Vicki05Author Commented:
Thanks Bill,

I will test and let you know if I run into any issues.
0
Vicki05Author Commented:
Thanks Bill,

Your solution worked like a charm. Lets hope when it works when its burnt to a CD.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.