batch-filebuildforge

Echo inside an echo


I'm trying to get to the value of a variable that's made up of two other variables. The only way I've gotten this to work is by using the FOR command with the /F switch and opening a second command shell:

SET A=This
SET B=That
SET ThisThat=YES!

FOR /F %I IN ('ECHO %A%%B%') DO cmd.exe /C ECHO %%I%

Output:

C:\>cmd.exe /c echo %ThisThat%
YES!

Does anyone know of a more elegant way to do this?

UPDATE

Here's a better explanation of what I was trying to accomplish (and was able to accomplish using y'all's - especially Aacini's - responses)

For a Rational BuildForge project, I have environment variables defined for each deployment target host. Example:

CURAM_HOST_NAME_DEV=DDW5T
CURAM_HOST_NAME_UAT=DUW5T
CURAM_HOST_NAME_TRN=DTW5T
CURAM_HOST_NAME_PRD=DPW5P

Another variable HOST contains the environment to deploy to. The deployment code is common for all deployment targets and uses the variable CURAM_HOST_NAME, so I needed to set this variable to the value of the variable that corresponds to the HOST being deployed to. The following does the trick:

@ECHO OFF

SET CURAM_HOST_NAME_DEV=DDW5T
SET CURAM_HOST_NAME_UAT=DUW5T
SET CURAM_HOST_NAME_TRN=DTW5T
SET CURAM_HOST_NAME_PRD=DPW5P

SET HOST=DEV
CALL ECHO %%CURAM_HOST_NAME_%HOST%%%
SET HOST=UAT
CALL ECHO %%CURAM_HOST_NAME_%HOST%%%
SET HOST=TRN
CALL ECHO %%CURAM_HOST_NAME_%HOST%%%
SET HOST=PRD
CALL ECHO %%CURAM_HOST_NAME_%HOST%%%

Output:

DDW5T
DUW5T
DTW5T
DPW5P

In the BuildForge step, the code that sets this particular variable looks like the following:

.bset env "CURAM_HOST_NAME=`CALL ECHO %%%CURAM_HOST_NAME_%%HOST%%%%`"

(The BF parser requires additional %'s)

For a value of DEV for the HOST variable this produces the following in the log:

1026 5/3/14 7:34 PM STEP .bset env "CURAM_HOST_NAME=`CALL ECHO %%%CURAM_HOST_NAME_%%HOST%%%%`"
1027 5/3/14 7:34 PM EXEC .bset env 'CURAM_HOST_NAME' = 'DDW5BTH

Thanks a lot everyone. Wish I had the reputation that would allow me to upvote your answers!

Jozef


Solution

  • @echo off
    
    SET A=This
    SET B=That
    SET ThisThat=YES!
    
    call echo %%%A%%B%%%
    

    EDIT: Updated answer to an updated question!

    @ECHO OFF
    
    SET CURAM_HOST_NAME_DEV=DDW5T
    SET CURAM_HOST_NAME_UAT=DUW5T
    SET CURAM_HOST_NAME_TRN=DTW5T
    SET CURAM_HOST_NAME_PRD=DPW5P
    
    for %%a in (DEV UAT TRN PRD) do CALL ECHO %%CURAM_HOST_NAME_%%a%%
    

    However, you should note that the !variable! replacement (together with setlocal EnableDelayedExpansion) run much faster than the CALL echo %%variable%% trick:

    @ECHO OFF
    setlocal EnableDelayedExpansion
    
    SET CURAM_HOST_NAME_DEV=DDW5T
    SET CURAM_HOST_NAME_UAT=DUW5T
    SET CURAM_HOST_NAME_TRN=DTW5T
    SET CURAM_HOST_NAME_PRD=DPW5P
    
    for %%a in (DEV UAT TRN PRD) do ECHO !CURAM_HOST_NAME_%%a!
    

    Perhaps you may want to review this post