Page 1 of 1

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

Posted: 22 Sep 2021 12:23
by darioit
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

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

Posted: 22 Sep 2021 15:09
by Squashman
Hmm. Going to assume you want help getting this to actually work. Just looking at it I see a lot of failure points.

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

Posted: 22 Sep 2021 22:41
by darioit
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

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

Posted: 23 Sep 2021 22:44
by darioit
Where did you see "lot of failure points" ?

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

Posted: 24 Sep 2021 06:22
by ShadowThief
Lack of quotes, mostly.

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

Posted: 24 Sep 2021 23:27
by darioit
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"


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

Posted: 25 Sep 2021 08:29
by atfon
One small tip I've been given: Always quote assignments.
viewtopic.php?f=3&t=10137