Batch file 如何在批处理脚本中将for循环的所有值合并到一个变量中
我给用户在命令行上选择某些属性的选项。用户希望选择逗号分隔列表。所以用户会看到这样的东西Batch file 如何在批处理脚本中将for循环的所有值合并到一个变量中,batch-file,Batch File,我给用户在命令行上选择某些属性的选项。用户希望选择逗号分隔列表。所以用户会看到这样的东西 建议1 建议2 建议3 所以用户可以给出1,3,然后按enter键,用户将得到创建的1和3个数据库。 但这些Prop1、Prop2、Prop3具有唯一的名称和ID,我在与属性相同的批处理脚本中给出了这些名称和ID,并且我希望根据用户选择的选项来确定所有这些名称和ID,并传递给我的构建脚本 Example of properties: SET propertyID1=11 SET propertyID2=12
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,并设置PROPID=11,13,以便我可以传递propNames和PROPID
从上面的代码中,我希望buildPropertiesList具有011013
是否有任何方法可以满足您的需要:
@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"
借助数组,您可以用更少的代码来解决这个问题。例如:
@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