Solved

Variable with a For Loop

Posted on 2008-11-03
10
920 Views
Last Modified: 2010-04-21
Hi There,

I'm trying to write MS DOS script to create a text file and rather than writing everything out long hand I have used a number of FOR Statements to loop around the contents of %PlanDBList% (Corp_BP II_BP RET_BP JOINT_BP GROUP_BP).

Summary of Batch
1.The System.Properties file acts as a ini file and contains numerous values
2. Reads the ON/OFFScenario files and populates the contents into a number of variables, for example:
%%a_xptONlineScenario becomes CORP_BP_xptONlineScenario which contains a number of values eg "actual","budget"
3. Loop through the %PlanDBList% and populate the echo statement with the variables.

I'm having trouble with No.3! where I'm trying to merge numerous variables to derive the correct value.

Many Thanks in Advance

Mark

REM *************************************
REM *** Create Substitution Variables ***
REM *************************************
for /f "tokens=1,2 delims==" %%i in (g:\System.properties) do set %%i=%%j
 
set HOMEDIR=%OnlineBatchPath%
 
rem echo %date:~0,10% %time:~0,8% - ALL, PFP, Creating Substitution Variables for OFFLINE / ONLINE switches.>>%LogFile%
 
REM *** Copy ONLINE/OFFLINE scenarios from PlanningAdmin utility ***
 
COPY /Y %ExcelUtility%\*lineExport.txt %HOMEDIR%\Inputs\*lineExport.txt
 
 
REM **********************************
REM *** Read ON/OFF line Scenarios ***
REM **********************************
 
for %%a in (%PlanDBList%) do (
	REM *** Read OFFLINE scenarios ***
	for /f "Delims=| Tokens=1,2" %%b in (%HOMEDIR%\Inputs\%%a_OfflineExport.txt) do (
		IF NOT "%%b"=="" set %%a_xptOFFlineScenario=%%b
		)
	REM *** Read OFFLINE scenarios ***
	for /f "Delims=| Tokens=1,2" %%c in (%HOMEDIR%\Inputs\%%a_OnlineExport.txt) do (
		IF NOT "%%c"=="" set %%a_xptONlineScenario=%%c
		)
)
 
 
REM ********************************************
REM *** Create MAXL Script for Sub Variables ***
REM ********************************************
 
echo login %EssUser% %EssPass% on Localhost;>%HOMEDIR%\SubVar.mxl
echo set timestamp %TimeStampOvernight%;>>%HOMEDIR%\SubVar.mxl
echo spool on to '%ROOTDIR%\BATCH\Logs\%batchdate%\%BSO_APP%AggOutline_MAXL.Log';>>%HOMEDIR%\SubVar.mxl
 
 
for %%a in (%PlanDBList%) do (
	Call Set ASOPAPP==%%%%a_ASO%%
	echo alter application '%ASOAPP%' drop variable '%%%a_xptONlineScenario%';>>%HOMEDIR%\SubVar.mxl
	echo alter application '%ASOAPP%' drop variable '%%%a_xptONlineScenario%';>>%HOMEDIR%\SubVar.mxl
	echo alter application '%ASOAPP%' drop variable '%%%a_xptONlineScenario%';>>%HOMEDIR%\SubVar.mxl
	)

Open in new window

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
  • 6
  • 4
10 Comments
 
LVL 5

Expert Comment

by:PaulKeating
ID: 22872183
I see one obvious problem, and something that looks to me like a problem but that may be because I don't fully understand what you're trying to do.

Obvious problem first. You are clearly expecting

%%%a_xptONlineScenario%

in lines 42-44 first to expand %%a to a value from %PlanDBList% (for example Corp_BB) and then to expand (for example) %Corp_BP_xptONlineScenario% to the value of the environment variable.

This won't work. The macro expansion process isn't like a programming language function call that can be nested: it is done in a single pass. So %%%a_xptONlineScenario% will expand to the string %Corp_BP_xptONlineScenario% as you expect, but you don't get a second bite at the cherry to expand %Corp_BP_xptONlineScenario% to the value of the environment variable it refers to. It stays that value, percent signs and all.

What you can do to get this sort of double dereferencing, is call a subroutine:

call :MyRoutine %a_xptONlineScenario
goto :eof

:MyRoutine
      echo alter application '%ASOAPP%' drop variable '%1';>>%HOMEDIR%\SubVar.mxl
      goto :eof


Now for the potential non-problem: The loop that starts in line 19 repeatedly sets the variables %%a_xptONlineScenario and %%a_xptOFFlineScenario to a value from the Export.txt files. But if there is more than one qualifying value in those files then your variable will only be set to the last one, as in

set myvar=1
set myvar=2

etc. I suspect that isn't what you meant but I'm not sure what you did mean.
   






 
0
 
LVL 5

Expert Comment

by:PaulKeating
ID: 22872205
Oops. Subroutine should be:

:MyRoutine
     set temp=%1
      echo alter application '%ASOAPP%' drop variable '%temp%';>>%HOMEDIR%\SubVar.mxl
      goto :eof



0
 

Author Comment

by:SuperLight
ID: 22875138
Thanks I'll give it a go
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 

Author Comment

by:SuperLight
ID: 22920228
Hi There,

I haven't forgotten this post, this is part of a larger development.

Thanks

Mark
0
 
LVL 5

Expert Comment

by:PaulKeating
ID: 22926122
No problem, it'll keep.
0
 

Author Comment

by:SuperLight
ID: 22937672
Paul,

Thanks for your comment about the "non problem" it was a problem and I couldn't understand why it was only returning one variable.  In the end I used two batches to create the desired result. The first batch looped through the first variable: %PlanDBList% and made a call to another batch.  I'll post the finished result up later (at home at the moment).  Any comments / observations would be most welcome.

Thanks

Mark
0
 

Author Comment

by:SuperLight
ID: 23002517
Paul,

Here is the code used:
PLANDBLIST contain CORP_BP,RET_BP,II_BP,JOINT_BP,GROUP_BP
The first half of the code calls another batch for the number of entries in the above

Many Thanks

Mark
1st Part
 
for /f "tokens=1,2 delims==" %%i in (g:\System.properties) do set %%i=%%j
 
SET HOMEDIR=%ROOTDIR%\Batch\Master
 
if "%Logfile%"=="" (
	set LogFile=%HOMEDIR%\..\Logs\SubVar.log
	)
 
COPY /Y %ExcelUtility%\*Export.txt %OnlineBatchPath%\Inputs
 
REM ********************************************
REM *** Create MAXL Script for Sub Variables ***
REM ********************************************
 
echo %date:~0,10% %time:~0,8% - ALL, PFP, Started creating Substitution Variables for OFFLINE/ONLINE partitions (CreateSubVariable BAT).>>%LogFile%
 
echo login %EssUser% %EssPass% on Localhost;>%HOMEDIR%\..\MaxL\SubVar.mxl
echo set timestamp %TimeStampOvernight%;>>%HOMEDIR%\..\MaxL\SubVar.mxl
echo spool on to '%ROOTDIR%\\BATCH\\Logs\\%batchdate%\\SubVariable_MAXL.Log';>>%HOMEDIR%\..\MaxL\SubVar.mxl
 
for /f "Delims=| Tokens=1,2" %%c in (%OnlineBatchPath%\Inputs\CYExport.txt) do (
IF NOT "%%c"=="" set CY=%%c
)
 
echo /*System Level Current Year Variable*/>>%HOMEDIR%\..\MaxL\SubVar.mxl
echo alter system drop variable 'CY';>>%HOMEDIR%\..\MaxL\SubVar.mxl
echo alter system add variable 'CY' '%CY%';>>%HOMEDIR%\..\MaxL\SubVar.mxl
 
REM *** Creates the DROP and CREATE for the Sub Var
for %%a in (%PlanDBList%) do (
	call %MainShared%..\OutlineConv\SubVarMAXL.bat %%a
	)
 
echo spool off;>>%HOMEDIR%\..\MaxL\SubVar.mxl
echo Shell 'echo %date:~0,10% %time:~0,8% - ALL, PFP, Completed creating Substitution Variables for OFFLINE/ONLINE partitions  (CreateSubVariable BAT).^>^>%LogFile%';>>%HOMEDIR%\..\MaxL\SubVar.mxl
echo logout;>>%HOMEDIR%\..\MaxL\SubVar.mxl
 
call %OnlineBatchPath%\SharedComponents\change.exe %HOMEDIR%\..\MaxL\SubVar.mxl [ '
 
essmsh %HOMEDIR%\..\MaxL\SubVar.mxl
 
call %HOMEDIR%\..\SharedComponents\ParseMaxLLogForErrors.cmd %ROOTDIR%\\BATCH\\Logs\\%batchdate%\\SubVariable_MAXL.Log
 
------------------------------------------------------------------
2nd Part (SubVarMAXL.bat)
 
REM **********************************
REM *** Read ON/OFF line Scenarios ***
REM **********************************
 
for /f "Delims=| Tokens=1,2" %%b in (%OnlineBatchPath%\Inputs\%1_OfflineExport.txt) do (
IF NOT "%%b"=="" call set %1_xptOFFlineScenario=%%b
)
for /f "Delims=| Tokens=1,2" %%c in (%OnlineBatchPath%\Inputs\%1_OnlineExport.txt) do (
iF NOT "%%c"=="" call set %1_xptONlineScenario=%%c
)
 
set BSOAPP=%1
 
call set ASOAPP=%%%1_ASO%%
Call set OnScenario=%%%BSOAPP%_xptONlineScenario%%
Call set OffScenario=%%%BSOAPP%_xptOFFlineScenario%%
 
 
REM ******************
REM *** Write MAXL ***
REM ******************
 
echo /*ONLINE SCENARIO - %1*/>>%HOMEDIR%\..\MaxL\SubVar.mxl
echo alter application '%PlanMainApp%' drop variable 'On_%1_Planning';>>%HOMEDIR%\..\MaxL\SubVar.mxl
echo alter application '%ASOAPP%%ASO_Active%' add variable 'On_%1_ASO' [%OnScenario%[;>>%HOMEDIR%\..\MaxL\SubVar.mxl
echo alter application '%PlanMainApp%' add variable 'On_%1_Planning' [%OnScenario%[;>>%HOMEDIR%\..\MaxL\SubVar.mxl
echo /*OFFLINE SCENARIO - %1*/>>%HOMEDIR%\..\MaxL\SubVar.mxl
echo alter application 'A_PMain' add variable 'Off_%1_ASO' [%OffScenario%[;>>%HOMEDIR%\..\MaxL\SubVar.mxl
echo alter application '%PlanMainApp%' drop variable 'Off_%1_Planning';>>%HOMEDIR%\..\MaxL\SubVar.mxl
echo alter application '%PlanMainApp%' add variable 'Off_%1_Planning' [%OffScenario%[;>>%HOMEDIR%\..\MaxL\SubVar.mxl

Open in new window

0
 

Author Comment

by:SuperLight
ID: 23113259
Paul,

How do you want to divy the point up?

Thanks

Mark
0
 
LVL 5

Accepted Solution

by:
PaulKeating earned 500 total points
ID: 23113539
Well, you worked out the details entirely on your own, all I did was offer pointers. (That worked, though!)

Divvy as seems to you best.
0
 

Author Closing Comment

by:SuperLight
ID: 31512656
Merry Christmas
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

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…

739 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