Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Call Set to get Variable Value - 3 Elements 1 Variable

Posted on 2015-02-16
6
Medium Priority
?
92 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 58

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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 

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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Many admins will agree: WSUS is is a nice invention but using it on the client side when updating a newly installed computer is still time consuming as you have to do several reboots and furthermore, the procedure of installing updates, rebooting an…
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
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…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …

609 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