1

I'm giving user options to select certain properties on command line. User want to select comma separated list. So user will see something like this

  1. Prop1
  2. Prop2
  3. Prop3

So user can give 1,3 and hit enter and user will get 1 and 3 databases created. But these Prop1, Prop2, Prop3 has unique names and ids, which I've given in same batch script as properties and I want to concat all those depending on options user has selected and pass to my build script.

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

I can read correct values from these properties on iteration and see it using echo in loop. But I want to concat these values and pass it to build script. But I don't get way to see all concatenated values in one variable.

I want something like this. set propNames = A,C and set propIds = 11,13 in the end so that I can pass propNames and PropIds.

From above code I want buildPropertiesList to have 011,013

Is there any way

2 Answers 2

1

this might work for you:

@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"
Sign up to request clarification or add additional context in comments.

Comments

1

You may solve this problem with less code with the aid of arrays. For example:

@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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.