By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,943 Members | 1,890 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,943 IT Pros & Developers. It's quick & easy.

Passing string values to batch files using vbscript

P: 31
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...

Expand|Select|Wrap|Line Numbers
  1. Dim WshShell
  2. Set WshShell = CreateObject("WScript.Shell")
  3. dim pos
  4. pos=InstrRev(full,"\")
  5. dim fpath
  6. dim pl
  7. pl= Len(full)-pos-1
  8. fpath= Left(full,pl)
  9. dim fname 
  10. dim flen
  11. flen= Len(full)-pos
  12. fname=Right(full,flen)
  13. WshShell.Run "setcpath2.bat"
  14.  
I need to pass fname and fpath to the batch file.
I would really appreciate any help with this.

Raghu
Jan 2 '08 #1
Share this Question
Share on Google+
1 Reply


P: 10
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.BAT"
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:\THENEWDIRECTORY"
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.
Mar 10 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.