0

I am using FC filename1 filename2 >> myLog.txt(where myLog.txt exists) command under Windows to compare two files. I use the command in a loop so I could compare many pairs of files.

I want to ignore the output when FC outputs:

Comparing files filename and filename2
FC: no differences encountered

Because it floods the output file with unwanted information.

How can I do this? And if possible, can I count the number of "successfull"(the one which output I ignore) compares?

I tried with FC filename1 filename2 2> myLog.txt to redirect only the error stream, but it doesn't output anything, meaning the command only outputs to the standard stream.

Zdravko Donev
  • 394
  • 3
  • 18
  • Perhaps you could write the `FC` output to a temporary file. After deciding that you want to keep it, the temporary file could be appended to the myLog.txt file. This would also be the place to increment a count. – lit Apr 17 '17 at 15:37
  • `fc` sets `ErrorLevel` in case of differences and clears it otherwise, so you could make use of that... – aschipfl Apr 17 '17 at 16:11

2 Answers2

2

You have not specified how you "use the command in a loop so I could compare many pairs of files" (how do you get the second file of each pair in the loop?), so you must modify the code below accordingly.

@echo off
setlocal

set successfull=0
(for %%a in (filename1*.txt) do (
   FC "%%a" "%%~Na2.txt" > diffs.tmp
   if errorlevel 1 (
      type diffs.tmp
   ) else (
      set /A successfull+=1
   )
)) > C:\other\folder\myLog.txt
del diffs.tmp

echo Successfull compares: %successfull%

If the myLog.txt file must grow with each run of this program, then just change the > redirection by an >> append one...

Aacini
  • 59,374
  • 12
  • 63
  • 94
  • So instead of my try "FC filename1 filename2 2> myLog.txt"(where I redirected the error stream) I just need to check whether the errorlevel flag is risen to 1? – Zdravko Donev Apr 17 '17 at 16:09
2

Easy enough to do by piping output of FC to FIND /V or FINDSTR /V. Check out the help on those commands. If you have any further questions please post your code.

RGuggisberg
  • 4,379
  • 1
  • 16
  • 23
  • 1
    How `FIND /V` or `FINDSTR /V` could be used to eliminate the first line ("Comparing files filename and filename2") from `FC filename1 filename2` output when no differences encountered? – Aacini Apr 17 '17 at 17:35
  • FC filename1 filename2 | FIND /I /V "no differences encountered" – RGuggisberg Apr 17 '17 at 18:05
  • I suggest you to _carefully_ read both the OP request and my comment! Your "solution" does NOT eliminate _the first line_ of `FC` command output, the one that is exemplified here as `Comparing files filename and filename2`!!! – Aacini Apr 17 '17 at 23:32
  • Ok... I did not put any detail in my original answer because the OP did not post code. What OP is asking for is accomplished with something like:FC filename1 filename2 | FIND /I /V "no differences encountered" | FIND /I /V "Comparing files" – RGuggisberg Apr 18 '17 at 01:09
  • In such a case the "Comparing files" line with the filenames would also be eliminated when _there are differences_! This is the request: "I want to ignore this FC output when no difference: `Comparing files filename and filename2 FC: no differences encountered`" and, as corollary: "include all FC output when there are differences". I am sure this result can _not_ be obtained via `FIND /V` nor `FINDSTR /V`, as you stated... – Aacini Apr 18 '17 at 02:02