varunesh varun
varunesh varun

Reputation: 3

Batch file needed for comparing two files

This is my requirement in windows batch file I tried the following

Example:
f1.txt
sam
varun
ramesh
babu

f2.txt
babu
sam

I need the output of

varun
ramesh

The program

@echo on
SETLOCAL EnableDelayedExpansion
for /F "tokens=* delims=." %%a in (f1.txt) do (
    call :myInnerLoop "%%a"
)

echo out of inner loop
)
goto :eof


:myInnerLoop
for /F "tokens=* delims=." %%b in (f2.txt) do (
    if "%~1"=="%%b" (
    echo inside inner loop
        goto :next
    ) else ( 
        echo %%a >> "E:\test\diff.txt"
    )
:next
goto :eof

But it is not working kindly help me.

Even I tried diff utility also from http://gnuwin32.sourceforge.net/packages/diffutils.htm no help.

Upvotes: 0

Views: 4953

Answers (3)

S-S
S-S

Reputation: 1

The best way to compare files in both directions

@echo off
::*********-Code by S-S Guca Srbija 2019-*********
title File-Compare by s-s
mode con cols=41 lines=10 & color 09
Set "File1a=File1.txt"
Set "File2b=File2.txt"
Set "Result=CompareResult.txt"
::************************************************
Set "File1b=%File1a%"
Set "File2a=%File2b%"
:Start
cls
set "Write=0"
Set /a "Test1+=1"
Echo(
Echo(=========================================
Echo( I compare Files %File1a% To %File2b%
Echo(=========================================
FOR /F "delims=" %%A in (%File1a%) do (
    Set /a "LineNum1+=1"
    Call :EXloop "%%A"
)
IF "%Test1%"=="2" Goto:End
Set "File1a=%File2a%"
Set "File2b=%File1b%"
Set "LineNum1="
Set "LineNum2="
Goto:Start
:End
EXIT
:EXloop
FOR /F "delims=" %%B in (%File2b%) do (
    IF "%~1"=="%%B" goto:next
)
Set "LineNum2=0"
FOR /F "delims=" %%C in (%File2b%) do (
    Set "MyFiles=%%C"
    Call :LineCount
)
Goto:next1
:LineCount
Set /a "LineNum2+=1"
IF "%LineNum2%"=="%LineNum1%" (
    set "File2=%MyFiles%"
)
goto:eof
:next1
Echo(>> "%Result%"
IF "%Test1%"=="1" (
    set /a "Write+=1"
        IF "%Write%"=="0" (
                Echo( They were compared %File1a% To %File2b%>> "%Result%"
        )
)
IF "%Test1%"=="2" (
    set /a "Write+=1"
        IF "%Write%"=="0" (
                Echo( They were compared %File1a% To %File2b%>> "%Result%"
        )
)
Echo(>> "%Result%"
Echo(=========================>> "%Result%"
Echo(%File1a%=Line=%LineNum1%=%~1>> "%Result%"
Echo((------------------------)>> "%Result%"
Echo(%File2b%=Line=%LineNum1%=%File2%>> "%Result%"
Echo(=========================>> "%Result%"
:next
goto:eof
::*********-Code by S-S Guca Srbija 2019-*********

OR

@echo off
::*********-Code by S-S Guca Srbija 2019-*********
title File-Compare by s-s
mode con cols=41 lines=10 & color 09
SETLOCAL EnableDelayedExpansion
Set "File1a=File1.txt"
Set "File2b=File2.txt"
Set "Result=CompareResult.txt"
::************************************************
Set "File1b=!File1a!"
Set "File2a=!File2b!"
:Start
cls
set "Write=0"
Set /a "Test1+=1"
Echo(
Echo(=========================================
Echo( I compare Files !File1a! To !File2b!
Echo(=========================================
FOR /F "delims=" %%A in (!File1a!) do (
    Set /a "LineNum1+=1"
    Call :EXloop "%%A"
)
IF "%Test1%"=="2" Goto:End
Set "File1a=!File2a!"
Set "File2b=!File1b!"
Set "LineNum1="
Set "LineNum2="
Goto:Start
:End
SETLOCAL DisableDelayedExpansion
EXIT
:EXloop
FOR /F "delims=" %%B in (!File2b!) do (
    IF "%~1"=="%%B" goto:next
)
Set "LineNum2=0"
FOR /F "delims=" %%C in (!File2b!) do (
    Set "MyFiles=%%C"
    Call :LineCount
)
Goto:next1
:LineCount
Set /a "LineNum2+=1"
IF "%LineNum2%"=="%LineNum1%" (
    set "File2=!MyFiles!"
)
goto:eof
:next1
Echo(>> "!Result!"
IF "%Test1%"=="1" (
    set /a "Write+=1"
        IF "%Write%"=="0" (
                Echo( They were compared !File1a! To !File2b!>> "!Result!"
        )
)
IF "%Test1%"=="2" (
    set /a "Write+=1"
        IF "%Write%"=="0" (
                Echo( They were compared !File1a! To !File2b!>> "!Result!"
        )
)
Echo(>> "!Result!"
Echo(=========================>> "!Result!"
Echo(!File1a!=Line=!LineNum1!=%~1>> "!Result!"
Echo((------------------------)>> "!Result!"
Echo(!File2b!=Line=!LineNum1!=!File2!>> "!Result!"
Echo(=========================>> "!Result!"
:next
goto:eof
::*********-Code by S-S Guca Srbija 2019-*********

:: Modify the code as you needed

Upvotes: 0

Daniel
Daniel

Reputation: 3057

Your code is almost correct, but you have some () wrong. Try this one:

@echo off
del d:\test\windows\comp\diff.txt
SETLOCAL EnableDelayedExpansion
for /F "tokens=* delims=." %%a in (f1.txt) do (
    echo %%a
    call :myInnerLoop "%%a"
)

echo out of inner loop
goto :eof

:myInnerLoop
for /F "tokens=* delims=." %%b in (f2.txt) do (
    echo "x: " %~1
    echo "y: " %%b
    if "%~1"=="%%b" (
        echo next
        goto :next
    )
)
echo "Log " %~1
echo %~1 >> "d:\test\windows\comp\diff.txt"

:next
goto :eof

Upvotes: 1

user541686
user541686

Reputation: 210362

Are you looking for the comp command?

Compares the contents of two files or sets of files.

COMP [data1] [data2] [/D] [/A] [/L] [/N=number] [/C] [/OFF[LINE]]

  data1      Specifies location and name(s) of first file(s) to compare.
  data2      Specifies location and name(s) of second files to compare.
  /D         Displays differences in decimal format.
  /A         Displays differences in ASCII characters.
  /L         Displays line numbers for differences.
  /N=number  Compares only the first specified number of lines in each file.
  /C         Disregards case of ASCII letters when comparing files.
  /OFF[LINE] Do not skip files with offline attribute set.

To compare sets of files, use wildcards in data1 and data2 parameters.

Upvotes: 0

Related Questions