Find unique values from a comma separated variables using a batch file.

deepak singla
deepak singla used Ask the Experts™
on
I have two variables A and B, having values in a comma separated format. I am trying to find out the result, which is present in A, but not in B using a batch file.
@echo off
set A=1,2,3,4
set B=3,4,5,6

expected result: 1,2
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
Like this, for example:
@echo off
setlocal

set A=1,2,3,4
set B=3,4,5,6
call :GetUnique result "%A%" "%B%"
echo Result: %result%
 
REM ===========================================================================
REM Function only below this point!
REM ===========================================================================
goto :eof

:GetUnique [ref]<return variable> <String 1> <String 2>
setlocal enabledelayedexpansion
set unique=
for %%a in (%~3) do set __GetUnique[%%a]=1
for %%a in (%~2) do (if not defined __GetUnique[%%a] set unique=!unique!,%%a)
if not "%unique%"=="" set unique=%unique:~1%
endlocal&set %1=%unique%

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial