acroniks
acroniks

Reputation: 109

Unset variable value assigned within if statement inside a nested for loop

I am trying to make one batch file to read parameters from the file and pass it to ant for deployment.

I have made the below batch file:

setlocal enabledelayedexpansion
echo on

IF EXIST "D:\testfile1.txt" (
    echo Do one thing

    set string=
    set /p string=< D:\testfile1.txt
    echo value taken from file is %string

    for /f "tokens=2,4,6 delims==:" %%G IN ("%string%") DO (

        echo %%G %%H %%I
        set env=%%G
        set dom=%%H
        set com=%%I
        echo ENV !env!
        echo DOM !dom!
        echo COM !com!


        cd D:\kpn_eai\EAI_FIXED\branches\kpn_eai_fixed\fixed\build\scripts

        %ANT_HOME%\bin\xanteai deploy %%G %%H %%I

    )
) ELSE (
    echo Do another thing
)

endlocal

In testfile1.txt I have parameters in below format:

Environment=Env_Name1:Domain=Domain_Name1:Component=Component_name1

Parameters are different deployments. When I am running the above code it is giving below output

D:\>echo off
Do one thing Ant home is C:\tibco\ant\apache-ant-1.9.13
value taken from file is Environment=Env_name1:Domain=Domain_Name1:Component=Component_name1
Env_name1 Domain_Name1 Component_name1
ENV Env_name1
DOM Domain_Name1
COM Component_name1

Deployments starts after this.

The issue I am facing is when I run this code for different parameters (in testfile1.txt) the values of ENV, DOM and COM remains same regardless of any parameters read from testfile1.txt.

Can anyone help me to correct this code and let me know how can I just assign the values read from file to a variable and pass it to ant for deployment?

NOTE:- This batch file will be placed in scheduler which will check for testfile1.txt after every 5 minutes, when it finds that file deployment process gets triggered. Thus I have included if condition to check availability of file.

Upvotes: 0

Views: 78

Answers (2)

Compo
Compo

Reputation: 38613

There really shouldn't be any need to be setting or echoing everything as you go, so why not just do something like this instead:

@Echo Off
If Exist "testfile1.txt" (Set /P "string="<"testfile1.txt"
    SetLocal EnableDelayedExpansion
    Echo value taken from file is !string!
    For /F "Tokens=2,4,6 Delims==:" %%A In ("!string!") Do (
        CD /D "D:\kpn_eai\EAI_FIXED\branches\kpn_eai_fixed\fixed\build\scripts"
        "%ANT_HOME%\bin\xanteai" deploy %%A %%B %%C)
    EndLocal) Else Echo Do another thing

Upvotes: 1

user6811411
user6811411

Reputation:

You could first split the read line at the colons with a simple for and
obtain the variable names env,dom,com from the content.

Here preceeded with an underscore for easier output.

:: Q:\Test\2019\03\08\SO_55061545_.cmd
@echo off
setlocal enabledelayedexpansion
set "FileIn=D:\testfile1.txt"

IF not EXIST "%FileIn%" (
    echo Do another thing
    Goto :somewhere
)

echo Do one thing
set "string="
set /p "string="<"%FileIn%"
echo value taken from file is %string%

for %%A in ("%string::=" "%") do for /f "tokens=1* delims==" %%B IN ("%%~A") DO (
    echo %%A %%B %%C
    set var=%%B
    set "_!var:~0,3!=%%C"
)
set _

:: cd D:\kpn_eai\EAI_FIXED\branches\kpn_eai_fixed\fixed\build\scripts
:: %ANT_HOME%\bin\xanteai deploy %_env% %_dom% %_com%

endlocal

:somewhere

>  SO_55061545_.cmd
Do one thing
value taken from file is Environment=Env_Name1:Domain=Domain_Name1:Component=Component_name1
"Environment=Env_Name1" Environment Env_Name1
"Domain=Domain_Name1" Domain Domain_Name1
"Component=Component_name1" Component Component_name1
_Com=Component_name1
_Dom=Domain_Name1
_Env=Env_Name1

Upvotes: 0

Related Questions