首页 > 解决方案 > 通过批处理文件创建没有重复的元素集合

问题描述

我在for生成结果数组的自定义工具上运行多个循环。

我需要将它们展平为一个没有重复的集合。

我的第一个想法是创建键是项目的数组,见下文。但这是之后迭代的噩梦......:

set apps=
for /d %%s in (".\manifests\*.xml") do (
    for /f "tokens=*" %%g in ('xml sel -t -v "\apps\app" %cd%\manifests\%%s') do (
            echo Found app - %%g [inside %%s]
            rem First idea below:
            set apps[%%g]=true
        )
)
rem How to iterate apps afterwards?

标签: batch-filefor-loopiteration

解决方案


刚刚搞定:

set apps=
set apps_defined=

for /d %%s in (".\manifests\*.xml") do (
    for /f "tokens=*" %%g in ('xml sel -t -v "\apps\app" %cd%\manifests\%%s') do (
            echo Found app - %%g [inside %%s]

            if not "apps_defined[%%g]"=="true" (
                 if not "!apps!"=="" set apps=!apps!,
                 set apps=!apps!%%g
            )

            set apps_defined[%%g]=true
        )
)

推荐阅读