Passing string values to batch files using vbscript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Raghunandan24
    New Member
    • Dec 2007
    • 31

    Passing string values to batch files using vbscript

    Hi,

    I would like to know how to pass arguments to a batch file using vb script.
    I am able to run the batch file using wshShell.Run. I am up against a wall here...

    [CODE=vb]
    Dim WshShell
    Set WshShell = CreateObject("W Script.Shell")
    dim pos
    pos=InstrRev(fu ll,"\")
    dim fpath
    dim pl
    pl= Len(full)-pos-1
    fpath= Left(full,pl)
    dim fname
    dim flen
    flen= Len(full)-pos
    fname=Right(ful l,flen)
    WshShell.Run "setcpath2. bat"
    [/CODE]

    I need to pass fname and fpath to the batch file.
    I would really appreciate any help with this.

    Raghu
    Last edited by Killer42; Jan 2 '08, 09:38 AM.
  • douglandmesser
    New Member
    • Feb 2008
    • 10

    #2
    Should be easy enough. Just use the OPEN commands to edit the file. I usually bring in the whole file or use APPEND. Example below:

    C:\TEST.BAT commands:
    -------------------------------------
    @ECHO OFF
    CLS
    CHDIR C:\TESTDIR
    EXIT

    Let's say you need a particular directory where "C:\TESTDIR " is located. Simply OPEN the file for INPUT as #1 and edit it:

    THEBATCHFILE$ = "C:\TEST.BA T"
    OPEN THEBATCHFILE$ FOR INPUT AS #1
    LINE INPUT #1, LINE1$
    LINE INPUT #1, LINE2$
    LINE INPUT #1, LINE3$
    LINE INPUT #1, LINE4$
    CLOSE #1

    This will load the batch file into variables. Now just write the new data out to the file:

    OPEN THEBATCHFILE$ FOR OUTPUT AS #1
    PRINT #1, LINE1$
    PRINT #1, LINE2$
    PRINT #1, "CHDIR C:\THENEWDIRECT ORY"
    PRINT #1, LINE4$

    It's a long way around using APPEND, but it works. Easy enough if you know how many lines are in the batch file. Alternatively you can just re-write the whole batch file and then run it. Simply use the PRINT command to overwrite the lines that are already there. Make sure to open as OUTPUT.

    Comment

    Working...