Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Compare 2 text files
#1
Hi,

I have a list box with values. 1, 2, 3.......... (same order) as baseline.
At run time I see values 2, 1, 3...........

How Can I compare these values.
I need code with a loop which can verify the 1st value in the list (at run time) compared against the baseline values which are in
1) a excel spreadsheet in a row.
2) a excel spreadsheet in a column.
Reply
#2
Reply
#3
Hi
Here is the code for compare two files.
------------------------------------
Code:
dim oFSO ' creating the file system object set oFSO = CreateObject ("Scripting.FileSystemObject") FilePath1 = "C:\text1.txt" FilePath2 = "C:\text2.txt" FilePathDiff = "C:\text3.txt" call FileCompare(FilePath1,FilePath2,FilePathDiff,false) 'Option Explicit ' ********************************************************************************************* ' Create a new txt file ' Parameters: ' FilePath - location of the file and its name ' ********************************************************************************************* Function CreateFile (FilePath) ' varibale that will hold the new file object dim NewFile ' create the new text ile set NewFile = oFSO.CreateTextFile(FilePath, True) set CreateFile = NewFile End Function ' ********************************************************************************************* ' Check if a specific file exist ' Parameters: ' FilePath - location of the file and its name ' ********************************************************************************************* Function CheckFileExists (FilePath) ' check if file exist CheckFileExists = oFSO.FileExists(FilePath) End Function ' ********************************************************************************************* ' Write data to file ' Parameters: ' FileRef - reference to the file ' str - data to be written to the file ' ********************************************************************************************* Function WriteToFile (byref FileRef,str) ' write str to the text file FileRef.WriteLine(str) End Function ' ********************************************************************************************* ' Read line from file ' Parameters: ' FileRef - reference to the file ' ********************************************************************************************* Function ReadLineFromFile (byref FileRef) ' read line from text file ReadLineFromFile = FileRef.ReadLine End Function ' ********************************************************************************************* ' Closes an open file. ' Parameters: ' FileRef - reference to the file ' ********************************************************************************************* Function CloseFile (byref FileRef) FileRef.close End Function '********************************************************************************************* ' Opens a specified file and returns an object that can be used to ' read from, write to, or append to the file. ' Parameters: ' FilePath - location of the file and its name ' mode options are: ' ForReading - 1 ' ForWriting - 2 ' ForAppending - 8 ' ********************************************************************************************* Function OpenFile (FilePath,mode) ' open the txt file and retunr the File object set OpenFile = oFSO.OpenTextFile(FilePath, mode, True) End Function ' ********************************************************************************************* ' Closes an open file. ' Parameters: ' FilePathSource - location of the source file and its name ' FilePathDest - location of the destination file and its name ' ********************************************************************************************* Sub FileCopy ( FilePathSource,FilePathDest) ' copy source file to destination file oFSO.CopyFile FilePathSource, FilePathDest End Sub ' ********************************************************************************************* ' Delete a file. ' Parameters: ' FilePath - location of the file to be deleted ' ********************************************************************************************* Sub FileDelete ( FilePath) ' copy source file to destination file oFSO.DeleteFile ( FilePath) End Sub ' ********************************************************************************************* ' Compare two text files. ' ' Parameters: ' FilePath1 - location of the first file to be compared ' FilePath2 - location of the second file to be compared ' FilePathDiff - location of the diffrences file ' ignoreWhiteSpace - controls whether or not to ignore differences in whitespace characters ' true - ignore differences in whitespace ' false - do not ignore difference in whitespace ' Return Value: true if files are identical, false otherwise' ' ********************************************************************************************* Function FileCompare (byref FilePath1, byref FilePath2, byref FilePathDiff, ignoreWhiteSpace) dim differentFiles differentFiles = false dim f1, f2, f_diff ' open the files set f1 = OpenFile(FilePath1,1) set f2 = OpenFile(FilePath2,1) set f_diff = OpenFile(FilePathDiff,8) dim rowCountF1, rowCountF2 rowCountF1 = 0 rowCountF2 = 0 dim str ' count how many lines there are in first file While not f1.AtEndOfStream str = ReadLineFromFile(f1) rowCountF1= rowCountF1 + 1 Wend ' count how many lines there are in second file While not f2.AtEndOfStream str = ReadLineFromFile(f2) rowCountF2= rowCountF2 + 1 Wend ' re-open the files to go back to the first line in the files set f1 = OpenFile(FilePath1,1) set f2 = OpenFile(FilePath2,1) ' compare the number of lines in the two files. ' assign biggerFile - the file that contain more lines ' assign smallerFile - the file that contain less lines dim biggerFile, smallerFile set biggerFile = f1 set smallerFile = f2 If ( rowCountF1 < rowCountF2) Then set smallerFile = f1 set biggerFile = f2 End If dim lineNum,str1, str2 lineNum = 1 str = "Line" & vbTab & "File1" & vbTab & vbTab & "File2" WriteToFile f_diff,str ' loop on all the lines in the samller file While not smallerFile.AtEndOfStream ' read line from both files str1 = ReadLineFromFile(f1) str2 = ReadLineFromFile(f2) ' check if we need to ignore white spaces, if yes, trim the two lines If Not ignoreWhiteSpace Then Trim(str1) Trim(str2) End If ' if there is a diffrence between the two lines, write them to the diffrences file If not (str1 = str2) Then differentFiles = true str = lineNum & vbTab & str1 & vbTab & vbTab & str2 WriteToFile f_diff,str End If lineNum = lineNum + 1 Wend ' loop on the bigger lines, to write its line two the diffrences file While not biggerFile.AtEndOfStream str1 = ReadLineFromFile(biggerFile) str = lineNum & vbTab & "" & vbTab & vbTab & str2 WriteToFile f_diff,str lineNum = lineNum + 1 Wend FileCompare = Not differentFiles End function
----------------------------
I hopes you may get the solucation with above code.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need to Validate Text filed is blank after clicking on the Text box balak89 3 5,919 09-13-2015, 12:06 AM
Last Post: ADITI1992
  Compare WebTable Elements saraiado 1 2,984 06-11-2015, 06:54 PM
Last Post: venkatesh9032
  How to compare two binary values Naresh 0 2,692 09-09-2014, 05:06 PM
Last Post: Naresh
  Capture Value through Text Checkpoint and compare with datatable value hamzaz 2 5,491 03-06-2014, 12:52 AM
Last Post: mallika.g
  QTP Should open files(say txt files) it can be n number from a folder. Shiv Y 1 3,253 12-18-2013, 01:45 AM
Last Post: mlkrqtp

Forum Jump:


Users browsing this thread: 1 Guest(s)