rrayas
rrayas

Reputation: 53

batch file variable in a variable replacement

I am trying to write a batch file to build some war files based on a template war. I have an input list of names and want to parse the names into variable substrings. The substrings will not always be the same length fyi.

I know the end of the string and want to store the beginning of it in a variable. When i try to cut off the endstring, it cannot read it. It is the last line in this snippet. How can I use a variable here?

@echo off
setlocal EnableDelayedExpansion
(for /F "tokens=*" %%d in (themeCompiler/themeList.txt) do (
    echo %%d
    set dirname=%%d
        REM set environment
        if not "!dirname!"=="!dirname:DEV=!" (
            set env=DEV
        )
        if not "!dirname!"=="!dirname:TST=!" (
            set env=TST
        )
        if not "!dirname!"=="!dirname:PRD=!" (
            set env=PRD
        )
        echo !env!
        REM check which node to use
        if not "!dirname!"=="!dirname:admin=!" (
            set nodename=admin
        )
        if not "!dirname!"=="!dirname:portal=!" (
            set nodename=portal
        )
        REM build name of theme war
        set endString=-!env!-theme-!nodename!
        echo endString !endString!
        set vpdi=!dirname:LP5-=!
        set vpdi2=!vpdi:!endString!=!

From themeList.txt

LP5-CCA-TST-theme-portal
LP5-CCCO-PRD-theme-admin
LP5-CCCO-PRD-theme-portal
LP5-CCCO-TST-theme-admin
LP5-CCCO-TST-theme-portal
LP5-CCCS-DEV-theme-admin

Upvotes: 0

Views: 59

Answers (1)

user6811411
user6811411

Reputation:

Use the parsing for /f properly

@echo off
for /F "tokens=1-4* delims=-" %%A in (themeCompiler/themeList.txt) do (
    echo %%A-%%B-%%C-%%D-%%E
    echo 1st token = %%A
    echo 2nd token vpdi2 = %%B
    echo 3rd token env = %%C
    echo 4th token = %%D
    echo 5th token nodename = %%E
)    

Upvotes: 1

Related Questions