How do I compare 2 files ?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
win_2020
Posts: 1
Joined: 10 Feb 2013 08:06

How do I compare 2 files ?

#1 Post by win_2020 » 10 Feb 2013 08:11

Hi..experts..

I want to compare data file with reference file using following logic...please help me

1 . First read column 1 and column 2 of file1.txt store it in array(reference)

2 . Now read column 1 and column 2 of file2.txt store it in another
array,to be compared with reference

3 . for loop : - for(i=1;i<=file2.txt_length;i++)

4 . x=0; y=0 this will get clear as and when i will increment

5 . nested loop :- for(j=1;j<=file1.txt_lenght;j++)

6 . if(col1_f1[j]>=col1_f2-0.1[i] && col1_f1[j]<=col1_f2+0.1[i])

if above if statement is true then,go to following statement else
check condition with j++

7 . if(col2_f2[i]>=col2_f1[j]) then x=x+1

8 . if(col2_f2[i]<=col2_f1[j]) then y=y+1

repeat 5,6,7 and 8 till j reaches file1.txt_length(no of rows),Once j
reaches NR following

9 . if(x>=1 && y>=1) then print column1_f2[i] column2_f2[i]

repeat statements after number 4 to 9 till i reaches
file2.txt_length(no of rows)

whether it's possibe ?

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: How do I compare 2 files ?

#2 Post by abc0502 » 10 Feb 2013 08:24

Comparing a whole column with another, I'm not sure but may be someone knows, but comparing a whole line with another one is possible.
This is a snippet i have from some time, it take to files as an input and compare every line in each file,
And you control the what happen if there is a match or not using an If command.

Code: Select all

(
   For /F "delims=" %%a in (C:\file1.txt) Do (
      Setlocal EnableDelayedExpansion
         Set /p line=
         if "%%a" NEQ "!line!" Echo !line!
      Endlocal
   )
)<"C:\file2.txt"
In the if command, it will display any un-equal lines if it find it.
You don't have to provide any thing else except the files locations, just make sure the 1st file name doesn't contain space in them, or a small modification will be needed.

if you can explain more, that will be helpful . :)
And try to post a sample of your columns if possible to test the code on it before posting.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How do I compare 2 files ?

#3 Post by foxidrive » 10 Feb 2013 12:07

win_2020 wrote:Hi..experts..

I want to compare data file with reference file using following logic...please help me


Use plain English to describe your task and supply some sample data to work with.

You see telling us how to do it in programming terms from C or something is pointless if the batch methods are not the same. Or you could write it yourself in C.

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: How do I compare 2 files ?

#4 Post by Aacini » 10 Feb 2013 18:41

Although your question is long and detailed, it missed to much information! I suggest you to write pure C code or use plain English. I assumed the following:

There are file1.txt and file2.txt files, each one have lines with two numbers separated by spaces and with ONE decimal digit each.

Code: Select all

@echo off
setlocal EnableDelayedExpansion

rem 1 . First read column 1 and column 2 of file1.txt store it in array(reference)
set i=0
for /F "tokens=1,2" %%a in (file1.txt) do (
   set /A i+=1
   set col1_f1[!i!]=%%a
   set col2_f1[!i!]=%%b
   REM APA: Batch just manage integer numbers, so we eliminate the decimal point
   for %%i in (!i!) do (
      set col1_f1[%%i]=!col1_f1[%%i]:.=!
      set col2_f1[%%i]=!col2_f1[%%i]:.=!
   )
)
set file1.txt_length=%i%

rem 2 . Now read column 1 and column 2 of file2.txt store it in another array,to be compared with reference
set i=0
for /F "tokens=1,2" %%a in (file2.txt) do (
   set /A i+=1
   set col1_f2[!i!]=%%a
   set col2_f2[!i!]=%%b
   REM APA: Batch just manage integer numbers, so we eliminate the decimal point
   for %%i in (!i!) do (
      set col1_f2[%%i]=!col1_f2[%%i]:.=!
      set col2_f2[%%i]=!col2_f2[%%i]:.=!
   )
)
set file2.txt_length=%i%

rem 3 . for loop : - for(i=1;i<=file2.txt_length;i++)
for /L %%i in (1,1,%file2.txt_length%) do (

   rem 4 . x=0; y=0 this will get clear as and when i will increment
   set /A x=0, y=0

   rem 5 . nested loop :- for(j=1;j<=file1.txt_lenght;j++)
   for /L %%j in (1,1,%file1.txt_lenght%) do (

      rem 6 . if(col1_f1[j]>=col1_f2-0.1[i] && col1_f1[j]<=col1_f2+0.1[i])
      set /A col1_f2MINUS0.1=col1_f2[%%i]-1, col1_f2PLUS0.1=col1_f2[%%i]+1
      if !col1_f1[%%j]! geq !col1_f2MINUS0.1! if !col1_f1[%%j]! leq !col1_f2PLUS0.1! (

         rem if above if statement is true then,go to following statement

         rem 7 . if(col2_f2[i]>=col2_f1[j]) then x=x+1
         if !col2_f2[%%i]! geq !col2_f1[%%j]! set /A x=x+1

         rem 8 . if(col2_f2[i]<=col2_f1[j]) then y=y+1
         if !col2_f2[%%i]! leq !col2_f1[%%j]! set /A y=y+1

      REM APA: Warning! Batch IF conditions does NOT have AND/OR operators.
      REM      Long command with two IF's above are CHAINED to simulate an "AND".
      REM      If you insert an ELSE here, it belongs to SECOND IF ONLY, NOT to the "and".

      )

   )

   rem repeat 5,6,7 and 8 till j reaches file1.txt_length(no of rows),Once j reaches NR following

   rem 9 . if(x>=1 && y>=1) then
   if !x! geq 1 if !y! geq 1 (

      rem print column1_f2[i] column2_f2[i]

      REM APA: Next %%i is local to this FOR, different of other FOR's with same parameter
      for /L %%i in (1,1,%file2.txt_length%) do (

         REM APA: Insert deleted decimal points in its place (ONE decimal digit)
         echo !col1_f2[%%i]:~0,-1!.!col1_f2[%%i]:~-1!    !col2_f2[%%i]:~0,-1!.!col2_f2[%%i]:~-1!

      )

   REM APA: Same comment about AND operator and bad use of ELSE here

   )

   rem repeat statements after number 4 to 9 till i reaches file2.txt_length(no of rows)

)


Antonio

Post Reply