Search code examples
windowsfilebatch-filecmdfilecompare

Windows fc ignore output when no difference


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.


Solution

  • 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...