Arrays in Batch File

Posted on 2006-04-26
Last Modified: 2012-05-05

In a typical DOS Batch file, can we have something like Arrays (like in other programming languages) so that we can read each of the element in the array, prepare a DOS command with this array and execute the same.

I would also like each of the DOS command being executed to wait for the executing command to exit before proceeding to issue the next command.

I can do this in C# but in DOS Batch file I am confused.
Question by:deepaknet
    LVL 95

    Expert Comment

    by:Lee W, MVP
    DOS batch files are a series of programs executed in sequence.  There are some ways of throwing in variables and executing out of sequence, but thinking of DOS as a programming language would be a stretch.

    You can do a lot with the for command - type for /? at a command prompt for more information on the for command.

    It would help if you could give a specific example of what you are looking for.
    LVL 15

    Author Comment

    Would then having a VBScript file and calling from CScript do the trick.

    Basically, I would give you an example:

    An array of Fruits like {Apple, Mango, Pomegranete, Banana}

    Now I have to call an application like

    FruitVendor.exe and pass each of the fruit one at a time. Additionally, while one instance of FruitVendor is running, the loop should wait for exit and then take the next one.

    In C#, I can do this as

    string[] strFruit = {"Apple", "Mango","Pomegranete", "Banana"};

    Then I can use foreach to iterate through this and call Process.Start ("fruitvendor.exe") and pass each of the fruit with WaitForExit() enabled.

    The problem is that this needs to be a batch file preferably instead of a compiled application.
    LVL 95

    Accepted Solution

    Ok, here's the thing - I know there's a way to do this as I worked on a question where we tried to parse drive letters like that, but the command that gave the drive letters worked strangely.  Unfortunately, I don't remember the EXACT "for" command syntax for it (I'll try to dig it up but I'e participated in a LOT of questions).

    One way to do it is to create a text file - fruits.txt - so the text file is your array. In your example, the text file would have 4 lines, one fruit per line, NOTHING ELSE.

    Then this for command would execute the command for each fruit.  The nature of a batch file won't allow it to run the next command until that one is done.  (Again, there are ways around this, but you WANT this behaviour).

    FOR /F "tokens=*" %a IN (fruits.txt) DO yourcommand %a

    For a demonstration, run this command:
    FOR /F "tokens=*" %a IN (fruits.txt) DO @Echo my fruit is %a

    NOTE: when using the for command in a batch file, you need to use %% instead of % - so BATCH FILE ONLY:
    FOR /F "tokens=*" %%a IN (fruits.txt) DO @Echo my fruit is %%a

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Disclosure: Use this tutorial only when no other options helps to get Windows XP running without any problems and you don't want to format the drive. The back up of the data is the responsible of the user, however there is a description of how t…
    Migration of Exchange mailbox can be done with the ExProfre.exe tool. But at times, when the ExProfre.exe tool migrates the Exchange Server user profile, it results in numerous synchronization problems. Synchronization error messages appear in the e…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now