Compare text files in FOR loop

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Boombox
Posts: 80
Joined: 18 Oct 2012 05:51

Compare text files in FOR loop

#1 Post by Boombox » 31 Oct 2012 16:42

.
I'm trying to create a loop that, line by line, compares 2 files, sending the difference to a 3rd

Code: Select all

FOR /F %%G IN (ALPHA.TXT) DO TYPE BETA.TXT | FIND /I /V "%%G">GAMMA.TXT


Can it be done this way?

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Compare text files in FOR loop

#2 Post by abc0502 » 31 Oct 2012 17:47

This is a long way i made by using codes snippets i had:

Code: Select all

@echo off

set "file1=list1.txt"
set "file2=list2.txt"
set "finalfile=all.txt"
set "separator=:"

(
   For /F "delims=" %%a in (%file1%) Do (
      Setlocal EnableDelayedExpansion
         Set /p line=
         Echo.%%a!separator!!line!
      Endlocal
   )
)<%file2%>>%finalfile%

For /F "tokens=1-2 delims=:" %%A in (all.txt) Do (
   IF not "%%A" == "%%B" Echo %%B>diff.txt
)

pause

If first combine the two lists in this form
line1:line1
line2:line2

then compare the first token with the second and if they not equal it output the defferant from token 2 to the diff.txt file


Edit:
try running your code that way:
FOR /F %%G IN (ALPHA.TXT) DO TYPE BETA.TXT | FIND /I "%%G">>GAMMA.TXT

without the /v it will make a list of all equal lines and won't write the differences, but the adding /V make it list all the contents all after each other

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Compare text files in FOR loop

#3 Post by foxidrive » 31 Oct 2012 20:07

This was asked recently - here's what I came up with.

Launch with: comparediff.bat "file1.txt" "file2.txt"


Code: Select all

@echo off
set "file1=%~1"
set "file2=%~2"
del "result.compare.txt" 2>nul
set c=0
setlocal enabledelayedexpansion
<%file2% (for /f "delims=" %%a in ('type "%file1%"') do (
 set /a c=c+1
 set b=
 set /p b=
   
      if not "%%a"=="!b!" (
        echo line !c! has a mismatch
        >> "result.compare.txt" echo %file1% line !c!: %%a
        >> "result.compare.txt" echo %file2% line !c!: !b!
      )
)
)
if exist "result.compare.txt" (
echo check "result.compare.txt" for mismatches
) else (
echo no mismatches found
)

pause

Boombox
Posts: 80
Joined: 18 Oct 2012 05:51

Re: Compare text files in FOR loop

#4 Post by Boombox » 31 Oct 2012 22:17

.
Great! Both are good, helpful scripts. Thanks guys.

I tried to bastardize your efforts but still couldn't get what I wanted.
I'm comparing 2 tasklist.txt's & want output of a 3rd txt file, containing the differing exe names (no duplicates)

Create tasklist.txt, wait xyz seconds, create tasklist(2).txt, compare the two, if exist in Output.txt goto start, else echo to Output.txt, goto start

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Compare text files in FOR loop

#5 Post by foxidrive » 31 Oct 2012 23:11

That's a different task altogether.

It helps enormously to describe what you actually need to do from the outset.

What command line are you using with tasklist? Do you only want the program name?

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Compare text files in FOR loop

#6 Post by abc0502 » 31 Oct 2012 23:45

if exist in Output.txt goto start, else echo to Output.txt, goto start

sorry, I didn't understand what do you mean ?

Compare Code:

Code: Select all

(
   For /F "delims=" %%a in (file1.txt) Do (
      Setlocal EnableDelayedExpansion
         Set /p line=
         if "%%a" NEQ "!line!" Echo !line!
      Endlocal
   )
)<"file2.txt"

Boombox
Posts: 80
Joined: 18 Oct 2012 05:51

Re: Compare text files in FOR loop

#7 Post by Boombox » 01 Nov 2012 06:09

.
That's a different task altogether.
It helps enormously to describe what you actually need to do from the outset.
What command line are you using with tasklist? Do you only want the program name?


* I was using tasklist to avoid the carriage return of WMIC

@Foxi, Sorry mate I thought I was clear yesterday...

Although you advised against it, ('You'll break things') I'm attempting to stop all new process' on a system..

I want to specify a directory of allowed .exe's (My USB)
Couple that with the currently running .exe's /windows stuff (Maybe even a 'Fresh Install Tasklist-List')
And ban all other .exe's


@ABC, this is the order of the commands (as I imagined it written)

1: Create tasklist.txt
2: wait xyz seconds
3: create tasklist(2).txt
4: compare the two
5: if exe name already exists in Output.txt, goto loop
6: else, echo name to Output.txt & goto loop

No duplicate entries should be found in the final (3rd) txt file!

I guess I'm nearly there though; after reading the other post. Thanks a bunch guys.
Damn FOR construct kept me up all night :(

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Compare text files in FOR loop

#8 Post by foxidrive » 01 Nov 2012 21:48

Boombox wrote:.
@Foxi, Sorry mate I thought I was clear yesterday...

Although you advised against it, ('You'll break things') I'm attempting to stop all new process' on a system..


This was a different thread. Next time I'll put on my psychic glasses and have a read. :D

Post Reply