Match two txt file and give result in new file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
DingDang
Posts: 26
Joined: 06 Jul 2012 11:04

Match two txt file and give result in new file

#1 Post by DingDang » 24 Oct 2012 00:05

Hi,

I have 2 Txt file in folder with same format, i want batch file to match both txt files line by line and give any mismatch cases in new seprate file with details of mismatch in below format

Mismatch details
file name 1 : 0215648897979 john rodrigs
file name 2 : 0215649987979 jonh bodrigs

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

Re: Match two txt file and give result in new file

#2 Post by foxidrive » 24 Oct 2012 01:39

You can't get colour in a text file. HTML would have to be used.

CSDiff is a difference compare tool that was recommended in alt.msdos.batch.nt

http://www.componentsoftware.com/products/csdiff/

DingDang
Posts: 26
Joined: 06 Jul 2012 11:04

Re: Match two txt file and give result in new file

#3 Post by DingDang » 24 Oct 2012 02:16

Sir,

Thanks for prompt reply, I dont want result in colour but in possible way like underline / Bold font etc. below is just example for understand the requirement.

have checked CSDiff utility its very usefull but I can't use it in my office PC as its not permissible to use outside utility for security reason.

IF possible pls provide batch file.

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

Re: Match two txt file and give result in new file

#4 Post by foxidrive » 24 Oct 2012 02:36

Text files can't use underline or bold fonts, or different fonts.

You'd need to use something like HTML.

DingDang
Posts: 26
Joined: 06 Jul 2012 11:04

Re: Match two txt file and give result in new file

#5 Post by DingDang » 24 Oct 2012 02:42

any alternative ??

is it possible to get only mismatch cases without any formating in txt file.

file name 1 : 0215648897979 john rodrigs
file name 2 : 0215649987979 jonh bodrigs

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

Re: Match two txt file and give result in new file

#6 Post by foxidrive » 24 Oct 2012 02:48

You can get a list of the mismatched lines.

Can you provide a sample of the file, to help develop some batch code? 10 lines would be enough.

We'd need to know if the text is purely alpha numeric or if it contains poison characters, too.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Match two txt file and give result in new file

#7 Post by Squashman » 24 Oct 2012 10:31

Windows has two native file compare utilities but you are not going to get the desired output from them. FC & COMP

Windiff is also in the Windows 2003 Server support tools. It officially runs on XP and 2003 Server but plenty of people on the web have said it works on Windows Vista and 7.
http://www.microsoft.com/en-us/download ... px?id=7911

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

Re: Match two txt file and give result in new file

#8 Post by foxidrive » 24 Oct 2012 20:43

This works here - beware of the usual poison characters.

Both files should contain the same number of lines.

Code: Select all

@echo off
set "file1=file1.txt"
set "file2=file2.txt"
del "result.compare.txt" 2>nul
set c=0
setlocal enabledelayedexpansion
<%file2% (for /f "delims=" %%a in ('type "%file1%"') do (
 set /a c=c+1
 set b=
 set /p b=
   
      if not "%%a"=="!b!" (
        echo line !c! has a mismatch
        >> "result.compare.txt" echo %file1% line !c!: %%a
        >> "result.compare.txt" echo %file2% line !c!: !b!
      )
)
)
if exist "result.compare.txt" (
echo check "result.compare.txt" for mismatches
) else (
echo no mismatches found
)

pause

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Match two txt file and give result in new file

#9 Post by dbenham » 25 Oct 2012 15:35

As others have pointed out, it is not possible to color code, bold, underline, etc. within a simple text file.

But you can use color to show the differences on the screen :!:

This is a bit slow, but it works. It uses the DosTips StrLen function. It also uses a variation of jeb's print in color routine. The filenames are passed to the script as the first two parameters (%1 and %2).

The files do not have to have the same number of lines, nor do the lines have to be the same length.

But keep in mind that 2 files could be identical except one file could have an extra line at the beginning. In this case the routine would report that all lines are different :!: :(

The console window must be wider than the largest line in either file.

showdiff.bat

Code: Select all

@echo off
setlocal enableDelayedExpansion
set file1=%1
set file2=%2
for /f %%N in ('find /c /v "" ^<%file1%') do set "cnt=%%N"
for /f %%N in ('find /c /v "" ^<%file2%') do if %%N gtr %cnt% set "cnt=%%N"
call :initColorPrint
8<"%file1%" 9<"%file2%" (
  for /l %%N in (1 1 %cnt%) do (
    set "ln1="
    set "ln2="
    <&8 set /p "ln1="
    <&9 set /p "ln2="
    if !ln1! neq !ln2! (
      call :strlen ln1 len
      call :strlen ln2 len2
      if !len2! gtr !len! set "len=!len2!"
      set "ln1=.!ln1!"
      set "ln2=.!ln2!"
      set chunk=0
      set "ln1.!chunk!="
      set "ln2.!chunk!="
      for /l %%A in (1 1 !len!) do (
        set "change="
        2>nul set /a "1/(chunk%%2)" && (
          if "!ln1:~%%A,1!" equ "!ln2:~%%A,1!" set change=1
        ) || (
          if "!ln1:~%%A,1!" neq "!ln2:~%%A,1!" set change=1
        )
        if defined change (
          set /a chunk+=1
          set "ln1.!chunk!="
          set "ln2.!chunk!="
        )
        for %%B in (!chunk!) do (
          set "ln1.%%B=!ln1.%%B!!ln1:~%%A,1!"
          set "ln2.%%B=!ln2.%%B!!ln2:~%%A,1!"
        )
      )
      if !chunk! gtr 0 (
        call :printLn 1
        call :printLn 2
        echo(
      )
    )
  )
)
call :cleanupColorPrint
exit /b

:printLn
call :colorPrint 0F "File%1: "
for /l %%N in (0 1 %chunk%) do if defined ln%1.%%N (
  2>nul set /a "1/(%%N%%2)" && (set color=0E) || set color=07
  call :colorPrintVar !color! ln%1.%%N
)
echo(
exit /b

:colorPrint Color  Str  [/n]
setlocal
set "str=%~2"
call :colorPrintVar %1 str %3
exit /b

:colorPrintVar  Color  StrVar  [/n]
if not defined %~2 exit /b
setlocal enableDelayedExpansion
set "str=a%DEL%!%~2:\=a%DEL%\..\%DEL%%DEL%%DEL%!"
set "str=!str:/=a%DEL%/..\%DEL%%DEL%%DEL%!"
set "str=!str:"=\"!"
pushd "%temp%"
findstr /p /A:%1 "." "!str!\..\x" nul
if /i "%~3"=="/n" echo(
exit /b

:initColorPrint
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do set "DEL=%%a"
<nul >"%temp%\x" set /p "=%DEL%%DEL%%DEL%%DEL%%DEL%%DEL%.%DEL%"
exit /b

:cleanupColorPrint
del "%temp%\x"
exit /b

:strLen string len -- returns the length of a string
::                 -- string [in]  - variable name containing the string being measured for length
::                 -- len    [out] - variable to be used to return the string length
:: Many thanks to 'sowgtsoi', but also 'jeb' and 'amel27' dostips forum users helped making this short and efficient
:$created 20081122 :$changed 20101116 :$categories StringOperation
:$source http://www.dostips.com
(   SETLOCAL ENABLEDELAYEDEXPANSION
    set "str=A!%~1!"&rem keep the A up front to ensure we get the length and not the upper bound
                     rem it also avoids trouble in case of empty string
    set "len=0"
    for /L %%A in (12,-1,0) do (
        set /a "len|=1<<%%A"
        for %%B in (!len!) do if "!str:~%%B,1!"=="" set /a "len&=~1<<%%A"
    )
)
( ENDLOCAL & REM RETURN VALUES
    IF "%~2" NEQ "" SET /a %~2=%len%
)
EXIT /b

test1.txt

Code: Select all

1 abcdefghijklmnopq

3 0123456789
4 abcdefghijklmnopq
5 ABCDEFGHIJKLMNOPQ
6 0123456789

test2.txt

Code: Select all

1 abcdefghijklmnopq
2 ABCDEFGHIJKLMNOPQ
3 0123A5678Z
x Abcdefghijklmnopq
5 ABCDEFGHIJKLMNOPQ

Simulation of Sample output:
D:\test>showdiff test1.txt test2.txt
File1:
File2: 2 ABCDEFGHIJKLMNOPQ

File1: 3 0123456789
File2: 3 0123A5678Z

File1: 4 abcdefghijklmnopq
File2: x Abcdefghijklmnopq

File1: 6 0123456789
File2:


Dave Benham

Post Reply