How can I setup an array variable in a DOS batch script? I would like to load it with a list of file names to process. I really would like to keep this as simple as possible. Thank you.
|
|
I figured it out:
I did something like this several years ago, so it just took some figuring out. (btw, I hate reinventing the wheel.) Now that it's posted here, hopefully others will find it useful too. |
||||
|
|
|
From Jakash3's Blog, Arrays in Batch describes how to emulate arrays in the command prompt. A batch file named array.bat is included in the article, that contains a library of functions used to handle arrays. You will need to select the the text in the article and paste it inside the bat file. For example, the following test script:
generates the following output:
|
|||
|
|
|
Yes you can do arrays in batch. While they aren't exactly like arrays in C or VB, you CAN do it:
|
||||
|
|
|
You can't really do an array in the command prompt (it's not really DOS unless you are running Windows 3.1). However, you can do set-based processing with the FOR command. To create the list of files to be processed, you can manually create a text file to be processed, or use the Something I learned the hard way with FOR - you need to use a SINGLE CHARACTER variable as your cursor! |
|||||
|
|
Check out the Microsfot documentation for the FOR batch command. You can use FOR to iterate over a set of directories, files in a directory, or files in a directory and all of its sub-directories. By default FOR handle set or just files:
Or, for recursive:
or, for just direcotries:
|
|||
|
|
For what you want to do, how about using an input variable for each file name, which you can pick up with the %1 %2 etc. variables eg: mybat.bat file1.nam file2.nam etc.. You can then use these variables in a loop but only need to use %1 as the main parameter as you can use SHIFT to bring the other variables into the %1 'position', having a test for null (end of variables) with something like IF "X%1" == "X" eg:
|
|||
|
|
|
If you save the list of files to filelist.txt, you can get
to print each line of a file (up to the first "|" in the line. If you don't specify your own delimiter, space and tab will be used, so unless your paths have no spaces, you need to specify a character that will not appear in the file to get complete lines). |
|||
|
|
|
In one of your comments to an answer you say,
I believe that what you are looking for is a Makefile, and possibly cygwin to give you a decent shell (bash, or whatever your flavour). |
|||
|
|
