Search code examples
windowsbatch-processing

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.


Solution

  • 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