想从下面的批处理文件中的 machines 变量中删除重复的值:
@echo off
set machines=node1,node2,node3,node2,node4,node1,node7,node6,node4
预期输出:
node1,node2,node3,node4,node6,node7
答案 0 :(得分:1)
一种完成任务的方法:
for
迭代计算机并将它们设置为数组,:: Q:\Test\2018\09\20\SO_52417320.cmd
@Echo off & Setlocal EnableDelayedExpansion
Set machines=node1,node2,node3,node2,node4,node1,node7,node6,node4
:: clear array machine[], then fill it
For /f "tokens=1 delims==" %%M in ('Set machine[ 2^>Nul') do Set "%%M="
For %%M in (%machines%) do Set "machine[%%M]=%%M"
Set machine[
Echo:
Set "machines="
For /f "tokens=2 delims==" %%M in ('Set machine[') do Set "machines=!machines!,%%M"
Echo:%machines:~1%
示例输出:
> SO_52417320.cmd
machine[node1]=node1
machine[node2]=node2
machine[node3]=node3
machine[node4]=node4
machine[node6]=node6
machine[node7]=node7
node1,node2,node3,node4,node6,node7
答案 1 :(得分:0)
一种简单的方法(不会对 machines 变量进行排序,只会删除重复项):
@echo off& setlocal EnableDelayedExpansion
set machines=node1,node2,node3,node2,node4,node1,node7,node6,node4
set "tmac=,"& for %%a in (%machines%) do if "!tmac:,%%a,=!"=="!tmac!" set "tmac=!tmac!%%a,"
set machines=%tmac:~1,-1%