We help IT Professionals succeed at work.

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

deepak singla
on
101 Views
Last Modified: 2018-09-24
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
Comment
Watch Question

Pavel GushchinProgrammer
CERTIFIED EXPERT

Commented:
@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

Author

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.
Pavel GushchinProgrammer
CERTIFIED EXPERT

Commented:
@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

Author

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!

Author

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

Programmer
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.