Batch Loop Variables

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
gungey
Posts: 2
Joined: 13 Feb 2010 11:21

Batch Loop Variables

#1 Post by gungey » 13 Feb 2010 11:22

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

gungey
Posts: 2
Joined: 13 Feb 2010 11:21

Re: Batch Loop Variables

#2 Post by gungey » 13 Feb 2010 12:28

It looks like I can use the tokens option using the FOR command to solve this.

Hope this helps someone else with this problem :)

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

Re: Batch Loop Variables

#3 Post by avery_larry » 22 Feb 2010 12:35

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!
)

Post Reply