2

I've made three text files: 0nl.txt, 1nl.txt and 2nl.txt.

They both have the same content:

test
hello

The only difference between them is how many newlines they have after the final 'o', with 0, 1 and 2 respectively.

I can compare between the files using FC:

C:\Users\NeatN\Desktop\fctest>fc 0nl.txt 1nl.txt
Comparing files 0nl.txt and 1NL.TXT
FC: no differences encountered


C:\Users\NeatN\Desktop\fctest>fc 0nl.txt 2nl.txt
Comparing files 0nl.txt and 2NL.TXT
***** 0nl.txt
***** 2NL.TXT

*****


C:\Users\NeatN\Desktop\fctest>fc 1nl.txt 2nl.txt
Comparing files 1nl.txt and 2NL.TXT
***** 1nl.txt
***** 2NL.TXT

*****

COMP, on the other hand, detects that they have different sizes but doesn't show the differences between them:

C:\Users\NeatN\Desktop\fctest>comp 0nl.txt 1nl.txt /M
Comparing 0nl.txt and 1nl.txt...
Files are different sizes.


C:\Users\NeatN\Desktop\fctest>comp 0nl.txt 2nl.txt /M
Comparing 0nl.txt and 2nl.txt...
Files are different sizes.


C:\Users\NeatN\Desktop\fctest>comp 1nl.txt 2nl.txt /M
Comparing 1nl.txt and 2nl.txt...
Files are different sizes.

How do I detect the missing newline in 0nl.txt compared to 1nl.txt, and show it to the user?

0

2 Answers 2

2

You may use fc /n. The /n parameter is described as :

/N    Display line numbers during an ASCII comparison.

This would look on comparison like this :

image

3
  • This doesn't do what I need: FC still ignores the missing newline in 0nl.txt.
    – NeatNit
    Nov 3, 2018 at 17:46
  • Is the problem that the newlines are Linux type line-feeds rather than Windows type carriage-return/line-feeds? Could you post these two files?
    – harrymc
    Nov 3, 2018 at 18:08
  • No, they were all made with notepad. I managed to figure it out, see my own answer - maybe that will clarify what I meant.
    – NeatNit
    Nov 3, 2018 at 18:20
0

I solved it with the following code:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FC /B 0nl.txt 1nl.txt > NUL
IF !ERRORLEVEL! EQU 0 (ECHO TEST PASSED) ELSE (
    ECHO TEST FAILED - output doesn't match
    FC /N 0nl.txt 1nl.txt
    IF !ERRORLEVEL! EQU 0 ECHO There is a missing or extra newline character ^(\n^) at the end of the file
)

Essentially, I do a binary comparison first, and if that fails then there must be a difference. If an ASCII comparison passes later, then the difference must be in the final newline.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .