0

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
rrayas
  • 43
  • 8
  • An example set of strings would make helping a lot easier... – aschipfl Feb 24 '17 at 15:49
  • You cannot do string substitution with delayed expansion like that. The replacement string needs to be normally expanded. – Squashman Feb 24 '17 at 16:25
  • How do you do that? – rrayas Feb 24 '17 at 16:35
  • I suggest you to read [this answer](http://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990). Although the topic is different, the _concept_ involved is the same: `call set vpdi2=%%vpdi:!endString!=%%` or `for /F %%x in ("!endString!") do set vpdi2=!vpdi:%%x=!` – Aacini Feb 24 '17 at 17:24

1 Answers1

1

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
)