0

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.

4
  • 2
    Why do you think only env/dom/com need the exclamation marks inside a code block? This applies also to the var !string! Simply reverse your if logic If not exist and exit the batch file then, so no code block is neccessary. Commented Mar 8, 2019 at 11:05
  • @LotPings I wouldn't say that reversing the condition is a good advise without knowing a context. OP could want to do "another thing" instead of exit. Commented Mar 8, 2019 at 11:15
  • Your provided output is not the result of the batch file you've posted! Commented Mar 8, 2019 at 11:19
  • @montonero Well, 1st choice is to properly use delayedexpansion. 2nd you are right, I just wanted to circumvent a goto if possible, but that's up to OP. Commented Mar 8, 2019 at 11:22

2 Answers 2

1

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

3 Comments

setting EnableDelayedExpansion inside if statement will only have its scope inside if block only right?
in my code after removing all the echoes which I used as marker to keep a track of values, my code looks the same inspite I didn't set EnableDelayedExpansion inside if statement
Yes, because I read your requirment as 'how to assign the values to pass to ant'. Once they've been passed, you don't need them any more, so the EndLocal removes them from the environment. Oh and BTW, you code was almost the same, except that you needed to learn to use doublequotes properly and also ensure that you use the /D option with the CD command. The main issue as you've already eluded to was that you needed to enable delayed expansion at the right time, which is what I've demonstrated above.
0

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

4 Comments

Idea of reversing the if statement actually worked for me... And yes i am new to batch coding that's the reason i was not sure how to use SET enabledelayedexpansion properly. By the way thanks a lot for you help.
@LotPings I don't quite get why you need a goto, else is fine and much cleaner. Code blocks do code more readable.
@montonero If properly used yes. I tend to put the shorter to handle condition first, so program flow is more clear and excessive nesting of code blocks can be avoided. But that is just my personal preference. You surely know that delayedexpansion is THE most often occuriong issue.
@LotPings For sure the delayed expansion isn't a most well known feature in batch scripts.

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.