Solved

Call Set to get Variable Value - 3 Elements 1 Variable

Posted on 2015-02-16
6
82 Views
Last Modified: 2015-03-27
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
0
Comment
Question by:SuperLight
  • 3
  • 2
6 Comments
 
LVL 24

Expert Comment

by:NVIT
ID: 40613069
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

0
 

Author Comment

by:SuperLight
ID: 40613081
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
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 40613282
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
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Accepted Solution

by:
SuperLight earned 0 total points
ID: 40623436
Hi All,

Thanks for the suggestions, but I persevered and came up with this :

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

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

SETLOCAL enabledelayedexpansion
IF EXIST %_OTL_SIZE_FILE% (
	FOR /F "TOKENS=1,2 DELIMS==" %%i IN (%_OTL_SIZE_FILE%) DO (
	SET /A _COUNTER+=1
	CALL SET %%i=%%j
	)
	ECHO %_APP%_!_COUNTER!_SIZE=%_SIZE%>>%_OTL_SIZE_FILE%
	SET _LATEST_SIZE=%_SIZE%
) ELSE (
	ECHO %_APP%_!_COUNTER!_SIZE=%_SIZE%>%_OTL_SIZE_FILE%
)

SET _NO=!_COUNTER!
Call set _ORIG_SIZE=%%%_APP%_1_SIZE%%
SET /A _GROWTH=%_MAX_FACTOR%*%_ORIG_SIZE%
 
IF %_LATEST_SIZE% GEQ %_GROWTH% (
    echo too big
) ELSE (
    echo OK
)

Open in new window


I had to move the indexing value to the middle of the variable name and also used "SETLOCAL enabledelayedexpansion".  Thanks to everyone who posted up.

Mark
0
 
LVL 24

Expert Comment

by:NVIT
ID: 40623444
Thanks for the update, SuperLight. Glad you got it working.
0
 

Author Closing Comment

by:SuperLight
ID: 40691415
As per my last comment.

Thanks to all,
Mark
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Log files are useful in diagnosing and repairing problems.  This is a list of common log files and their standard locations that I've compiled.   While this is not exhaustive, it is a pretty good list that I've found to be useful.  I may update it f…
The way I use Experts Exchange to assist me in analyzing and diagnosing a problem is I first enter a Verbose Question at Experts Exchange like: Office 2007 will hang when opening and saving files I then launch WordPad (any text editor will do) an…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question