tags:

views:

33

answers:

2

I need help with this batch file I'm working on,

Basically everytime it does a task it should plus the variable %number% by one which works fine and all and then repeat over again doing a different task until its completed.

This is the code which it uses to repeat

I need a way of settings basically:

set svn=%svn%%number%
set svnlink=%svnlink%%number%
set svnfolder=%svnfolder%%number%

Instead I am using:

if %number%==1 (
set svn=%svn1%
set svnlink=%svnlink1%
set svnfolder=%svnfolder1%)
if %number%==2 (
set svn=%svn2%
set svnlink=%svnlink2%
set svnfolder=%svnfolder2%)
if %number%==3 (
set svn=%svn3%
set svnlink=%svnlink3%
set svnfolder=%svnfolder3%)

Which i really need to shorten as this goes up to number 20+

Is this possible? If you could help me, Thanks!

A: 

Use a temporary batch file. This might not be elegant, but it works:

set svn1=First text
set svn2=Second text

REM This echos the first text
set number=1
echo echo %%svn%number%%% > a.bat
call a

REM This echos the second text
set number=2
echo echo %%svn%number%%% > a.bat
call a

if exist a.bat del a.bat

But beware: It can get quite difficult to find out the number and order of %'s to use here. In the example, %%svn%number%%% is transformed to %svn[actual value X of variable number]% and this is transformed to the actual value of svnX inside of a.bat.

So your actual code should be like this:

REM Note that number has to be set here already!
echo set svn=%%svn%number%%% > a.bat
echo set svnlink=%%svnlink%number%%% >> a.bat
echo set svnfolder=%%svnfolder%number%%% >> a.bat
call a.bat

This can also be used as a subroutine:

set number=1
set returnmark=mark1
goto setvars
:mark1

set number=45
set returnmark=mark2
goto setvars
:mark2

goto end

:setvars
echo set svn=%%svn%number%%% > a.bat
echo set svnlink=%%svnlink%number%%% >> a.bat
echo set svnfolder=%%svnfolder%number%%% >> a.bat
call a.bat
goto %returnmark%

:end
schnaader
It works. Thank you schnaader.
eyefinity
A: 

Use delayed expansion:

setlocal enableextensions enabledelayedexpansion

Then you can do it like this:

set svn=!svn%number%!
set svnlink=!svnlink%number%!
set svnfolder=!svnfolder%number%!

With %number% being evaluated upon parsing, you then have !svn1!, etc. which are evaluated right before running the command (! replacing the % for delayed expansion).

You can also use an undocumented trick (but beware that it might spontaneously stop working since it's undocumented and therefore unsupported):

call set svn=%%svn%number%%%
call set svnlink=%%svnlink%number%%%
call set svnfolder=%%svnfolder%number%%%

This simply adds another layer of indirection to the variable access, just like delayed expansion but doesn't need said feature.

Joey