Mike
Mike

Reputation: 133

Cmd Variables with Reserved Character %

I have a service account that I am trying to assign a variable to - I am unable to change the password.

Password is "password%6k"

I repeat the % character to escape

SET $9=password%%6k
REM $9

Cmd Output:
password%6k

call startmaxl.bat %$9%

-> In MAXL 
echo "$9";

MAXL Output:
passwordK

This is causing my MAXL batch to fail due to incorrect credentials - How do I pass the properly formatted password to the MAXL engine?

New Code:

SET "$9=password%%6k"
echo $9=%9%
CALL echo $9=%9%

Cmd Output:
echo $9=password%6k

MAXL:
shell echo "$9";
MAXL Output: password%6k
-- This is correct now 

The batch then exits expectantly - I have to specify this full path to start MAXL

Batch File Currently: 
echo Beginning Test at %time% on %date% > D:\Logs\TEST_START.log
SET (parameters 1-8)
SET "$9=password%%6k"
echo $9=%9%
CALL echo $9=%9%

D:\Oracle\Middleware\user_projects\epmsystem1\EssbaseServer\essbaseserver1\bin\startMaxl.bat test.msh %$1% %$2% %$3% %$4% %$5% %$6% %$7% %$8% %$9%

echo Ending Test at %time% on %date% > D:\Logs\TEST_END.log

I'm currently able to pass the correct password through, build a portion of an essbase cube, but the batch is exiting immediately after the MAXL logs out and skips the last TEST_END log.

Is there anyway to do a CALL instead? How would I go about setting this up

Upvotes: 0

Views: 311

Answers (1)

JosefZ
JosefZ

Reputation: 30123

Each CALL does one substitution of the variables (see the first 2 output lines from the following simple .bat script). You could apply DelayedExpansion as follows:

@ECHO OFF
rem setlocal script level 
SETLOCAL EnableExtensions DisableDelayedExpansion

rem percent sign escaped by doubling
SET "$9=password%%6k"

rem use the $9 variable                # %% => %
echo $9=%$9% (echo^)

rem CALL the $9 variable               # %6 => 6th line parameter
CALL echo $9=%$9% (CALL echo^)
echo ---

rem setlocal call level
SETLOCAL EnableDelayedExpansion
echo --- call startmaxl !$9:%%=%%%%!
call :startmaxl !$9:%%=%%%%!
rem endlocal call level 
ENDLOCAL

rem endlocal script level 
ENDLOCAL
goto :eof

rem the following procedure substitutes startmaxl.bat script
:startmaxl
echo in %~0 parameter=[%~1], variable=[%$9%]
goto :eof

Please note that I'm calling an internal subroutine (label :startmaxl) instead of an external batch script (startmaxl.bat) without loss of generality or accuracy. You can use call startmaxl.bat !$9:%%=%%%%! with the same result.

Output:

D:\bat\SO\59793838.bat a b c d e SixthParam g
$9=password%6k (echo)
$9=passwordSixthParamk (CALL echo)
---
--- call startmaxl password%%6k
in :startmaxl parameter=[password%6k], variable=[password%6k]

Upvotes: 1

Related Questions