变量替换中的批处理文件变量

时间:2017-02-24 15:44:09

标签: batch-file

我正在尝试编写一个批处理文件来构建一些基于模板战争的war文件。我有一个名称的输入列表,并希望将名称解析为变量子串。子串的长度并不总是相同的。

我知道字符串的结尾,并希望将其开头存储在变量中。当我试图切断辫子时,它无法读取它。这是此代码段中的最后一行。我怎样才能在这里使用变量?

@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!=!

来自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

1 个答案:

答案 0 :(得分:1)

正确使用解析for /f

@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
)