Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 923
  • Last Modified:

Variable with a For Loop

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
SuperLight
Asked:
SuperLight
  • 6
  • 4
1 Solution
 
PaulKeatingCommented:
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
 
PaulKeatingCommented:
Oops. Subroutine should be:

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



0
 
SuperLightAuthor Commented:
Thanks I'll give it a go
0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
SuperLightAuthor Commented:
Hi There,

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

Thanks

Mark
0
 
PaulKeatingCommented:
No problem, it'll keep.
0
 
SuperLightAuthor Commented:
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
 
SuperLightAuthor Commented:
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
 
SuperLightAuthor Commented:
Paul,

How do you want to divy the point up?

Thanks

Mark
0
 
PaulKeatingCommented:
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
 
SuperLightAuthor Commented:
Merry Christmas
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now