Compare text files in batch

August 18, 2010 at 01:10:13
Specs: Windows XP
Good Morning

Hope someone can help meI need to create a batch script that compare two text files (lets say test1.txt and test2.txt)

My text file look like
Test1.txt
Arsenal
abc.txt.txt
abc2.txt.txt

and Test2.txt
Liverpool
abc.txt.txt
abc3.txt.txt

The batch script should compare the lines in the text file first.
If the number of lines different in both text files, then its check the string inside the text file if its similar or not.

If a difference is found I want it to write the line of data to a new text file - Difference.txt

Hope someone help me in this
thanks


See More: Compare text files in batch

Report •


#1
August 18, 2010 at 09:10:34
Ok I coded a program just for you to do this, but the only thing is you have to have quit alot of experience with commands to edit the script for it to work with other files. Also the difference.txt file is outputted to the same folder as the batch file, test1.txt and test2.txt. Well anyway here is my code. Oh and one more thing the differences are what is different in test2.txt so that will be in the differences.txt file.

--------------------------------
@echo off
setlocal enabledelayedexpansion
:file1
set linecount=1
for /f "tokens=1 delims=" %%a in (test1.txt) do (
set line!linecount!=%%a
set /a linecount=!linecount!+1
)
set file1line1=%line1%
set file1line2=%line2%
set file1line3=%line3%

:file2
set linecount=1
for /f "tokens=1 delims=" %%a in (test2.txt) do (
set line!linecount!=%%a
set /a linecount=!linecount!+1
)
set file2line1=%line1%
set file2line2=%line2%
set file2line3=%line3%

echo File 1 text:
echo %file1line1%
echo %file1line2%
echo %file1line3%
echo.
echo File 2 text:
echo %file2line1%
echo %file2line2%
echo %file2line3%
pause
cls
if %file1line1%==%file2line1% goto next1
echo %file2line1%>>"Difference.txt"
goto next1

:next1
if %file1line2%==%file2line2% goto next2
echo %file2line2%>>"Difference.txt"
goto next2

:next2
if %file1line3%==%file2line3% goto done
echo %file2line3%>>"Difference.txt"
goto done

:done
echo Differences have been outputed to Difference.txt
pause
exit
--------------------------------


Report •

#2
August 19, 2010 at 06:08:39
fc test1.txt test2.txt > Difference.txt


Report •

Related Solutions


Ask Question