stevenwmn Posted June 19, 2008 Posted June 19, 2008 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 containsthis line of text will contain a random value of 0001.4411.5500 in the same place in each linethis 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 containsThis line contains 0001.4411.5500 as a value in the same place in each lineThis 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 lineI 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 Posted June 19, 2008 Posted June 19, 2008 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 Posted June 19, 2008 Author Posted June 19, 2008 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 Posted June 19, 2008 Posted June 19, 2008 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.Option ExplicitConst For_Reading = 1Const For_Writing = 2Const For_Appending = 8Dim dictFile2 Dim File1 Dim File2Dim File3Dim objFSODim strNextLineDim arrNextLineset objFSO = CreateObject("Scripting.FileSystemObject")Set dictFile2 = CreateObject("Scripting.Dictionary")'change path and file name as neededset 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 ifloopdo 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 ifloopfile1.closefile2.closefile3.closeset objFSO = nothingset dictFile2 = nothingset File1 = nothingset File2 = nothingset File3 = nothing
gunsmokingman Posted June 20, 2008 Posted June 20, 2008 Here is another way of doing what you wantOption 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
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now