Want to remove duplicate values from comma separated variable using batch file

I have a batch file. Here I assigned some values to a variable. Want to remove duplicate values from the variable.

@echo off
set test=1,2,4,1,5,6,2,3


expected output: 1,2,3,4,5,6
deepak singlaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Pavel GushchinProgrammerCommented:
@ECHO OFF
set test=1,2,4,1,5,6,2,3
del tmp.txt >nul

for %%a in (%test%) do echo %%a >>tmp.txt

for /f "delims=" %%F in ('sort tmp.txt') do (
  set "curr=%%F"
  setlocal enabledelayedexpansion
  if "!prev!" neq "!curr!" echo !curr!
  endlocal
  set "prev=%%F"
)

del tmp.txt >nul

Open in new window

0
deepak singlaAuthor Commented:
Thanks @Pavel

Actually, I want to store the result in a variable with a comma separated instead to echo it. So that I will return the variable to the parent batch file.
eg: set result=1,2,3,4,5,6

I tried to store in a variable, but I am getting an error "Could Not Find C:\chef\tmp.txt"

Could you please help me to resolve it.
0
Pavel GushchinProgrammerCommented:
@ECHO OFF
set test=1,2,4,1,5,6,2,3
del tmp.txt >nul
del tmp_sorted.txt >nul

for %%a in (%test%) do echo %%a>>tmp.txt

for /f "delims=" %%F in ('sort tmp.txt') do (
  set "curr=%%F"
  setlocal enabledelayedexpansion
  if "!prev!" neq "!curr!" echo !curr! >>tmp_sorted.txt
  endlocal
  set "prev=%%F"
)

setlocal EnableDelayedExpansion
set "txt="
set input=tmp_sorted.txt
for /f "delims=" %%a in (%input%) do (
  set "txt=!txt!%%a,"
)
set "txt=!txt:~0,-1!"
>new.txt echo !txt!

del tmp.txt >nul
del tmp_sorted.txt >nul

Open in new window

0
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

deepak singlaAuthor Commented:
Thanks

I also found another way to resolve it

@ECHO off
SETLOCAL EnableDelayedExpansion
SET oldString=node1node2,node3,node4,node2,node3,node5
SET newstring=

FOR %%a IN ("%oldString:,=";"%") DO (
    IF NOT !test%%~a!==TRUE (
        SET test%%~a=TRUE
        IF "!newstring!"=="" (
                  SET newstring=%%~a
        ) ELSE (
            SET newstring=!newstring!,%%~a
        )
    )
)

ECHO Old String: !oldString!
ECHO New String: !newstring!
0
deepak singlaAuthor Commented:
Hi Pavel

I am getting below error from your latest batch script.

Could Not Find c:\chef\tmp.txt
Could Not Find c:\chef\tmp_sorted.txt

Open in new window

0
Pavel GushchinProgrammerCommented:
It's not a critical error... change it so:
@ECHO OFF
set test=1,2,4,1,5,6,2,3

if exist tmp.txt del tmp.txt
if exist tmp_sorted.txt del tmp_sorted.txt

for %%a in (%test%) do echo %%a>>tmp.txt

for /f "delims=" %%F in ('sort tmp.txt') do (
  set "curr=%%F"
  setlocal enabledelayedexpansion
  if "!prev!" neq "!curr!" echo !curr! >>tmp_sorted.txt
  endlocal
  set "prev=%%F"
)

setlocal EnableDelayedExpansion
set "txt="
set input=tmp_sorted.txt
for /f "delims=" %%a in (%input%) do (
  set "txt=!txt!%%a,"
)
set "txt=!txt:~0,-1!"
>new.txt echo !txt!

if exist tmp.txt del tmp.txt
if exist tmp_sorted.txt del tmp_sorted.txt

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.