0

I am trying to compare two files (old.txt and new.txt), if any lines that are in new.txt are different then old.txt then output to a txt file. I don't want any lines from old.txt present in the new file.

New files are uploaded to c:\temp\download and once processed, are copied to c:\temp\download\archived.

The script I'm using below will find the latest file in c:\temp\download and compare it with the last modified file in c:\temp\download\archived, and outputs the differences to a txt file. It works for the most part but the problem I am facing is that the output (%latestD%) file also contains lines from the old.txt. It should be a one way compare, new vs old and the output should not contain lines from the old.txt.

Here is what I am looking to achieve:

OLD.txt
Jim 789
Jane 123
James 999
Lily 111

NEW.txt
Jim 123
Jane 123
James 123

OUTPUT.txt
Jim 123
James 123

set "fC=C:\temp\download"
set "latestC="
for /f "delims=" %%a in ('dir "%fC%" /b /od /a-d /tw 2^>nul') do set "latest=%%a" & set   "latestC=%fC%\%%a"
set "fD=C:\temp\download\archived
set "latestD="
for /f "delims=" %%a in ('dir "%fD%" /b /od /a-d /tw 2^>nul') do set "latestD=%fD%\%%a"
if not defined latestC ( echo NO File in C & exit /b )
if not defined latestD ( echo NO File in D & exit /b )
for /f "tokens=1,*" %%a in (
'diff "%latestC%" "%latestD%" ^| findstr /r /c:"^<" /c:"^>"'
) do (
>> "C:\temp\%latest%" echo(%%b
)

Thank you!

Aacini
  • 65,180
  • 12
  • 72
  • 108
ID10T
  • 25
  • 2
  • 11
  • Are the files html? Your findstr command is outputting `<` and `>` and batch has limitations. FWIW Windows doesn't have a `diff` command. – foxidrive Jun 03 '14 at 13:43
  • The files are all *.txt files. I have cygwin installed so utilizing the unix diff command. – ID10T Jun 03 '14 at 13:48
  • Does the diff command give you what you want when it is run alone, with just the filenames? – foxidrive Jun 03 '14 at 13:55
  • If I run the diff command alone, I get the same results, the contents of output contains duplicate lines (one from old.txt and the modified line from new.txt). Also lines that are present in old.txt and not new.txt are in the output when they shouldn't be. – ID10T Jun 03 '14 at 14:34

1 Answers1

0

This should do what you ask:

findstr /v /g:"old.txt" "new.txt"
foxidrive
  • 40,353
  • 10
  • 53
  • 68
  • If only it were that simple. `/L` option needed to force literal interpretation. Backslash \ in search strings may need to be escaped as \\, depending on context. Best to always escape them. The killer probem is a nasty FINDSTR bug - multiple literal search strings of varying length may fail to find all matches unless the `/I` (case insensitive) option is used. See [What are the undocumented features and limitations of the Windows FINDSTR command?](http://stackoverflow.com/q/8844868/1012053) – dbenham Jun 03 '14 at 16:43
  • Thanks Dave. For the case cited in the question it works perfectly though. – foxidrive Jun 04 '14 at 14:08