batch-filecomparefindstr

How do I compare two text files excluding the first 5 characters of each line?


I'm trying to compare two files. They should be the same except for the firs 5 characters. I need the error file to contain the line(s) that do not match. Is there a way to limit the findstr command to only look at the characters at position 6 and beyond?

@echo off
echo Start %date% %time%
REM Verify that File1.txt and File2.txt are identical except for the first 5 characters
findstr /vio File1.txt File2.txt > Berror.txt
echo End %date% %time%
Berror.txt
Pause

File1.txt

00000spider
00000center
00000earwax
00000horn
00000cup
00000ton
00000ankle
00000foster
00000restaurant
00000ecstasy

File2.txt

46884spider
12464center
31197earwax
32514horn
64856cup
61465ton
91468anklet
31515foster
91580restaurant
41623ecstasy

I've tried using /r with [6-20] but I can't find an example with the sytax so I don't really know how to use it. The output I am expecting is line 7: 91468anklet and 00000ankle since that is the only line with a difference in this example.


Solution

  • @echo off
    setlocal EnableDelayedExpansion
    
    < file2.txt (
       for /F "tokens=1,* delims=:" %%a in ('findstr /N "^" file1.txt') do (
          set "line1=%%b"
          set /P "line2="
          if "!line1:~5!" neq "!line2:~5!" (
             echo Line # %%a:
             echo File1: !line1!
             echo File2: !line2!
          )
       )
    )
    

    Output:

    Line # 7:
    File1: 00000ankle
    File2: 91468anklet