比较两个文件所需的批处理文件

时间:2011-07-28 05:45:45

标签: windows batch-processing

这是我在Windows批处理文件中的要求我尝试了以下

Example:
f1.txt
sam
varun
ramesh
babu

f2.txt
babu
sam

我需要输出

varun
ramesh

该计划

@echo on
SETLOCAL EnableDelayedExpansion
for /F "tokens=* delims=." %%a in (f1.txt) do (
    call :myInnerLoop "%%a"
)

echo out of inner loop
)
goto :eof


:myInnerLoop
for /F "tokens=* delims=." %%b in (f2.txt) do (
    if "%~1"=="%%b" (
    echo inside inner loop
        goto :next
    ) else ( 
        echo %%a >> "E:\test\diff.txt"
    )
:next
goto :eof

但它不能帮助我。

即使我在http://gnuwin32.sourceforge.net/packages/diffutils.htm没有帮助也尝试了diff实用程序。

3 个答案:

答案 0 :(得分:1)

您的代码几乎是正确的,但您有一些()错误。试试这个:

@echo off
del d:\test\windows\comp\diff.txt
SETLOCAL EnableDelayedExpansion
for /F "tokens=* delims=." %%a in (f1.txt) do (
    echo %%a
    call :myInnerLoop "%%a"
)

echo out of inner loop
goto :eof

:myInnerLoop
for /F "tokens=* delims=." %%b in (f2.txt) do (
    echo "x: " %~1
    echo "y: " %%b
    if "%~1"=="%%b" (
        echo next
        goto :next
    )
)
echo "Log " %~1
echo %~1 >> "d:\test\windows\comp\diff.txt"

:next
goto :eof

答案 1 :(得分:0)

您在寻找comp命令吗?

Compares the contents of two files or sets of files.

COMP [data1] [data2] [/D] [/A] [/L] [/N=number] [/C] [/OFF[LINE]]

  data1      Specifies location and name(s) of first file(s) to compare.
  data2      Specifies location and name(s) of second files to compare.
  /D         Displays differences in decimal format.
  /A         Displays differences in ASCII characters.
  /L         Displays line numbers for differences.
  /N=number  Compares only the first specified number of lines in each file.
  /C         Disregards case of ASCII letters when comparing files.
  /OFF[LINE] Do not skip files with offline attribute set.

To compare sets of files, use wildcards in data1 and data2 parameters.

答案 2 :(得分:0)

双向比较文件的最佳方法

@echo off
::*********-Code by S-S Guca Srbija 2019-*********
title File-Compare by s-s
mode con cols=41 lines=10 & color 09
Set "File1a=File1.txt"
Set "File2b=File2.txt"
Set "Result=CompareResult.txt"
::************************************************
Set "File1b=%File1a%"
Set "File2a=%File2b%"
:Start
cls
set "Write=0"
Set /a "Test1+=1"
Echo(
Echo(=========================================
Echo( I compare Files %File1a% To %File2b%
Echo(=========================================
FOR /F "delims=" %%A in (%File1a%) do (
    Set /a "LineNum1+=1"
    Call :EXloop "%%A"
)
IF "%Test1%"=="2" Goto:End
Set "File1a=%File2a%"
Set "File2b=%File1b%"
Set "LineNum1="
Set "LineNum2="
Goto:Start
:End
EXIT
:EXloop
FOR /F "delims=" %%B in (%File2b%) do (
    IF "%~1"=="%%B" goto:next
)
Set "LineNum2=0"
FOR /F "delims=" %%C in (%File2b%) do (
    Set "MyFiles=%%C"
    Call :LineCount
)
Goto:next1
:LineCount
Set /a "LineNum2+=1"
IF "%LineNum2%"=="%LineNum1%" (
    set "File2=%MyFiles%"
)
goto:eof
:next1
Echo(>> "%Result%"
IF "%Test1%"=="1" (
    set /a "Write+=1"
        IF "%Write%"=="0" (
                Echo( They were compared %File1a% To %File2b%>> "%Result%"
        )
)
IF "%Test1%"=="2" (
    set /a "Write+=1"
        IF "%Write%"=="0" (
                Echo( They were compared %File1a% To %File2b%>> "%Result%"
        )
)
Echo(>> "%Result%"
Echo(=========================>> "%Result%"
Echo(%File1a%=Line=%LineNum1%=%~1>> "%Result%"
Echo((------------------------)>> "%Result%"
Echo(%File2b%=Line=%LineNum1%=%File2%>> "%Result%"
Echo(=========================>> "%Result%"
:next
goto:eof
::*********-Code by S-S Guca Srbija 2019-*********

OR

@echo off
::*********-Code by S-S Guca Srbija 2019-*********
title File-Compare by s-s
mode con cols=41 lines=10 & color 09
SETLOCAL EnableDelayedExpansion
Set "File1a=File1.txt"
Set "File2b=File2.txt"
Set "Result=CompareResult.txt"
::************************************************
Set "File1b=!File1a!"
Set "File2a=!File2b!"
:Start
cls
set "Write=0"
Set /a "Test1+=1"
Echo(
Echo(=========================================
Echo( I compare Files !File1a! To !File2b!
Echo(=========================================
FOR /F "delims=" %%A in (!File1a!) do (
    Set /a "LineNum1+=1"
    Call :EXloop "%%A"
)
IF "%Test1%"=="2" Goto:End
Set "File1a=!File2a!"
Set "File2b=!File1b!"
Set "LineNum1="
Set "LineNum2="
Goto:Start
:End
SETLOCAL DisableDelayedExpansion
EXIT
:EXloop
FOR /F "delims=" %%B in (!File2b!) do (
    IF "%~1"=="%%B" goto:next
)
Set "LineNum2=0"
FOR /F "delims=" %%C in (!File2b!) do (
    Set "MyFiles=%%C"
    Call :LineCount
)
Goto:next1
:LineCount
Set /a "LineNum2+=1"
IF "%LineNum2%"=="%LineNum1%" (
    set "File2=!MyFiles!"
)
goto:eof
:next1
Echo(>> "!Result!"
IF "%Test1%"=="1" (
    set /a "Write+=1"
        IF "%Write%"=="0" (
                Echo( They were compared !File1a! To !File2b!>> "!Result!"
        )
)
IF "%Test1%"=="2" (
    set /a "Write+=1"
        IF "%Write%"=="0" (
                Echo( They were compared !File1a! To !File2b!>> "!Result!"
        )
)
Echo(>> "!Result!"
Echo(=========================>> "!Result!"
Echo(!File1a!=Line=!LineNum1!=%~1>> "!Result!"
Echo((------------------------)>> "!Result!"
Echo(!File2b!=Line=!LineNum1!=!File2!>> "!Result!"
Echo(=========================>> "!Result!"
:next
goto:eof
::*********-Code by S-S Guca Srbija 2019-*********

::根据需要修改代码