Batch file loop variables

Microsoft Windows server 2008 r2 standar...
February 13, 2010 at 09:19:47
Specs: Windows 2008
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!

See More: Batch file loop variables

Report •

February 13, 2010 at 10:28:02
It looks like I've solve this by using the tokens option using the FOR command :)

Report •

February 13, 2010 at 11:55:53
Indeed, the FOR /F loop does exactly that, it can parse a given inputfile, line by line.

If you haven't decided on the format (or, it's you who can choose anyway), I would say to avoid syntax like :

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

But choose something like

Cheese.rar * Cheese * c:\cheese
Mice.rar * Mouse * c:\squeek

It doesn't have to be a star as separator, but it must be a character that doesn't occur inside the data itself. I'm also assuming each line has got 3 variables (like in the example) : a filename, some info, and a directory.

Report •

Related Solutions

Ask Question