1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Passing parameters from a file

Discussion in 'Support' started by therealmikenelson, May 31, 2008.

  1. therealmikenelson

    Joined:
    May 28, 2008
    Messages:
    5
    Likes Received:
    0
    I want to use a file or files to pass parameters to a batch file where I'd use it as a %var%. I want to avoid human involvement and get all the parameters required from a text file or other batch file residing in the same directory.

    Is there a way to have a batch file read another batch file to get a variable/parameter?

    In other words batchA.cmd says call batchB.cmd

    BatchB contains only a string such as "45553442" or "MCCNumericCode"

    BatchA then uses that variable (%whatever%) in its execution.

    Doable?

    Mike Nelson
     
  2. Jim Cook

    Joined:
    May 20, 2008
    Messages:
    604
    Likes Received:
    0
    You bet. Here's one way using the @line[] function:
    TCC 9.02.148 Windows XP [Version 5.1.2600]
    TCC Build 148 Windows XP Build 2600 Service Pack 3


    D:\>type datafile.dat
    45553442
    MCCNumericCode

    D:\>type batcha.btm
    @echo off
    set whatever=%@line[datafile.dat,0]
    echo I read %whatever% from the first entry of data file

    set whatever=%@line[datafile.dat,1]
    echo I read %whatever% from the second entry of data file

    D:\>batcha.btm
    I read 45553442 from the first entry of data file
    I read MCCNumericCode from the second entry of data file


    --
    2008 Fridays: 4/4, 6/6, 8/8, 10/10, 12/12 and 5/9, 9/5, 7/11, 11/7.
    Next year they're Saturday.
    Measure wealth by the things you have for which you would not take money.
     
  3. Steve Fabian

    Joined:
    May 20, 2008
    Messages:
    3,520
    Likes Received:
    4
    therealmikenelson wrote:
    | I want to use a file or files to pass parameters to a batch file
    | where I'd use it as a %var%. I want to avoid human involvement and
    | get all the parameters required from a text file or other batch file
    | residing in the same directory.
    |
    | Is there a way to have a batch file read another batch file to get a
    | variable/parameter?
    |
    | In other words batchA.cmd says call batchB.cmd
    |
    | BatchB contains only a string such as "45553442" or "MCCNumericCode"
    |
    | BatchA then uses that variable (%whatever%) in its execution.
    |
    | Doable?

    Yes. Look at the @line[] function. Excellent if file has only a few lines.
    You can also do this:

    set n=0
    do val in @batchb.cmd
    set var%n=%val
    set n=%@inc[%n]
    enddo


    This will set var0 to the contents of line 0, var1 to the contents of line
    1, etc.

    Another technique is to format the data file in the style of .INI files,
    with sections and variables with values. You can read the values using
    @iniread[].

    BTW, .CMD for data files is not a good extension - it is misleading, one
    expects a script file designed for CMD.EXE.

    Furthermore, .BTM files are executed faster in TCC than .CMD files.
    --
    HTH, Steve
     
  4. Charles Dye

    Charles Dye Super Moderator
    Staff Member

    Joined:
    May 20, 2008
    Messages:
    3,351
    Likes Received:
    39
    Actually, since the second file isn't a batch file at all (it doesn't contain commands), I would suggest a .TXT extension.
     
  5. samintz

    samintz Scott Mintz

    Joined:
    May 20, 2008
    Messages:
    1,201
    Likes Received:
    11
    You have a number of options to achieve that goal. The simplest is to
    have two script files and just have the first call the second passing the
    second the parms it needs.

    batchA:
    call batchB parm1 parm2 parm3

    batchB:
    echo %1 %2 %3


    A second option would allow you to use a response file:

    rspfile:
    parm1
    parm2
    parm3

    batchA:
    echo %@line[rspfile,0] %@line[rspfile,1] %@line[rspfile,2]

    You could also have all 3 parms on the same line and pick out the
    individual parameters using @WORD or @FIELD

    And you could also use an INI file and use @INIREAD to pull out the named
    items you want.

    There are additional ways to achieve this goal as well. But that should
    be enough to get you started.

    -Scott




    therealmikenelson <>
    05/31/2008 01:39 PM
    Please respond to



    To
    Samintz@ra.rockwell.com
    cc

    Subject
    [Support-t-97] Passing parameters from a file






    I want to use a file or files to pass parameters to a batch file where I'd
    use it as a %var%. I want to avoid human involvement and get all the
    parameters required from a text file or other batch file residing in the
    same directory.

    Is there a way to have a batch file read another batch file to get a
    variable/parameter?

    In other words batchA.cmd says call batchB.cmd

    BatchB contains only a string such as "45553442" or "MCCNumericCode"

    BatchA then uses that variable (%whatever%) in its execution.

    Doable?

    Mike Nelson
     

Share This Page