使用win batch脚本比较SIDE by SIDE文件

时间:2014-05-05 11:04:17

标签: windows batch-file

我正在做以下事情,但有两个问题(win 2003):

  1. 输出包括匹配的第一行和最后一行: - (
  2. 输出不是并排
  3. 这两个问题可以解决吗? Suggestiongs(我试过/ nnn和/ lb选项)

    SCRIPT:

    :bof
        ::Swith off echo - the @ switch off echo for this line
        @echo off  
        :: Clear screen
        cls 
        :: Keep Locals contained to this batch file           
        setlocal       
    
        :: Set the two files and check that they exist
        set fileA=X:\tst\pfsrc\alex.txt
        set fileB=X:\tst\cbsrc\alex.txt
        if not exist "%fileA%" echo %fileA% not found & goto :end
        if not exist "%fileB%" echo %fileB% not found & goto :end
    
        :: del .old file rename previous output file to .old
        rem if exist X:\tst\cbsrc\resultPF1.old del X:\tst\cbsrc\resultPF1.old
        rem if exist X:\tst\cbsrc\resultPF1.txt rename X:\tst\cbsrc\resultPF1.txt *.old
    
        :: compare files
        FC /c /l /n /w %fileA% %fileB% 
    
    :end
    

    第一个输入文件:

    new line
    line1
    line2
    line3
    line4
    insert new line
    and another new line
    line5
    line6
    line7
    and a line here
    line8
    line9
    line10
    what is this line?
    line11
    

    第二个输入文件:

    new line
    alex
    hart
    was
    here
    line5
    line6
    line7
    line8
    line here
    line9
    line10
    

    输出:

    Comparing files X:\TST\PFSRC\alex.txt and X:\TST\CBSRC\ALEX.TXT
    ***** X:\TST\PFSRC\alex.txt
        1:  new line
        2:  line1
        3:  line2
        4:  line3
        5:  line4
        6:  insert new line
        7:  and another new line
        8:  line5
    ***** X:\TST\CBSRC\ALEX.TXT
        1:  new line
        2:  alex
        3:  hart
        4:  was
        5:  here
        6:  line5
    *****
    
    ***** X:\TST\PFSRC\alex.txt
       10:  line7
       11:  and a line here
       12:  line8
       13:  line9
    ***** X:\TST\CBSRC\ALEX.TXT
        8:  line7
        9:  line8
       10:  line here
       11:  line9
    *****
    
    ***** X:\TST\PFSRC\alex.txt
       15:  what is this line?
       16:  line11
    ***** X:\TST\CBSRC\ALEX.TXT
    *****
    

1 个答案:

答案 0 :(得分:2)

FC命令始终显示差异,因为一组接一个列出的行;没有办法并排显示差异。您可以获得FC输出并在批处理程序中处理它,以便区别显示差异,但您必须意识到此程序应识别不同的特定情况,以便正确显示两个部分并排。

前段时间我写了这样一个程序;我称之为“FComp.bat”,你可以下载它here。例如:

C:\> FComp.bat /C /L /N /W 1stInputFile.txt 2ndInputFile.txt
Comparing files 1stInputFile.txt and 2NDINPUTFILE.TXT

==============================  SECTION MODIFIED  =============================

    1:  new line                       |    1:  new line
    2:  line1                          |    2:  alex
    3:  line2                          |    3:  hart
    4:  line3                          |    4:  was
    5:  line4                          |    5:  here
    6:  insert new line                |    6:  line5
    7:  and another new line
    8:  line5

==============================  SECTION MODIFIED  =============================

   10:  line7                          |    8:  line7
   11:  and a line here                |    9:  line8
   12:  line8                          |   10:  line here
   13:  line9                          |   11:  line9

OLD SECTION DELETED AT END OF FILE  ===========================================

-   15:  what is this line?
-   16:  line11