Roland77
Roland77

Reputation: 15

Batch Job to Write to VBS file

I am trying to use a batch file to write this code to a VBS file but I cannot get a successful output. Can someone help?

This is what I am trying to write to Output.vbs

set w = CreateObject("WScript.Shell")
W.Run chr(34) & "C:\Program Files\Test\Test.bat" & chr(34), 0
set w= Nothing

I tried this:

>"C:\Program Files\Test\Output.vbs" (
echo set w = CreateObject("WScript.Shell")
echo W.Run chr(34) & "C:\Program Files\Test\Test.bat" & chr(34), 0
echo set w= Nothing
)

But the data I am getting in Output.vbs is only this:

set w = CreateObject("WScript.Shell"

It obviously doesn't like these ( and ) Is there a way around this? Not super skilled in CMD other than the basics. I assume I need to escape this somehow?

Any advice?

Thanks,

Upvotes: 0

Views: 141

Answers (1)

Compo
Compo

Reputation: 38613

You need to escape the ampersands and nested closing parentheses, just like this:

@(  Echo Set w = CreateObject("WScript.Shell"^)
    Echo w.Run Chr(34^) ^& "C:\Program Files\Test\Test.bat" ^& Chr(34^), 0
    Echo Set w = Nothing) 1> "C:\Program Files\Test\Output.vbs"

Upvotes: 2

Related Questions