线与批处理的比较



我想将.txt的第一列与每行after.txt行的第一列进行比较(第一个,第二个,第二个,依此类推(。现在输出,我仅获得最后一个值E992A84B8C8A1FEF3B94242403D5D84B两次。我的问题是如何比较所有行并获取结果消息?

 FOR /F "tokens=1 delims= " %%G IN (before.txt) DO set variable1=%%G
    FOR /F "tokens=1 delims= " %%I IN (after.txt) DO set variable2=%%I 
    echo !variable1!
    echo !variable2!
 if /I !variable1!==!variable2! (
     echo !variable1! !variable2! md5sum check is ok
     )

after.txt

94F948D2615318505FD84D722A6F5F4F  U:testbmbf012F96B1522AMASTER0000001.tif
4F5022E3290A9A8A4E8905C5CAAFB1A6  U:testbmbf012F96B1522AMASTER0000002.tif
776DF4069AD1914D9C37593E423BC0E4  U:testbmbf012F96B1522AMASTER0000003.tif
95EC963E9C789B3502E1E2C85E505218  U:testbmbf012F96B1522AMASTER0000004.tif
D5DD98F880A7204092EAA9355A4B558B  U:testbmbf012F96B1522AMASTER0000005.tif
1A08F9B01904F3EF689B44093343AE2C  U:testbmbf012F96B1522AMASTER0000006.tif
437DC62245852A01CCF4F2689F82920E  U:testbmbf012F96B1522AMASTER0000007.tif
A4E7C76EC523F1E2799BE8CE049D28FC  U:testbmbf012F96B1522AMASTER0000008.tif
177689553B9D9392AD6B72130EE1D22F  U:testbmbf012F96B1522AMASTER0000009.tif
EB0F2F741428CF376909AB65BEF6659F  U:testbmbf012F96B1522AMASTER0000010.tif
E992A84B8C8A1FEF3B94242403D5D84B  U:testbmbf012F96B1522AMASTER0000011.tif

ther.txt

94F948D2615318505FD84D722A6F5F4F  U:testbmbf012F96B1522A0000001.tif
4F5022E3290A9A8A4E8905C5CAAFB1A6  U:testbmbf012F96B1522A0000002.tif
776DF4069AD1914D9C37593E423BC0E4  U:testbmbf012F96B1522A0000003.tif
95EC963E9C789B3502E1E2C85E505218  U:testbmbf012F96B1522A0000004.tif
D5DD98F880A7204092EAA9355A4B558B  U:testbmbf012F96B1522A0000005.tif
1A08F9B01904F3EF689B44093343AE2C  U:testbmbf012F96B1522A0000006.tif
437DC62245852A01CCF4F2689F82920E  U:testbmbf012F96B1522A0000007.tif
A4E7C76EC523F1E2799BE8CE049D28FC  U:testbmbf012F96B1522A0000008.tif
177689553B9D9392AD6B72130EE1D22F  U:testbmbf012F96B1522A0000009.tif
EB0F2F741428CF376909AB65BEF6659F  U:testbmbf012F96B1522A0000010.tif
E992A84B8C8A1FEF3B94242403D5D84B  U:testbmbf012F96B1522A0000011.tif

aschipl的批次调整了您的要求:

:: Q:Test2018427SO_50067394.cmd
@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "FILE1=after.txt"
set "FILE2=before.txt"
:: set "RET=Comparison.txt" & rem // (none to output to console)
if not defined RET set "RET=con"
for /F %%C in ('^< "%FILE1%" find /C /V ""') do set "NUM1=%%C"
4< "%FILE1%" 3< "%FILE2%" > "%RET%" (
     for /L %%I in (1,1,%NUM1%) do (
         set "LINE1="
         0<&4 set /P "LINE1="
         set "LINE2="
         0<&3 set /P "LINE2="
         if not ErrorLevel 1 If "!LINE1:~0,32!"=="!LINE2:~0,32!" echo(!LINE1:~0,32! !LINE2:~0,32! md5sum check is ok
     )
)
endlocal
exit /B

样本输出:

> Q:Test2018427SO_50067394.cmd
94F948D2615318505FD84D722A6F5F4F 94F948D2615318505FD84D722A6F5F4F md5sum check is ok
4F5022E3290A9A8A4E8905C5CAAFB1A6 4F5022E3290A9A8A4E8905C5CAAFB1A6 md5sum check is ok
776DF4069AD1914D9C37593E423BC0E4 776DF4069AD1914D9C37593E423BC0E4 md5sum check is ok
95EC963E9C789B3502E1E2C85E505218 95EC963E9C789B3502E1E2C85E505218 md5sum check is ok
D5DD98F880A7204092EAA9355A4B558B D5DD98F880A7204092EAA9355A4B558B md5sum check is ok
1A08F9B01904F3EF689B44093343AE2C 1A08F9B01904F3EF689B44093343AE2C md5sum check is ok
437DC62245852A01CCF4F2689F82920E 437DC62245852A01CCF4F2689F82920E md5sum check is ok
A4E7C76EC523F1E2799BE8CE049D28FC A4E7C76EC523F1E2799BE8CE049D28FC md5sum check is ok
177689553B9D9392AD6B72130EE1D22F 177689553B9D9392AD6B72130EE1D22F md5sum check is ok
EB0F2F741428CF376909AB65BEF6659F EB0F2F741428CF376909AB65BEF6659F md5sum check is ok
E992A84B8C8A1FEF3B94242403D5D84B E992A84B8C8A1FEF3B94242403D5D84B md5sum check is ok

在此答案中描述了并行读取几个文件的常用方法,包括通过 for /F命令读取第一个文件和通过重定向的set /P命令(s(读取第一个文件(s(。例如:

@echo off
setlocal EnableDelayedExpansion
rem First file is read with FOR /F command
rem Second file is read via redirected stdin
< after.txt (for /F %%G in (before.txt) do (
   rem Read next line from after.txt
   set /P "variable2="
   rem Compare first token of both files
   for /F %%I in ("!variable2!") do (
      if /I %%G==%%I (
         echo %%G %%I md5sum check is ok
      )
   )
))

请注意,默认的for /F行为是由Space划定的第一个令牌,因此"tokens=1 delims= "选项不是必需的...

但是,如果文件很小,则可以使用更简单的方法:

@echo off
for /F "tokens=1,2 delims=: " %%F in ('findstr /N "^" before.txt') do (
   for /F "tokens=1,2 delims=: " %%H in ('findstr /N "^" after.txt') do (
      if %%F equ %%H if /I %%G == %%I (
         echo %%G  %%I md5sum check is ok
      )
   )
)

在此方法中,findstr /N "^"命令用于枚举两个文件中的所有行,因此if %%F equ %%H命令都用于在两个文件中同步(选择相同(行。此方法不是有效的,因此如果文件不小,则需要更长的时间...

off主题powershell答案与相同结果

## Q:Test2018427SO_50067394.ps1
$after  = Import-csv .after.txt  -delimiter ' ' -header MD5,X,FileName
$before = Import-csv .before.txt -delimiter ' ' -header MD5,X,FileName
for ($I=0;$I -le $after.Count;$I++) {
  If ($after[$i].MD5 -eq $before[$I].MD5){
    "{0} {1} md5sum check is ok" -f $after[$i].MD5,$before[$I].MD5
  } else {
    "MD5 differs between files {0} and {1}" -f $after[$i].FileName,$before[$I].FileName5
  }
}

您可以set 2计数器。当计数器相等时,您可以测试2列。这样,您还会得到错误的行号!

@echo off
setlocal enabledelayedexpansion
set $counter1=1
for /f "delims= " %%a in (before.txt) do (
   set $counter2=1
   for /f "delims= " %%b in (after.txt) do (
        if !$counter1!==!$counter2! (
            if "%%a"=="%%b" (echo line !$counter1! cheksum OK
            ) else (echo line !$counter1! cheksum KO)
        )
        set /a $counter2+=1
   )
   set /a $counter1+=1
   )

输出:

line 1 cheksum OK
line 2 cheksum OK
line 3 cheksum OK
line 4 cheksum OK
line 5 cheksum OK
line 6 cheksum OK
line 7 cheksum OK
line 8 cheksum OK
line 9 cheksum KO
line 10 cheksum OK
line 11 cheksum OK

相关内容

  • 没有找到相关文章

最新更新