Compare two lists of elements, remove same and recycle for next run

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Compare two lists of elements, remove same and recycle for next run

#1 Post by darioit » 22 Sep 2021 12:23

Hi everyone,
this is a small contribution for the community to thank all the time I have been helped, I hope it will be useful to someone, and I hope don't made any mistakes lol

Purpose: compare two list of elements to remove all duplicate in each list, also item remain will be used for next compare with a new file

Code: Select all

@echo off
setlocal EnableDelayedExpansion

if exist %1 type %1 >> Recycle_%1
sort.exe -u Recycle_%1 > %1 && del /q Recycle_%1
if exist %2 type %2 >> Recycle_%2
sort.exe -u Recycle_%2 > %2 && del /q Recycle_%2

for /f %%i in ("%1") do set size_%1=%%~zi
for /f %%i in ("%2") do set size_%2=%%~zi

if %size_%2% equ 0 (
	type %1 > Recycle_%1
	) else if %size_%1% neq 0 (
		findstr /livg:%1 %2 > Recycle_%2
		) else (type %1 > Recycle_%1)

if %size_%1% equ 0 (
	type %2 > Recycle_%2
	) else if %size_%2% neq 0 (
		findstr /livg:%2 %1 > Recycle_%1
		) else (type %2 > Recycle_%2)

del /q %1
del /q %2

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Compare two lists of elements, remove same and recycle for next run

#2 Post by Squashman » 22 Sep 2021 15:09

Hmm. Going to assume you want help getting this to actually work. Just looking at it I see a lot of failure points.

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: Compare two lists of elements, remove same and recycle for next run

#3 Post by darioit » 22 Sep 2021 22:41

Of course I really appreciate any bug fix or improvement, I chose to use this approach because the input file or the recycle file can be empty, and using other methods the comparison always failed, and also Recycle file must be always present at the end of program

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: Compare two lists of elements, remove same and recycle for next run

#4 Post by darioit » 23 Sep 2021 22:44

Where did you see "lot of failure points" ?

ShadowThief
Expert
Posts: 1159
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Compare two lists of elements, remove same and recycle for next run

#5 Post by ShadowThief » 24 Sep 2021 06:22

Lack of quotes, mostly.

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: Compare two lists of elements, remove same and recycle for next run

#6 Post by darioit » 24 Sep 2021 23:27

ok thank you now is better?

Code: Select all

@echo on
setlocal EnableDelayedExpansion

type nul >> "Recycle_%1"
type nul >> "Recycle_%2"

if exist "%1" type "%1" >> "Recycle_%1"
sort.exe -u "Recycle_%1" > "%1" && del /q "Recycle_%1"
if exist "%2" type "%2" >> "Recycle_%2"
sort.exe -u "Recycle_%2" > "%2" && del /q "Recycle_%2"

for /f %%i in ("%1") do set size_%1=%%~zi
for /f %%i in ("%2") do set size_%2=%%~zi

if %size_%2% equ 0 (
	type "%1" > "Recycle_%1"
	) else if %size_%1% neq 0 (
		findstr /livg:%1 "%2" > "Recycle_%2"
		) else (type "%1" > "Recycle_%1")

if %size_%1% equ 0 (
	type "%2" > "Recycle_%2"
	) else if %size_%2% neq 0 (
		findstr /livg:%2 "%1" > "Recycle_%1"
		) else (type "%2" > "Recycle_%2")

del /q "%1"
del /q "%2"


atfon
Posts: 178
Joined: 06 Oct 2017 07:33

Re: Compare two lists of elements, remove same and recycle for next run

#7 Post by atfon » 25 Sep 2021 08:29

One small tip I've been given: Always quote assignments.
viewtopic.php?f=3&t=10137

Post Reply