Dos Batch用于比较两个文本文件是否逐行操作相同

时间:2015-08-03 16:35:22

标签: batch-file text

text1.txt

aaa aaa aa1
bbb bbb bb2
ccc ccc cc3
ddd ddd dd4

text2.txt

000004
aaa aaa aa1
bbb bbb bb2
ccc ccc cc3
ddd ddd dd4
END

我有两个文件。首先关注text2.txt。 000004表示有4条有效行。

我想要做的是使用批处理跳过第一行直到END并将中间四行与text1.txt内容进行比较。

它可能涉及循环或嵌套循环以逐行比较每一行。如果中间的4行完全匹配text1.txt,

      echo valid file
else (echo invalid file)

有什么想法吗? 真的非常感谢你。

3 个答案:

答案 0 :(得分:1)

我明白了,现在我知道使用了000004.确保“4”表示可用行,如果text2.txt和text1.txt的第5行匹配但第一行是000004,则可能返回无效文件

@echo off
setlocal enabledelayedexpansion

set bool=1
set /a count=0
set /a count_true=0
set /a skip_count=0
set access_bool=0

for /f "tokens=*" %%a in (text2.txt) do (
    if !bool! == 0 (
        call :Next_loop Next_content !skip_count! !access_bool! blank_param
        set /a skip_count+=1
        if %%a == !Next_content! (
            set /a count_true+=1
        )
    )
    if !bool! == 1 (
        set first_line=%%a
        set lines_to_skip=!first_line:~-1,1!
        call :Next_loop Next_content !skip_count! !access_bool! check_lines_in_loop

        if !check_lines_in_loop! gtr !lines_to_skip! goto result
        if !check_lines_in_loop! lss !lines_to_skip! goto result

        set bool=0
    )
)


:result
if !count_true! == !lines_to_skip! (
    echo Valid file!
) else (echo Invalid file!)
pause >nul


:Next_loop
set /a check_lines
set /a Next_count=0

for /f "tokens=*" %%b in (text1.txt) do (
    if %3 == 0 (
        set /a check_lines+=1
    )

    if !Next_count! == %2 (
        set %1=%%b
    )
    set /a Next_count+=1
)
if %3 == 0 (
    set %4=!check_lines!
    set %3=1
)

答案 1 :(得分:1)

回答KenyKeny问题。

call :Next_loop Next_content !skip_count! !access_bool! check_lines_in_loop

基本上它用4个参数调用:Next_loop:Next_content,!skip_count!等等...

没有“!”的参数标志
这样的参数是尚未分配的变量,如未知变量 2.您可以通过:Next_loopset %1=hello中为其指定一个值(%1将在稍后讨论)

带“!”的参数标志
1.像!skip_count!这样的参数是一个赋值变量,它具有值 2.您可以在:Next_loop中使用此参数,例如echo %2,因为之前已经分配了此参数 3.您也可以为其指定新值。

什么是%1,%2,%~1等。?
它表示由参数传递的值。 %1表示第一个参数(Next_content),其中%2表示第二个参数(!skip_content!)
2. %~1基本上是引号(“”)标记过滤器。例如:

@echo off
call :function "hello"

:function
echo %1
echo %~1

第一行回显“hello”,其中第二行过滤掉引号,只留下 hello

答案 2 :(得分:0)

这是我自己的编码,参考谷歌,我使用数组方法。每个人,让我们看看,我已经尝试过,它可以验证两个文件是否相同。我添加一些额外的回声来显示一些数组是null。确保两个文本文件位于同一目录中。

@echo off
setlocal enabledelayedexpansion

set textfile1=text1.txt
set textfile2=text2.txt

rem Verify textfile1 and textfile2
echo Now check whether text1.txt is the same as the text2.txt.
echo.

echo Read in textfile2 into array2
set arr_counter2=0
for /f "skip=1 delims==" %%a in (%textfile2%) do ( 
  if "%%a" equ "END" ( 
  echo You reach "END"
  goto :break1 
) else (
echo !arr_counter2!
set arr2[!arr_counter2!]=%%a
rem echo %%array2[!index!]%%
set /a arr_counter2+=1
echo !arr_counter2!
pause
)
)
:break1
echo number of elements in array = !arr_counter2!

echo !arr2[0]!
echo !arr2[1]!
echo !arr2[2]!
echo !arr2[3]!

echo !arr2[4]!
echo !arr2[5]!
echo !arr2[6]!
echo !arr2[7]!
echo !arr2[8]!
echo !arr2[9]!
pause 
echo.









echo Read in textfile1 into array1
set arr_counter1=0
for /f "delims==" %%b in (%textfile1%) do ( 
rem if "%%a" equ "END" ( 
rem echo You reach "END"
rem goto :break1 
rem ) else (
echo !arr_counter1!
set arr1[!arr_counter1!]=%%b
rem echo %%array1[!index!]%%
set /a arr_counter1+=1
echo !arr_counter1!
pause
)
)

echo number of elements in array = !arr_counter1!

echo !arr1[0]!
echo !arr1[1]!
echo !arr1[2]!
echo !arr1[3]!

echo !arr1[4]!
echo !arr1[5]!
echo !arr1[6]!
echo !arr1[7]!
echo !arr1[8]!
echo !arr1[9]!
pause 
echo.




set /a final_index=!arr_counter1!-1 
rem compare each element from [0] to [final_index]
for /l %%n in (0,1,!final_index!) do (
rem echo !arrayline[%%n]!
if !arr1[%%n]! equ !arr2[%%n]! (
echo same content.
pause 
) else (
echo wrong!
pause 
goto :eof
)
)
echo all contents are the same

pause