lin zh
lin zh

Reputation: 13

Unable to use batch script to write to a .vbs script

I am using a batch script to create and write into a .vbs file. The file is named invisible.vbs.

This is the batch script that creates and write the .vbs script.

echo.>"C:\Users\LinFamily\Desktop\invisible.vbs"
echo CreateObject("Wscript.Shell").Run &WScript.Arguments(0)&,0,False >> invisible.vbs

The first line of code runs fine and invisible.vbs is created, but the script does not write the second line of code to invisible.vbs , instead, it tries to run it like it is a code.

I used quotation marks,

"CreateObject("Wscript.Shell").Run &WScript.Arguments(0)&,0,False" >> invisible.vbs

and the code gets written onto invisible.vbs, but the quotation marks get written onto invisible.vbs too, and that is not what I want. Invisible.vbs will not work if there is a quotation mark.

I have tried using parenthesis but that also does not work.

Is there anyway to write the code to invisble.vbs without the quotation at the end? Thanks for the help!

Upvotes: 1

Views: 141

Answers (2)

T3RR0R
T3RR0R

Reputation: 2951

special characters ) and & need ^ escaping.

rem [ write all lines in one operation using parenthesis ]
> "invisible.vbs" (
echo(CreateObject("Wscript.Shell"^).Run ^&WScript.Arguments(0^)^&,0,False
)

Upvotes: 1

user14510432
user14510432

Reputation: 11

There are ampersands in your code that do nothing, that aren't in any standard for VBScript. In short voodoo code. It is illegal VBScript code.

Ampersands and redirection are evaluated by CMD exe before other commands. So it won't write them.

So two reasons why the code don't work.

If you had tried to run that VBScript is says Line 1 Character 35 (&) Syntax Error.

Upvotes: 1

Related Questions