2011-07-28 114 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 –