Link to home
Start Free TrialLog in
Avatar of SuperLight
SuperLight

asked on

Call Set to get Variable Value - 3 Elements 1 Variable

Hi Experts,

I'm having a problem extracting the value of a variable from a file, the contents:

1_Costs_SIZE=341331
2_Costs_SIZE=350000
3_Costs_SIZE=380000
4_Costs_SIZE=500000

Where by the left hand side of the '=' is the variable name and the right hand side is the value ([Variable] = [Value])

This file is dynamic and will increment each time the process is run and is read by a FOR loop.  The structure of the variable is: [Incremental]_[Variable]_[Constant]

I have tried variations on the below to extract the different variable values:

Source File Contents:
MAIN_EMAIL=someone@somewhere.com
SUB_EMAIL=one@eaddress.com

Set _APP=Main
Call set _EMAIL=%%%_APP%_EMAIL%%

echo %%%_APP%_EMAIL%% - %_EMAIL%
REM *** Would output:  MAIN_EMAIL - someone@somewhere.com ***

Open in new window


This method combines a variable element and a constant to create the variable name and get the variable value.  I want to be able to join 2 variable elements to a constant.  Is this possible?

Thanks in advance,
Mark
Avatar of NVIT
NVIT
Flag of United States of America image

Not sure what you're trying to do... Does this help?
VarsAndVals.txt:
1_Costs_SIZE=341331
2_Costs_SIZE=350000
3_Costs_SIZE=380000
4_Costs_SIZE=500000

Open in new window


This batch assigns the right side to the left side:
echo off
setlocal ENABLEDELAYEDEXPANSION

set FNSrc=VarsAndVals.txt

for /f "tokens=1-3 delims==" %%a in (%FNSrc%) do (
  set var=%%a& echo var=!var!
  set !var!=%%b& echo !var!=%%b
)
goto :eof

Open in new window

Avatar of SuperLight
SuperLight

ASKER

Hi,

Thanks for posting, this might help a little more (this is the code I'm working with):

SET _FILEPATH="D:\Oracle\Middleware\user_projects\epmsystem1\EssbaseServer\essbaseserver1\app\APP\DB\Costs.otl"
SET _APP=AppName
SET _DB=Costs
SET _OTL_SIZE=%~dp0%_APP%_OTL_SIZE.txt
SET /A _COUNTER=1

REM ### Get OTL Size ###
FOR /F "usebackq" %%A IN ('%_FILEPATH%') DO SET _SIZE=%%~zA

setlocal enabledelayedexpansion
REM ### Check if _OTL_SIZE file exists ###
IF EXIST %_OTL_SIZE% (
	REM ### Read through the existing _OTL_SIZE file ###
	FOR /f "TOKENS=1,2 DELIMS==" %%i IN (%_OTL_SIZE%) DO (
	SET /A _COUNTER+=1
	)
	REM ### Create a new line ###
	ECHO %_COUNTER%_%_APP%_SIZE=%_SIZE%>>%_OTL_SIZE%
) ELSE (

	REM ### This is the first line if the file doesn't exist ###
	ECHO %_COUNTER%_%_APP%_SIZE=%_SIZE%>%_OTL_SIZE%
)

REM ### In this section I want to find out the difference between the 1st entry in %_OTL_SIZE% and the latest ###

Open in new window


At the end I want to find out the value of the 1st and last in the file that's produced:

1_Costs_SIZE=341331
2_Costs_SIZE=350000
3_Costs_SIZE=380000
4_Costs_SIZE=500000

But also I'd like the option of being able to pull a different variable (and value) based upon the incrementing number.

Thanks,
Mark
Hive this a try, see if it gives you some ideas.  It reads the file, gets the first and last values, and also stores all values in an "array" of variable names that you could reference by number.  Let me know what questions you have.

SET _FILEPATH="D:\Oracle\Middleware\user_projects\epmsystem1\EssbaseServer\essbaseserver1\app\APP\DB\Costs.otl"
SET _APP=AppName
SET _DB=Costs
SET _OTL_SIZE=%~dp0%_APP%_OTL_SIZE.txt
SET /A _COUNTER=1

REM ### Get OTL Size ###
FOR /F "usebackq" %%A IN ('%_FILEPATH%') DO SET _SIZE=%%~zA

setlocal enabledelayedexpansion
REM ### Check if _OTL_SIZE file exists ###
IF EXIST %_OTL_SIZE% (
	REM ### Read through the existing _OTL_SIZE file ###
	FOR /f "TOKENS=1,2 DELIMS==" %%i IN (%_OTL_SIZE%) DO (
	SET /A _COUNTER+=1
	)
	REM ### Create a new line ###
	ECHO %_COUNTER%_%_APP%_SIZE=%_SIZE%>>%_OTL_SIZE%
) ELSE (

	REM ### This is the first line if the file doesn't exist ###
	ECHO %_COUNTER%_%_APP%_SIZE=%_SIZE%>%_OTL_SIZE%
)

REM ### In this section I want to find out the difference between the 1st entry in %_OTL_SIZE% and the latest ###

setlocal EnableDelayedExpansion

set CostFirst=
set CostLast=

for /f "tokens=1-4 delims=_=" %%A in (%_OTL_SIZE%) do (
    if "!CostFirst!" EQU "" (
        set CostFirst=%%D
    )
    set CostLast=%%D
    set Cost[%%A]=%%D
)
set Cost

Open in new window

~bp
ASKER CERTIFIED SOLUTION
Avatar of SuperLight
SuperLight

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the update, SuperLight. Glad you got it working.
As per my last comment.

Thanks to all,
Mark