How to save vbs in batch file one command in multiple lines

偶尔善良 提交于 2019-12-11 01:37:47

问题


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.


回答1:


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"



回答2:


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)


来源:https://stackoverflow.com/questions/19471117/how-to-save-vbs-in-batch-file-one-command-in-multiple-lines

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!