0

I'm trying to run a bat file that will compare 1 file to another and output the differences

I've tried using gnu diff utilites, fc, and endless googleing to find a solution but I cant seem to figure it out

File 1

C:\Books\Tolkien, J.R.R. - The Adventures Of Tom Bombadil.pdf
C:\Books\test.rtf.epub
C:\Books\w_E_20130215.epub

File 2

C:\Books\test.rtf.epub
C:\Books\w_E_20130215.epub

I want file 3 to be

C:\Books\Tolkien, J.R.R. - The Adventures Of Tom Bombadil.pdf

Any one have any ideas?

Xanderu
  • 747
  • 1
  • 8
  • 30

2 Answers2

1

You could use diff from the DiffUtils and something like this:

diff file1.txt file2.txt | findstr /r /c:"^<" /c:"^>" >file3.txt

The output lines will be preceded by < or >, depending on which file the respective line was missing in. If you want to remove those indicators as well, use something like this:

for /f "tokens=1*" %a in (
  'diff file1.txt file2.txt ^| findstr /r /c:"^<" /c:"^>"'
) do @echo %b >>file3.txt

Change %a and %b into %%a and %%b if you want to run this in a batch file.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • This actually worked perfectly. Had to do a little tweaking (pushd and pathing) but it worked like a charm! Thanks!! – Xanderu May 23 '13 at 11:37
  • It does appear to be generating a blank space at the end. Any way to stop it from doing that? I use the output to generate files and its creating files like this "XYZ .ext" EDIT: NVM I changed it to do @echo %b>>file3.txt and it worked – Xanderu May 23 '13 at 13:49
  • Either that, or put the redirection at the beginning of the line: `@>>file3.txt echo %b`. – Ansgar Wiechers May 23 '13 at 17:55
  • Having some more issues with this... When I try to do a diff using the same command on the following two files. It still says there is a difference https://dl.dropboxusercontent.com/u/54973965/Musicconverttemp.txt https://dl.dropboxusercontent.com/u/54973965/DontconvertFormed.txt The files include the same data but it seems to keep saying there are lines in one that are not in the other and vice versa – Xanderu Jun 14 '13 at 21:48
  • I finally figured it out... it was the sort order... I added `sort %rundir%\temp\DontConvertformed.txt >> %rundir%\temp\DontConvertformedsorted.txt sort %rundir%\temp\Musicconverttemp.txt >> %rundir%\temp\Musicconverttempsorted.txt` and it is working – Xanderu Jun 14 '13 at 22:33
0
FINDSTR /v /b /e /l /g:file2. file1. >file3.

should produce the required results - lines in file1 missing from file2.

/v says 'not found', /b /e forces exact match - not part-of-line-matches /l literal.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Oddly this produces C:\Books\w_E_20130215.epub – Xanderu May 23 '13 at 10:55
  • Only that line, or in addition? The last line will be reported if FILE1. does not end with a new-line. You may add e newline to file1. with `echo(>>file1.` (make sure there are two `>` characters) OR try `type file1|FINDSTR /v /b /e /l /g:file2. >file3.` – Magoo May 23 '13 at 11:28