【问题标题】:how to concat all values of for loop in one variable in batch script如何在批处理脚本的一个变量中连接for循环的所有值
【发布时间】:2013-11-07 12:12:28
【问题描述】:

我正在为用户提供在命令行上选择某些属性的选项。用户想要选择逗号分隔的列表。所以用户会看到这样的东西

  1. 道具1
  2. 道具2
  3. Prop3

所以用户可以输入 1,3 并按回车键,用户将创建 1 和 3 个数据库。 但是这些 Prop1、Prop2、Prop3 具有唯一的名称和 id,我在同一个批处理脚本中给出了属性,我想根据用户选择的选项连接所有这些并传递给我的构建脚本。

Example of properties:
SET propertyID1=11
SET propertyID2=12
SET propertyID3=13
SET propertyID4=14
SET propertyID5=15

SET propertyIDPref1=011
SET propertyIDPref2=012
SET propertyIDPref3=013
SET propertyIDPref4=014
SET propertyIDPref4=015

SET propertyName1=A
SET propertyName2=B
SET propertyName3=C
SET propertyName4=D
SET propertyName5=E

call :parse "%M%"

pause
goto :eof

:parse
setlocal
set list=%~1
for /F "tokens=1* delims=," %%f in ("%list%") do (
    if not "%%f" == "" call :getLineNumber %%f
    if not "%%g" == "" call :parse "%%g"
    if "%%g" == "" call :printPropertiesConcatenation
)

endlocal

goto :eof

:printPropertiesConcatenation
setLocal
    echo "Gr8 " %buildPropertiesList%
endLocal
goto :eof
:getLineNumber
setlocal
echo file name is %1
set propID = 'propertyID'%1%

set propStr=propertyID
set propID=%1
set newvar=!%propStr%%propID%!
echo %newvar%

set propNameStr=propertyName
set propName=!%propNameStr%%propID%!
echo %propName%

set propIDPrefix=propertyIDPref
set propIDPrefixWithPrefix=!%propIDPrefix%%propID%!
echo %propIDPrefixWithPrefix%

set buildPropertiesList=%buildPropertiesList%','!%propIDPrefix%%propID%!

goto :eof

我可以在迭代时从这些属性中读取正确的值,并在循环中使用 echo 查看它。 但我想连接这些值并将其传递给构建脚本。但我无法在一个变量中查看所有连接值。

我想要这样的东西。 最后设置 propNames = A,C 并设置 propIds = 11,13 以便我可以传递 propNames 和 PropIds。

从上面的代码我希望 buildPropertiesList 有 011,013

有什么办法

【问题讨论】:

    标签: batch-file


    【解决方案1】:

    这可能对你有用:

    @ECHO OFF &SETLOCAL ENABLEDELAYEDEXPANSION
    SET propertyID1=11
    SET propertyID2=12
    SET propertyID3=13
    SET propertyID4=14
    SET propertyID5=15
    
    SET propertyIDPref1=011
    SET propertyIDPref2=012
    SET propertyIDPref3=013
    SET propertyIDPref4=014
    SET propertyIDPref4=015
    
    SET propertyName1=A
    SET propertyName2=B
    SET propertyName3=C
    SET propertyName4=D
    SET propertyName5=E
    
    for /f "tokens=1*delims==" %%a in ('set "property"') do (
        set "apx=%%a"
        set "apx=!apx:*property=!"
        for /f "delims=0123456789" %%b in ("!apx!") do set "apx=%%b"
        if defined props!apx! (
            call set "props!apx!=%%props!apx!%%,%%b"
        ) else (
            set "props!apx!=%%b"
        )
    )
    set "props"
    

    【讨论】:

      【解决方案2】:

      借助数组,您可以用更少的代码解决这个问题。例如:

      @echo off
      setlocal EnableDelayedExpansion
      
      rem Create the arrays of properties:
      set /A i=1, ID=11, IDPref=1011
      for %%a in (A B C D E) do (
         SET propertyID[!i!]=!ID!
         SET propertyIDPref[!i!]=!IDPref:~-3!
         SET propertyName[!i!]=%%a
         set /A i+=1, ID+=1, IDPref+=1
      )
      
      echo Properties menu:
      echo/
      for /L %%i in (1,1,5) do echo %%i. !propertyName[%%i]!
      echo/
      
      set /P "M=Enter properties list: "
      call :parse "%M%"
      echo Names: !propNames:~0,-1!
      echo Ids:   !propIds:~0,-1!
      echo List:  !propList:~0,-1!
      
      pause
      goto :eof
      
      :parse
      set "propNames="
      set "propIds="
      set "propList="
      for %%i in (%~1) do (
         set "propNames=!propNames!!propertyName[%%i]!,"
         set "propIds=!propIds!!propertyID[%%i]!,"
         set "propList=!propList!!propertyIDPref[%%i]!,"
      )
      exit /B
      

      【讨论】:

        猜你喜欢
        • 2016-05-17
        • 2011-12-12
        • 1970-01-01
        • 2012-07-11
        • 2016-07-02
        • 2020-11-13
        • 2014-06-24
        • 1970-01-01
        • 2023-04-02
        相关资源
        最近更新 更多