2

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 Answers 2

4

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"
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much for your fast answer. I have got a NEW problem. The saved vbs file is working as well. But when I open the batch file then it doesn't start the vbs file, it just simply starts CMD, I don't know why, can you help me please?
You're welcome,but since you don't supply the code of the batch-file which attempts to call that vbs that you mean then I can't know what and where is the problem, maybe you would consider to mark this answer as a'accepted' and to create a new question about your new problem, I don't understanded your new question sorry, thanks for read and forgive my English.
Okay I think I marked this as a Accepted. I wrote a new question, so hopefully you can see what going on. If you want you can try it on your own.
4

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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.