Is any way to compare these two txt files and get an message if the list on two are different, display the difference?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

Is any way to compare these two txt files and get an message if the list on two are different, display the difference?

#1 Post by goodywp » 06 Apr 2018 15:15

Hi I have two txt files and most time they are equal in terms of the list, but one just has some partial

text1.txt is just a list as below
  • 500006011000.S3S
    500007011000.S3S
    500008011000.S3S
    500009011000.S3S
    500010011000.S3S
    500012011000.S3S
    500014011000.S3S
    500016011000.S3S
    500134010100.S3S
    500028011201.S3S
    500129010200.S3S
    500015011800.S3S
    500142010100.S3S
    500144010100.S3S
text2.txt has some words on top then the list, but the list is just first 6 digits same as text1.txt which we can compare with each other..
  • The following is a list of mockup required to package with TSA for mockup testing.

    From \T501-08680-0103\ Application_Signing\Signed_Schemes\MockupSigned_T3
    • 500006*.S3S.mockup ;TlvAESCiph
    • 500007*.S3S.mockup ;TlvCipherDa
    • 500008*.S3S.mockup ;TlvDukptTDes
    • 500009*.S3S.mockup ;TlvFree
    • 500010*.S3S.mockup ;TlvHMAC
    • 500014*.S3S.mockup ;TlvLoadKey
    • 500016*.S3S.mockup ;TlvMacTDes
    • 500134*.S3S.mockup ;GENERATE_AES
    • 500028*.S3S.mockup ;LOAD_E2EE
    • 500129*.S3S.mockup ;GENERATE_DES_EXPORT_SUPPLIED_RSA
    • 500142*.S3S.mockup ;MOVEKEY
    • 500144*.S3S.mockup ;OWFTR31
Basically text1.txt has two more lists than text2.txt
500012011000.S3S
500015011800.S3S

Is any way to compare these two give the message that the two files has different list, or not equal...
Last edited by goodywp on 09 Apr 2018 08:59, edited 1 time in total.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Is any way to compare these two txt file and get an message if the list on two are different?

#2 Post by Compo » 07 Apr 2018 04:52

You could just use the FindStr command

Code: Select all

@Echo Off
SetLocal EnableDelayedExpansion
For /F "UseBackQ Delims=" %%A In ("text1.txt") Do (Set "_=%%A"
	FindStr /IC:"!_:~,6!" "text2.txt">Nul||Echo %%A)
Pause

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Is any way to compare these two txt file and get an message if the list on two are different?

#3 Post by Aacini » 07 Apr 2018 17:34

The phrase "compare two files and give the message that has different lists" doesn't makes sense in this case... We can show list elements in text1.txt that does not exist in text2.txt:

Code: Select all

@echo off
setlocal

(set LF=^
%Empty line%
)

(<nul cmd /V /C "for /F %%a in (text2.txt) do @set "m=%%a"&set /P "=!m:~0,6!!LF!"")|findstr /V /B /G:/ text1.txt
If you want to show list elements in text2.txt that does not exist in text1.txt, you must repeat previous code exchanging the two files...

If you just want to show a message if the list are different (not show the individual elements) use this code:

Code: Select all

(<nul cmd /V /C "for /F %%a in (text2.txt) do @set "m=%%a"&set /P "=!m:~0,6!!LF!"")|findstr /V /B /G:/ text1.txt>nul
if %errorlevel% equ 0 echo Has different list
Antonio

goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

Re: Is any way to compare these two txt file and get an message if the list on two are different?

#4 Post by goodywp » 09 Apr 2018 08:21

Compo wrote:
07 Apr 2018 04:52
You could just use the FindStr command

Code: Select all

@Echo Off
SetLocal EnableDelayedExpansion
For /F "UseBackQ Delims=" %%A In ("text1.txt") Do (Set "_=%%A"
	FindStr /IC:"!_:~,6!" "text2.txt">Nul||Echo %%A)
Pause
Thank you! I tried and got the expected.
Here are the expected results, just copied two piece of results as example

...................
this one shows same
C:\Users\goodywp>(
Set "_=500129010200.S3S"
FindStr /IC:"!_:~,6!" "text2.txt" 1>Nul || Echo 500129010200.S3S
)

and this one shows different, so at the end display the name of this list as 500015011800.S3S
C:\Users\goodywp>(
Set "_=500015011800.S3S"
FindStr /IC:"!_:~,6!" "text2.txt" 1>Nul || Echo 500015011800.S3S
)
500015011800.S3S


................................
My question will be how I can echo the difference of the list into a new file, like this case
500012011000.S3S
500015011800.S3S

will be output into a new file...
OK I figure it out now and got this works perfectly

Code: Select all

@echo off
if exist missinglist.txt (del missinglist.txt)
SetLocal EnableDelayedExpansion
For /F "UseBackQ Delims=" %%A In ("text1.txt") Do (Set "_=%%A"
	FindStr /IC:"!_:~,6!" "text2.txt">Nul||Echo %%A>>missinglist.txt )
Thanks
Last edited by goodywp on 09 Apr 2018 09:59, edited 6 times in total.

goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

Re: Is any way to compare these two txt file and get an message if the list on two are different?

#5 Post by goodywp » 09 Apr 2018 08:28

Aacini wrote:
07 Apr 2018 17:34
The phrase "compare two files and give the message that has different lists" doesn't makes sense in this case... We can show list elements in text1.txt that does not exist in text2.txt:

Code: Select all

@echo off
setlocal

(set LF=^
%Empty line%
)

(<nul cmd /V /C "for /F %%a in (text2.txt) do @set "m=%%a"&set /P "=!m:~0,6!!LF!"")|findstr /V /B /G:/ text1.txt
I tried this piece of code and give me the whole list of text1.txt not the two list which is not in the text2.txt

If you want to show list elements in text2.txt that does not exist in text1.txt, you must repeat previous code exchanging the two files...

I tried the above code and just switch the file names, and give me the whole text of text2.txt, not the missing part.

If you just want to show a message if the list are different (not show the individual elements) use this code:

Code: Select all

(<nul cmd /V /C "for /F %%a in (text2.txt) do @set "m=%%a"&set /P "=!m:~0,6!!LF!"")|findstr /V /B /G:/ text1.txt>nul
if %errorlevel% equ 0 echo Has different list
Antonio
This one works for me and I got the message showing they are different. However, when I made the lists same in these two text file, still echo %errorlevel% 0... any clue?

Would you please make that your first piece of code also work as expected, that is displaying two different list, like this case
500012011000.S3S
500015011800.S3S

Thanks

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Is any way to compare these two txt file and get an message if the list on two are different?

#6 Post by Aacini » 09 Apr 2018 11:14

goodywp wrote:
09 Apr 2018 08:28
I tried this piece of code and give me the whole list of text1.txt not the two list which is not in the text2.txt
It works here.

text1.txt:

Code: Select all

500006011000.S3S
500007011000.S3S
500008011000.S3S
500009011000.S3S
500010011000.S3S
500012011000.S3S
500014011000.S3S
500016011000.S3S
500134010100.S3S
500028011201.S3S
500129010200.S3S
500015011800.S3S
500142010100.S3S
500144010100.S3S
text2.txt:

Code: Select all

500006*.S3S.mockup	;TlvAESCiph
500007*.S3S.mockup	;TlvCipherDa
500008*.S3S.mockup	;TlvDukptTDes
500009*.S3S.mockup	;TlvFree
500010*.S3S.mockup	;TlvHMAC
500014*.S3S.mockup	;TlvLoadKey
500016*.S3S.mockup	;TlvMacTDes
500134*.S3S.mockup	;GENERATE_AES
500028*.S3S.mockup	;LOAD_E2EE
500129*.S3S.mockup	;GENERATE_DES_EXPORT_SUPPLIED_RSA
500142*.S3S.mockup	;MOVEKEY
500144*.S3S.mockup	;OWFTR31
output:

Code: Select all

500012011000.S3S
500015011800.S3S
I invite you to copy the files I used and test the code again. Please, note that you did NOT posted the data files between code tags, so I don't know what is the real format of your files!

Antonio

goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

Re: Is any way to compare these two txt file and get an message if the list on two are different?

#7 Post by goodywp » 09 Apr 2018 11:50

Aacini wrote:
09 Apr 2018 11:14
goodywp wrote:
09 Apr 2018 08:28
I tried this piece of code and give me the whole list of text1.txt not the two list which is not in the text2.txt
It works here.

text1.txt:

Code: Select all

500006011000.S3S
500007011000.S3S
500008011000.S3S
500009011000.S3S
500010011000.S3S
500012011000.S3S
500014011000.S3S
500016011000.S3S
500134010100.S3S
500028011201.S3S
500129010200.S3S
500015011800.S3S
500142010100.S3S
500144010100.S3S
text2.txt:

Code: Select all

500006*.S3S.mockup	;TlvAESCiph
500007*.S3S.mockup	;TlvCipherDa
500008*.S3S.mockup	;TlvDukptTDes
500009*.S3S.mockup	;TlvFree
500010*.S3S.mockup	;TlvHMAC
500014*.S3S.mockup	;TlvLoadKey
500016*.S3S.mockup	;TlvMacTDes
500134*.S3S.mockup	;GENERATE_AES
500028*.S3S.mockup	;LOAD_E2EE
500129*.S3S.mockup	;GENERATE_DES_EXPORT_SUPPLIED_RSA
500142*.S3S.mockup	;MOVEKEY
500144*.S3S.mockup	;OWFTR31
output:

Code: Select all

500012011000.S3S
500015011800.S3S
I invite you to copy the files I used and test the code again. Please, note that you did NOT posted the data files between code tags, so I don't know what is the real format of your files!

Antonio
Thanks Antonio! Yes I copied your format and works. But it there is some format, like this below for text2.txt, not working. Anyway, the above code from compo is already working as expected. Thanks again!

Code: Select all

From \T501-08680-0103\ Application_Signing\Signed_Schemes\MockupSigned_T3
•	500006*.S3S.mockup	;TlvAESCiph
•	500007*.S3S.mockup	;TlvCipherDa
•	500008*.S3S.mockup	;TlvDukptTDes
•	500009*.S3S.mockup	;TlvFree
•	500010*.S3S.mockup	;TlvHMAC
•	500014*.S3S.mockup	;TlvLoadKey
•	500016*.S3S.mockup	;TlvMacTDes
•	500134*.S3S.mockup	;GENERATE_AES
•	500028*.S3S.mockup	;LOAD_E2EE
•	500129*.S3S.mockup	;GENERATE_DES_EXPORT_SUPPLIED_RSA
•	500142*.S3S.mockup	;MOVEKEY
•	500144*.S3S.mockup 	;OWFTR31


Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Is any way to compare these two txt files and get an message if the list on two are different, display the differenc

#8 Post by Aacini » 09 Apr 2018 20:04

Just a small adjustment in the code is needed:

Code: Select all

@echo off
setlocal

(set LF=^
%Empty line%
)

(<nul cmd /V /C "for /F "skip=1 tokens=2" %%a in (text2.txt) do @set "m=%%a"&set /P "=!m:~0,6!!LF!"")|findstr /V /B /G:/ text1.txt
Antonio

goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

Re: Is any way to compare these two txt files and get an message if the list on two are different, display the differenc

#9 Post by goodywp » 10 Apr 2018 08:08

Aacini wrote:
09 Apr 2018 20:04
Just a small adjustment in the code is needed:

Code: Select all

@echo off
setlocal

(set LF=^
%Empty line%
)

(<nul cmd /V /C "for /F "skip=1 tokens=2" %%a in (text2.txt) do @set "m=%%a"&set /P "=!m:~0,6!!LF!"")|findstr /V /B /G:/ text1.txt
Antonio
Thanks Antonio! Yes it works!
I added >>missinglist.txt at the end to echo out the results

Code: Select all

@Echo Off
if exist missinglist.txt (del missinglist.txt)
setlocal
(set LF=^
%Empty line%
)
(<nul cmd /V /C "for /F "skip=1 tokens=2" %%a in (text2.txt) do @set "m=%%a"&set /P "=!m:~0,6!!LF!"")|findstr /V /B /G:/ text1.txt >>missinglist.txt
Could you please explain a little more on "skip=1 tokens=2" ?
Thanks again!

Post Reply