Solved

Call Set to get Variable Value - 3 Elements 1 Variable

Posted on 2015-02-16
6
78 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 23

Expert Comment

by:NVIT
Comment Utility
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
Comment Utility
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 51

Expert Comment

by:Bill Prew
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Accepted Solution

by:
SuperLight earned 0 total points
Comment Utility
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 23

Expert Comment

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

Author Closing Comment

by:SuperLight
Comment Utility
As per my last comment.

Thanks to all,
Mark
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article describes how to set permissions to allow a limited-permissions user to start and stop a particular System Service.   It is always best to give users only the permissions that they need to perform their job, so tweaking particular permi…
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…
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…
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now