Batch - Comparing two txt files

400 Views Asked by At

I have some difficulties comparing two txt files with batch. I used the "findstr" function with many option matchings but none works (for example FINDSTR /I /V /B /G:file1.txt file2.txt). I have a first txt file as following:

File1.txt

Object 1
Argument 50
Object 2
Argument 10
Object 3
Argument 10
Object 4
Argument 10

The second file has the same beginning as the first one:

File2.txt

Object 1
Argument 50
Object 2
Argument 10
Object 3
Argument 10
Object 4
Argument 10
Object 5
Argument 10
Object 6
Argument 50
Object 7
Argument 10
Object 8
Argument 10

The purpose of this comparaison is to separate the parts which are repeated (on the same lines only) in the second file. The results have to be as following:

File1.txt (unchanged)

Object 1    
Argument 50        
Object 2
Argument 10
Object 3
Argument 10
Object 4
Argument 10

and

File2.txt

Object 5
Argument 10
Object 6
Argument 50
Object 7
Argument 10
Object 8
Argument 10

A For loop may perhaps be usefull... Thanks a lot for your precious help!

2

There are 2 best solutions below

0
On BEST ANSWER

The code below keep lines from File2.txt from the first line that differ vs. File1.txt on:

@echo off
setlocal EnableDelayedExpansion

rem Redirect the *larger* file as input
< File2.txt (

   rem Merge it with lines from shorter file
   for /F "delims=" %%a in (file1.txt) do (

      rem Read the next line from File2
      set /P "line="

      rem If it is different than next line from File1...
      if "!line!" neq "%%a" (

         rem Show this line (the first different one)
         echo !line!
         rem Show the rest of lines from File2
         findstr "^"
         rem And terminate
         goto break

      )
   )

   rem If all lines in File1 were equal to File2, show the rest of File2
   findstr "^"

) > File2_new.txt

:break
move /Y File2_new.txt File2.txt
2
On

If the comparision between the two files is necessary

@echo off
    setlocal enableextensions disabledelayedexpansion

    set "file1=.\file1.txt"
    set "file2=.\file2.txt"

    rem If it is necessary to COMPARE the same lines of the two files
    for %%t in ("%temp%\%~nx0.%random%%random%.tmp") do (
        findstr /n "^" "%file2%" > "%%~ft.2"
        findstr /n "^" "%file1%" > "%%~ft.1"
        findstr /v /l /b /g:"%%~ft.1" "%%~ft.2" > "%%~ft"
        (for /f "usebackq tokens=* delims=0123456789" %%a in ("%%~ft") do (
            set "line=%%a"
            setlocal enabledelayedexpansion
            echo(!line:~1!
            endlocal
        )) > "%file2%.new"
        del /q "%%~ft*"
    )
    type "%file2%.new"

Or, if file2 is always file1 plus more lines

@echo off
    setlocal enableextensions disabledelayedexpansion

    set "file1=.\file1.txt"
    set "file2=.\file2.txt"

    rem If the only need is to skip the start of the second file 
    rem AND it has less than 65535 lines
    for /f %%a in ('find /v /c "" ^< "%file1%"') do set "nLines=%%a"
    more +%nLines% < "%file2%" > "%file2%.new"
    type "%file2%.new"