Solved

comparing two files only looping once

Posted on 2010-11-29
4
464 Views
Last Modified: 2012-05-10
Hi, Im trying to compare two text files. Im taking the first line from file A and checking each line in file B, then trying to take the 2nd line from file A and check all lines in file B. The problem im having is that once i go through all lines in file B, i cant restart the count - my loop only goes through file B once. Is there a way in which i can set it to start reading in the file from the begining each time?
thanks
Function CheckFileDetails()

Dim fso, objFile_B, objFile_A, FileA, SavedFilesFolder, intRows 
Dim FileAContents, arrFileAContents, strLine, linecount 

'Set Variables 
linecount = 0
strFolder = "C:\Test_Auto\" 
objFile_B = "FileB.txt"
objFile_A = "FileA.txt"
 
'Create Objects 
Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objShell = CreateObject("Shell.Application") 
 
'Set variables
	billerFound = False

Set objFSO = CreateObject("Scripting.FileSystemObject") 
Const intForReading = 1 
'Read in the contents of the biller master file  and new billers file
Set objFile_B = objFSO.OpenTextFile(strFolder & objFile_B, intForReading) 
Set objFile_A = objFSO.OpenTextFile(strFolder & objFile_A, intForReading) 

'Get todays date
today = Date
TodayArray = Split(today, "#", -1, 1)
today = TodayArray(0)

'Move through the new biller temp file and check if any of the activation dates are before todays date (if so check the BMF)
While Not (objFile_A.AtEndOfStream) 
Dim NewBillerArray
	strLine = objFile_A.ReadLine 
'	call SplitUpNewBillerFile(strLine)
	NewBillerArray = Split(strLine, ",", -1, 1)

'check the activation date is before or on todays date
If (NewBillerArray(0) <= today) Then

'  if NewBillerArray(0) exists in strLine
   strLineBMF = objFile_B.ReadLine

	'searching the BMF  for Long Name - if that exists, then check that entire line and compare against New Biller file
		While Not ((objFile_B.AtEndOfStream) OR (billerFound = True))
		   strLineBMF = objFile_B.ReadLine 'read in line again on next loop

	If InStr(strLineB, NewBillerArray(3)) Then

'do something


	End If
        Wend 
End If

Wend


'Close both files that we're comparing
objFile_B.Close
objFile_A.Close

End Function

Open in new window

0
Comment
Question by:DiCanio13
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 34236701
How about reading both files into arrays and then processing them completely in memory ... or at least the second file and then cycle through that array?

Chris
0
 
LVL 13

Accepted Solution

by:
themrrobert earned 300 total points
ID: 34236713
You absolutely should compare the files in memory.


'Get todays date
today = Date
TodayArray = Split(today, "#", -1, 1)
today = TodayArray(0)

'Set variables
	billerFound = False

Set objFSO = CreateObject("Scripting.FileSystemObject") 
Const intForReading = 1 
'Read in the contents of the biller master file  and new billers file
Set objFile_B = objFSO.OpenTextFile(strFolder & objFile_B, intForReading) 
Set objFile_A = objFSO.OpenTextFile(strFolder & objFile_A, intForReading) 
Dim strFile_A as String
Dim strFile_B as String

'Move through the new biller temp file and check if any of the activation dates are before todays date (if so check the BMF)
While Not (objFile_A.AtEndOfStream) 
	strFile_A = strFile_A & objFile_A.ReadLine & vbCrLf
Wend
While Not (objFile_B.AtEndOfStream) 
	strFile_B = strFile_B & objFile_B.ReadLine & vbCrLf
Wend
'Close both files that we're comparing
objFile_B.Close
objFile_A.Close

'Now compare with the temp 'files' which are large strings containing the entire file.

Open in new window

0
 
LVL 59

Assisted Solution

by:Chris Bottomley
Chris Bottomley earned 200 total points
ID: 34236791
Without wishing to try and edit your whole script ... the following takes a single file and creates an array for processing:

DO it twice one after the other and you will have two arrays with the data and the files closed and released before you even start data processing.

Chris
Set objFSO = CreateObject("scripting.filesystemobject")
   Set objStream =
objFSO.getfile(strFolder & objFile_B).openastextstream
   arr_B = split(objStream.ReadAll, vbcrlf)
   objstream.close
' Repeat for A
'...
	for each entity in arr_A
        	for each entity in arr_B
     	        	'Compare lines in B with Line in A
        	Next
	Next

Open in new window

0
 

Author Closing Comment

by:DiCanio13
ID: 34237032
thanks guys, great help as usual
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question