Solved

Variable with a For Loop

Posted on 2008-11-03
10
916 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
  • 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

867 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

16 Experts available now in Live!

Get 1:1 Help Now