Anonymous 1582
Anonymous 1582

Reputation: 21

How to write VBScript from batch?

I have a problem with adding VBScript to a Batch file. I tried this:

@echo off
echo MsgBox("Hello")
echo Do
echo MsgBox("Hello")
echo Loop >>msg2.vbs
start msg2.vbs

But it gave me an error that I used Loop without Do. What am I doing wrong?

Upvotes: 0

Views: 387

Answers (2)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200193

Your batch file doesn't magically know which lines you want in the VBScript. Either redirect each echo output (as agriffaut suggested), or run the echo statements in a command block and redirect the entire output of that block (so you don't have to append repeatedly):

(
  echo MsgBox("Hello"^)
  echo Do
  echo MsgBox("Hello"^)
  echo Loop
)>msg2.vbs

Note that for the latter you need to escape closing parentheses inside the block. In this particular case you could just remove them entirely, though:

(
  echo MsgBox "Hello"
  echo Do
  echo MsgBox "Hello"
  echo Loop
)>msg2.vbs

Another option would be using a single echo statement and escaping the line breaks:

>msg2.vbs echo MsgBox "Hello"^

Do^

MsgBox "Hello"^

Loop

Note that the blank lines are required here.

Upvotes: 1

agriffaut
agriffaut

Reputation: 16

Your Batch script actually only append loop to msg2.vbs file on each run..

You should append all 'vbs' lines from your batch file like this:

@echo off
echo msgBox("Hello") > msg2.vbs    :: > creates file if not exists with: msgBox("Hello")
echo do >> msg2.vbs                :: >> appends line with: do
echo msgBox("Hello") >> msg2.vbs   :: >> appends line with: msgBox("Hello") 
echo loop >> msg2.vbs              :: >> appends line with: loop
start msg2.vbs

Upvotes: 0

Related Questions