Avatar of deepak singla
deepak singla

asked on 

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
Windows Batch

Avatar of undefined
Last Comment
Pavel Gushchin
Avatar of Pavel Gushchin
Pavel Gushchin

@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

Avatar of deepak singla
deepak singla

ASKER

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.
@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

Avatar of deepak singla
deepak singla

ASKER

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!
Avatar of deepak singla
deepak singla

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of Pavel Gushchin
Pavel Gushchin

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Windows Batch
Windows Batch

Batch files are text files containing a script of commands that are executed by the command interpreter on DOS, OS/2 and Windows systems. Most commonly, they are used to perform a series of functions that are repeated -- copying a set of files created daily with one step, for example.

13K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo