How to save vbs in batch file one command in multi

2019-07-20 05:27发布

I would like to save a vbs file in a batch file like this:

echo set shell = createobject("wscript.shell")
wscript.sleep(1000)
shell.sendkeys("blablabla")
Shell.SendKeys "{Enter}"
wscript.sleep(1000) >"c:\folder\blablabla.vbs"

but the batch file doesn't work. If I do it like this(If I type the vbs code in one line):

echo set shell = createobject("wscript.shell") wscript.sleep(1000) shell.sendkeys("blablabla") Shell.SendKeys "{Enter}" wscript.sleep(1000) >"c:\folder\blablabla.vbs"

then the vbs file doesn't work. So my question is: how can I save the vbs file correctly, without making the batch file think that I want him to do multiple commands.

2条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-07-20 05:41

First choice:

Agroupate the commands you wanr using the agroupation operators () and escape conflictive Batch operators in code using ^.

(
 echo set shell = createobject^("wscript.shell"^)
 echo wscript.sleep^(1000^)
 echo shell.sendkeys^("blablabla"^)
 echo Shell.SendKeys "{Enter}"
 echo wscript.sleep^(1000^)
)>"c:\folder\blablabla.vbs"

Second choice

To concatenate instructions in VBScript you need to use the : operator, then an all-in-one line code should look like this:

echo set shell = createobject^("wscript.shell"^) : wscript.sleep^(1000^) : shell.sendkeys^("blablabla"^) : Shell.SendKeys "{Enter}" : wscript.sleep^(1000^) >"c:\folder\blablabla.vbs"
查看更多
淡お忘
3楼-- · 2019-07-20 05:46

Here is another method. The ::: is harmless in VBS, and works like a comment in batch. What makes it particularly nice is you don't have to worry about escaping any of the VBS code.

I chose ::: because it is unlikely to appear elsewhere in your batch script; whereas : is used in batch labels, and :: is frequently used as a batch comment.

::: set shell = createobject("wscript.shell")
::: wscript.sleep(1000)
::: shell.sendkeys("blablabla")
::: Shell.SendKeys "{Enter}"
::: wscript.sleep(1000)
findstr /b ::: "%~f0" >"c:\folder\blablabla.vbs"

Here are some more options that only require a string to mark the beginning of the VBS, so they may be easier to write if dealing with a significant amount of VBS. Be sure to have exit /b before the VBS.

This second option is slower, and it will strip empty lines as currently written.

@echo off
setlocal
set "vbs=test.vbs"
for /f "delims=:" %%N in ('findstr /nb ":: begin VBS" "%~f0"') do set "skip=%%N"
(for /f usebackq^ skip^=%skip%^ delims^=^ eol^= %%A in ("%~f0") do echo %%A) >"%vbs%"
cscript /nologo "%vbs%"
del "%vbs%"
exit /b

:: begin VBS
set shell = createobject("wscript.shell")
wscript.sleep(1000)
shell.sendkeys("dir *.h*")
Shell.SendKeys "{Enter}"
wscript.sleep(1000)

This third option is fast, but MORE will convert tabs into a string of spaces.

@echo off
setlocal
set "vbs=test.vbs"
for /f "delims=:" %%N in ('findstr /nb ":: begin VBS" "%~f0"') do set "skip=%%N"
more +%skip% "%~f0" >"%vbs%"
cscript /nologo "%vbs%"
del "%vbs%"
exit /b

:: begin VBS
set shell = createobject("wscript.shell")
wscript.sleep(1000)
shell.sendkeys("dir *.h*")
Shell.SendKeys "{Enter}"
wscript.sleep(1000)
查看更多
登录 后发表回答