Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Loading data into an array in MS DOS Batch file

Options
  • 20-03-2009 4:57pm
    #1
    Registered Users Posts: 5,949 ✭✭✭


    Hi, just wondering can someone tell me how to load data from a file into an array as a set of variables. I then want to create a for loop which will run a command on each of those variables. The set of variables in the file is different everytime the program is run, and the number of lines of data in the file also varies.

    So for example...

    Sample_file
    Data_entry1
    Data_entry2
    Data_entry3
    ......

    So i want to insert (Data_entry1 Data_entry2 Data_entry3....) into an Array. Then run a for loop

    for each variable in array
    do......

    I thought this would have been a basic part of any program containing data but I'm finding problems finding a tutorial on this despite googling forever. In all tutorials on array you have to manually add each entry into the array in the actual script, which seems pointless. The number of lines the file containing the data varies each time the program is run.

    I done something similiar for a different project in Linux and I did something like this.

    A=( $(<tempfile) )

    So basically I'm looking for a Dos equivalent or something utilising a linux command that can be downloaded in Windows. I've tried a few different things but hasn't worked. If its not possible in Dos, can someone tell me how to do it in C or Java.


    Thanks a lot!


Comments

  • Registered Users Posts: 1,998 ✭✭✭lynchie


    check out cygwin.. gives u full unix utils in windows.


  • Registered Users Posts: 5,379 ✭✭✭DublinDilbert


    what are you trying to do?

    depending on what your trying to do, it might be as easy to write a small command-line C program.


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    If you have a text file with single line for each file you can use "FOR /F"

    For example a file called test.txt.
    temp
    program files
    

    then...
    for /F %x in (test.txt) do dir  "%x"
    

    This would display a directory list for the "temp" and "program files" folders.

    If you want to do from command line then you would use the SWITCH statement. Although the first option is probably better.

    In a batch file where you have a number of commands you should have "do call xxxx" where xxxx is the label. the end of the code block must jump to the end of the batch file in order to continue executing.


Advertisement