?
Solved

Call Set to get Variable Value - 3 Elements 1 Variable

Posted on 2015-02-16
6
Medium Priority
?
90 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 25

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 56

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 25

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
Ever notice how you can't use a new drive in Windows without having Windows assigning a Disk Signature?  Ever have a signature collision problem (especially with Virtual Machines?)  This article is intended to help you understand what's going on and…
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…

764 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