Help - Search - Members - Calendar
Full Version: Find word in a text file then find same word in another text file
MSFN Forums > Coding, Scripting and Servers > Programming (C++, Delphi, VB, etc.)

   
Google Internet Forums Unattended CD/DVD Guide
stevenwmn
Hi, I am new to VB and need some help with a vbscript. I have two text files that have similar data but the data is in different lines, what I want to do is look in one text file for a specific word and then search a second text file for that word and write the line from the second text file to a 3rd text file. The words in text file 1 and two are always in the same position in each line. Here is an example

Text file 1 contains
this line of text will contain a random value of 0001.4411.5500 in the same place in each line
this line of text will contain a random value of 5835.5468.1165 in the same place in each line
... (file keeps going)

Text file 2 contains
This line contains 0001.4411.5500 as a value in the same place in each line
This line contains 0351.5558.6556 as a value in the same place in each line
...(file keeps going)

So text file 3 would contain:
This line contains 0001.4411.5500 as a value in the same place in each line

I want to look for each random value of whatever is the 11th word in the first text file and see if that value exist in the 4th word of the 2nd text file, if it does, write the entire line from the 2nd text file to a 3rd text file. The string I am looking for is always the same length and always in the same position in each file.

Thank you for any help with this!
IcemanND
There are a number of ways to accomplish this but here's an outline of one of them.

Read Text file 2 into a dictionary object, the key would be you value in position 4, and the associated item the full line of text.

then read text file 1 and as you read it check to see if the value of position 11 exists in the dictionary, if it does add the associated dictionary item to text file 3.
stevenwmn
Any example of that would be greatly appreciated, I have a little VB experience but not enough to set up what you talked about.
IcemanND
This will not account for duplicate lines in File 2 but I if as you say all the lines are the same except for the lookup value then it should not make a difference.

CODE
Option Explicit

Const For_Reading = 1
Const For_Writing = 2
Const For_Appending = 8

Dim dictFile2
Dim File1
Dim File2
Dim File3
Dim objFSO
Dim strNextLine
Dim arrNextLine

set objFSO = CreateObject("Scripting.FileSystemObject")
Set dictFile2 = CreateObject("Scripting.Dictionary")

'change path and file name as needed
set File1 = objFSO.OpenTextFile("File1.txt",For_Reading)
set File2 = objFSO.OpenTextFile("File2.txt",For_Reading)
Set File3 = objFSO.CreateTextFile("File3.txt",For_Writing)

do until File2.AtEndofStream
    strNextLine = File2.Readline
    arrNextLine = split(strNextLine," ")
        'change (3) to zero based position of lookup value in file 2
    if not dictFile2.exists(arrNextline(3)) then
        dictFile2.add arrNextLine(3), strNextLine
    end if
loop

do until File1.AtEndofStream
    strNextLine=File1.Readline
    arrNextLine = split(strNextLine," ")
       'change (10) to zero based position of lookup value in file 1
    if dictFile2.exists(arrnextline(10)) then
                'change (10) to zero based position of lookup value in file 1
        file3.WriteLine dictFile2.item(arrNextLine(10))
    end if
loop

file1.close
file2.close
file3.close
set objFSO = nothing
set dictFile2 = nothing
set File1 = nothing
set File2 = nothing
set File3 = nothing
gunsmokingman
Here is another way of doing what you want

QUOTE
CODE
Option Explicit
Const ForReading = 1
Const ForWriting = 2
Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")
Dim C1, ChkTxt1, ChkTxt2, i, StrLine, StrValue, Ts, Txt1, Txt2, Txt3
  Txt1 = "Text1.txt"
  Txt2 = "Text2.txt"
  Txt3 = "Text3.txt"
  If Fso.FileExists(Txt1) Then
'/-> Process First Text File And Get The Number
   Set Ts = Fso.OpenTextFile(Txt1, ForReading)
    Do Until Ts.AtEndOfStream
     StrValue = Split(Ts.ReadLine, " ")
     For Each i In StrValue
      C1 = C1 + 1
      If C1 = 11 Then ChkTxt1 = i
     Next
    Loop
  Else
   WScript.Echo "Cannot Find " & Txt1
   WScript.Quit
  End If
  If Fso.FileExists(Txt2) Then
'/-> Process Second Text File And Compare
   Set Ts = Fso.OpenTextFile(Txt2, ForReading)
    Do Until Ts.AtEndOfStream
     StrValue = Split(Ts.ReadLine, " ")
     For Each i In StrValue
      If InStr(i,ChkTxt1) Then ChkTxt2 = i
     Next
    Loop
'/-> Write The Information To The 3rd Text File
  Set Ts = Fso.OpenTextFile(Txt3,ForWriting,True)
   Ts.WriteLine ChkTxt2
   Ts.Close
  Else
   WScript.Echo "Cannot Find " & Txt2
   WScript.Quit
  End If
Google Internet Forums Unattended CD/DVD Guide
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.