2011-07-28 106 views
0

这是我在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 

但它不能帮助我。

即使我尝试diff效用也从http://gnuwin32.sourceforge.net/packages/diffutils.htm没有帮助。

+4

是否可以选择使用perl或python等脚本语言代替windows批处理文件?如果是这样,这个问题就变得微不足道了。 –

+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 
+0

感谢这工作!!!!!!!! –

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. 
+0

我也尝试过comp命令,它不会将我需要的结果作为geeting E:\ test> comp f1.txt f2.txt 比较f1.txt和f2.txt ... 文件是不同的大小。 对比更多文件(Y/N)? n E:\ test> comp/D f1.txt f2.txt 比较f1.txt和f2.txt ... 文件大小不同。 对比更多文件(Y/N)? n E:\ test> comp/A f1.txt f2.txt 比较f1.txt和f2.txt ... 文件大小不同。 对比更多文件(Y/N)? n –