【问题标题】:Dos Batch to compare if two text files are the same by line by line operationDos Batch逐行比较两个文本文件是否相同
【发布时间】:2015-08-03 16:35:21
【问题描述】:

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)

有什么想法吗? 真的提前谢谢你。

【问题讨论】:

  • 你没有使用fc.exe有什么原因吗?

标签: batch-file text


【解决方案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
)

【讨论】:

  • 亲爱的dark fang,谢谢你的工作,但是在text1.txt如果我在底部多输入一行12dsf3585 dsf,程序似乎仍然声称这是有效的,你能有一个看吧,因为我还是一批菜鸟。
  • 我知道 for 循环中 %%a%%b 的含义,但是 %1 %2 %3 %4 是什么意思?它们是否在初始化时存储值,因为我没有看到%2 的初始化?简短的指南就足够了。谢谢。
  • @KenyKeny 如果你想通过call 命令传递一个变量,比如call :Next_loop Next_content !skip_count!。 1) Next_content 仍然是一个未知变量,但我们可以在 :Next_loop 中分配它,例如set %1=5,其中%1 代表Next_content。 2) !skip_count! 是一个赋值变量,所以我们可以在:Next_loop 中赋值一个新的/使用它,例如set /a assign_variable=%2,其中%2 代表s!kip_count!
  • 亲爱的dark fang,您的代码很有用,但我必须为新请求修改一些代码。在 text2 中,第一行变为 00000420150629,我尝试在 if !bool! 中进行一些更改。 == 1(),我写的是if !bool! == 1 ( set first_line=%%a set first_line=!first_line:~0,6! set /a first_line = 1!first_line!-1000000 set lines_to_skip=!first_line! echo !lines_to_skip! call :Next_loop Next_content !skip_count! !access_bool! ........................ set bool=0 )但是不行,说“系统找不到指定的路径”
  • 任何更改和 cmets,我确实尝试过,但我无法这样做,非常感谢
【解决方案2】:

回答 KenyKeny 问题。

call :Next_loop Next_content !skip_count! !access_bool! check_lines_in_loop

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

不带“!!”的参数标志
1. 像这样的参数是尚未赋值的变量,就像一个未知变量。
2. 您可以通过set %1=hello:Next_loop 中为其赋值(稍后将讨论%1)

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

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

@echo off
call :function "hello"

:function
echo %1
echo %~1

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

【讨论】:

    【解决方案3】:

    这是我自己参考google的编码,我使用了数组方法。大家来看看,我试过了,它可以验证两个文件是否相同。我添加了一些extra echo来显示某些数组为空。确保两个文本文件在同一个目录。

    @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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-02
      • 1970-01-01
      • 2015-12-02
      • 1970-01-01
      • 2014-08-24
      • 2013-10-01
      • 2017-01-29
      相关资源
      最近更新 更多