Page 1 of 1

Batch Loop Variables

Posted: 13 Feb 2010 11:22
by gungey
Hello all!

I am trying to create a batch script which is waaaaaaaay more complex than I know how to code for... but I know it can be done somehow, so I hope one of you geniuses can help me.

I have a MySQL and file backup script which I use to copy/zip/ftp a selection of domains on my server. At the moment, I have to manually add a lot of code to a batch file each time I add a domain.

What I would like is to specifiy a variables section at the top of a batch file or in a new INI file that would loop through some variables and allow me to code the backup script.

For example, I could specifiy some variables such as:

Filename=Cheese.rar, Database=Cheese, Location=c:\cheese
Filename=Mice.rar, Database=Mouse, Location=c:\squeek
and so on....

Then, I would be able to add the actual code that performs the actions after this. For example, I will just use the echo command:

Echo You have backed up database: %database% and files located in %location% to an archive called %filename%

This would then be looped for every row of variables.

How could I achive a way to read the rows of data in to an array of variabes and then perform a loop?

Any help would be great!
Ian

Re: Batch Loop Variables

Posted: 13 Feb 2010 12:28
by gungey
It looks like I can use the tokens option using the FOR command to solve this.

Hope this helps someone else with this problem :)

Re: Batch Loop Variables

Posted: 22 Feb 2010 12:35
by avery_larry
I like using sequential variables for something like this:

filename1=cheese.rar
filename2=mice.rar
database1=cheese
location1=c:\cheese

etc.

Then you can do this:

Code: Select all

@echo off
setlocal enabledelayedexpansion

set (the variables listed above)
set lastnum=2

for /l %%a in (1,1,%lastnum%) do (
   echo Put your generic code here
   echo Currently working with !filename%%a!, !database%%a!, and !location%%a!
)