行与批次的比较 [英] Comparison of the lines with batch

查看:24
本文介绍了行与批次的比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I want to compare the first column of before.txt with the first column of after.txt line per line ( the first with the first,the second with the second and so on). As output now I get only the last value E992A84B8C8A1FEF3B94242403D5D84B two times. My question is how to compare all the lines and get the result message?

 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:	estbmbf012F96B1522AMASTER0000001.tif
4F5022E3290A9A8A4E8905C5CAAFB1A6  U:	estbmbf012F96B1522AMASTER0000002.tif
776DF4069AD1914D9C37593E423BC0E4  U:	estbmbf012F96B1522AMASTER0000003.tif
95EC963E9C789B3502E1E2C85E505218  U:	estbmbf012F96B1522AMASTER0000004.tif
D5DD98F880A7204092EAA9355A4B558B  U:	estbmbf012F96B1522AMASTER0000005.tif
1A08F9B01904F3EF689B44093343AE2C  U:	estbmbf012F96B1522AMASTER0000006.tif
437DC62245852A01CCF4F2689F82920E  U:	estbmbf012F96B1522AMASTER0000007.tif
A4E7C76EC523F1E2799BE8CE049D28FC  U:	estbmbf012F96B1522AMASTER0000008.tif
177689553B9D9392AD6B72130EE1D22F  U:	estbmbf012F96B1522AMASTER0000009.tif
EB0F2F741428CF376909AB65BEF6659F  U:	estbmbf012F96B1522AMASTER0000010.tif
E992A84B8C8A1FEF3B94242403D5D84B  U:	estbmbf012F96B1522AMASTER0000011.tif

before.txt

94F948D2615318505FD84D722A6F5F4F  U:	estbmbf012F96B1522A0000001.tif
4F5022E3290A9A8A4E8905C5CAAFB1A6  U:	estbmbf012F96B1522A0000002.tif
776DF4069AD1914D9C37593E423BC0E4  U:	estbmbf012F96B1522A0000003.tif
95EC963E9C789B3502E1E2C85E505218  U:	estbmbf012F96B1522A0000004.tif
D5DD98F880A7204092EAA9355A4B558B  U:	estbmbf012F96B1522A0000005.tif
1A08F9B01904F3EF689B44093343AE2C  U:	estbmbf012F96B1522A0000006.tif
437DC62245852A01CCF4F2689F82920E  U:	estbmbf012F96B1522A0000007.tif
A4E7C76EC523F1E2799BE8CE049D28FC  U:	estbmbf012F96B1522A0000008.tif
177689553B9D9392AD6B72130EE1D22F  U:	estbmbf012F96B1522A0000009.tif
EB0F2F741428CF376909AB65BEF6659F  U:	estbmbf012F96B1522A0000010.tif
E992A84B8C8A1FEF3B94242403D5D84B  U:	estbmbf012F96B1522A0000011.tif

解决方案

The usual way to read several files in parallel is described at this answer and consists of read the first file via a for /F command and the other file(s) via redirected set /P command(s). For example:

@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
      )
   )
))

Note that the default for /F behavior is get the first token delimited by space, so "tokens=1 delims= " option is not necessary...

However, if the files are small you may use a simpler method:

@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
      )
   )
)

In this method a findstr /N "^" command is used to enumerate all lines in both files, so if %%F equ %%H command is used to synchronize (select the same) lines in both files. This method is not efficient, so it will take longer if the files are not small...

这篇关于行与批次的比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆