How to write VBScript from batch? - batch-file

I have a problem with adding VBScript to a Batch file.
I tried this:
#echo off
echo MsgBox("Hello")
echo Do
echo MsgBox("Hello")
echo Loop >>msg2.vbs
start msg2.vbs
But it gave me an error that I used Loop without Do.
What am I doing wrong?

Your batch file doesn't magically know which lines you want in the VBScript. Either redirect each echo output (as agriffaut suggested), or run the echo statements in a command block and redirect the entire output of that block (so you don't have to append repeatedly):
(
echo MsgBox("Hello"^)
echo Do
echo MsgBox("Hello"^)
echo Loop
)>msg2.vbs
Note that for the latter you need to escape closing parentheses inside the block. In this particular case you could just remove them entirely, though:
(
echo MsgBox "Hello"
echo Do
echo MsgBox "Hello"
echo Loop
)>msg2.vbs
Another option would be using a single echo statement and escaping the line breaks:
>msg2.vbs echo MsgBox "Hello"^
Do^
MsgBox "Hello"^
Loop
Note that the blank lines are required here.

Your Batch script actually only append loop to msg2.vbs file on each run..
You should append all 'vbs' lines from your batch file like this:
#echo off
echo msgBox("Hello") > msg2.vbs :: > creates file if not exists with: msgBox("Hello")
echo do >> msg2.vbs :: >> appends line with: do
echo msgBox("Hello") >> msg2.vbs :: >> appends line with: msgBox("Hello")
echo loop >> msg2.vbs :: >> appends line with: loop
start msg2.vbs

Related

Why is 'echo on' not effective within an if clause in bat script?

With the following code,
echo off
if 1==1 (
echo on
pwd
)
I am expecting the following output,
C:\> echo off
pwd
C:/
but I am getting these.
C:\> echo off
C:/
Why is 'pwd' missing after I turn on echo again in the if clause?
Full code:
:: Read 1.
echo off
:: Echo off here and will affect Read 2.
#echo
:: Read 2.
if 1==1 (
echo on
cd
)
:: Echo on here and will affect Read 3.
#echo
:: Read 3.
cd
3 reads from the interpreter are the focus of this code.
#echo is ignored as a actual read as it is just to show
the current echo state for testing.
The 1st read is:
echo off
As the script starts as default with echo on, then this read will be displayed with echo on.
The 2nd read is:
if 1==1 (
echo on
cd
)
The parentheses causes a multiline code block so it is read as one read.
The execution of the echo on will have no effect until the next read.
It is too late for the echo on in this code block to affect the
current read as it has already been read.
The 3rd read is:
cd
The echo on of the 2nd read will affect the 3rd read and will echo
the command cd before executing the command.

making a batch file that creates a specific vbs file

im making a batch file that creates a vbs script, which sends key inputs, but this doesn't seem to echo the "%" into the script.
echo set shell = createobject ("wscript.shell")> tempoary.vbs
echo shell.SendKeys "%f" >> tempoary.vbs
start tempoary.vbs
% is used for variables in a batch file and so your batch program is attempting to substitute your variable %f with it's value. Of course there is no such variable, so you are getting nothing.
You should be able to escape the % so it is treated as a literal percent:
echo shell.SendKeys "%%f" >> tempoary.vbs
You should write it like this :
To escape special character like ) you should add a carret ^)
To escape special character like % you should add %%
further reading : Escape Characters
#echo off
(
echo set shell = createobject ("wscript.shell"^)
echo shell.SendKeys "%%f"
)> tempoary.vbs
start "" tempoary.vbs

Echo **Echo 3> args.txt** >X.bat, how do i fix this code?

Echo Echo 3> args.txt >X.bat
I'm trying to place this line of code ( Echo 3> args.txt ) into a batch file named X, but what ends up happening
: X Batch File
echo
:Args text file
There is nothing inside
It place echo (Just echo) into x.batch and creates a blank args text file.
How do I fix the code so it will place Echo 3> args.txt into x batch?
Use
Echo Echo 3^> args.txt>X.bat
As > has a special meaning (redirect), it must be escaped with ^ to be interpreted as literal character.
One more hint:
Insert no space before >X.bat or this space would be appended to end of line in the created batch file.
cmd.exe executes this line as
Echo Echo 3> args.txt 1>X.bat
See also Microsoft's TechNet article Using command redirection operators.
EDIT:
If environment variable password could contain also special characters as listed at end of help output in a command prompt window after running cmd.exe /?, it would be even better to use:
#echo off
setlocal EnableDelayedExpansion
set "password=<hello>"
echo echo var1=!password!^>args.txt>>Enter_PassCode.bat
endlocal
The delayed environment variable expansion is explained in help of command set output by running set /? or help set. Delayed expansion is used here to avoid a syntax error on running this batch file because of < and > in string of environment variable password.
This example would append to Enter_PassCode.bat the line
echo var1=<hello>>args.txt
Executing this batch file would of course result again in an error on execution for <hello> as password.
A solution for making Enter_PassCode.bat executable without any error would be:
#echo off
setlocal EnableDelayedExpansion
set "password=<hello>"
echo #echo off>Enter_PassCode.bat
echo setlocal EnableDelayedExpansion>>Enter_PassCode.bat
echo set "password=!password!">>Enter_PassCode.bat
endlocal
echo echo var1=!password!^>args.txt>>Enter_PassCode.bat
echo endlocal>>Enter_PassCode.bat
This batch code produces Enter_PassCode.bat with content
#echo off
setlocal EnableDelayedExpansion
set "password=<hello>"
echo var1=!password!>args.txt
endlocal

taking and writing variables for a batch file

Basically here's what I want: A batch file that prompts the user to set a variable,
set /p x=
then the batch file writes the variable to a file of some sort (abc.txt) Then later on, in a different batch file, the program retrieves the variable from the text document, and sets it as %x% again for whatever use. If there are any questions, or if I'm not clear enough, please comment, and I will revise. thanks.
In Batch Files, you can redirect input and ouput using < and > respectively.
Input.bat
#echo off
:: Take input and set value to x
set /p "x=: "
:: Print out the value of x to the screen, but redirect this to a text file
Echo %x% >> abc.txt
Echo EOF & Pause & Exit
Read.bat
#echo off
:: Set x to the first line in abc.txt
set /p x=< abc.txt
Echo First Line of abc.txt: %x%
Echo.
:: Set x to last line in abc.txt, incase it is multi-line file
for /f "delims=" %%a in (abc.txt) do (set x=%%a)
Echo Last Line of abc.txt: %x%
Echo.
Echo EOF & Pause & Exit
That should help you understand.
Mona.
I figured out a way that works best for me.
So I have the variable %x%, right? I got it from this:
set /p x=
then I write a mini-batch file.
echo set y=%x% >> abc.bat
then later on in a different script, I can use
call abc.bat
the variable y will be the value that I had in the origional batch script.

Print Batch results to a text file?

I created a batch file to lookup my external ip.
and it works well .
This is the code.
#echo off
>"%temp%\ip.vbs" echo Set objHTTP = CreateObject("MSXML2.XMLHTTP")
>>"%temp%\ip.vbs" echo Call objHTTP.Open("GET", "http://checkip.dyndns.org", False)
>>"%temp%\ip.vbs" echo objHTTP.Send()
>>"%temp%\ip.vbs" echo strHTML = objHTTP.ResponseText
>>"%temp%\ip.vbs" echo wscript.echo strHTML
for /f "tokens=7 delims=:<" %%a in ('cscript /nologo "%temp%\ip.vbs"') do set ip=%%a
echo %ip:~1%
pause
What i want is to Print the results to a text file named "IPlog.txt"
and every time i run the bat file it has to do the same thing and print the new results to the next line in the text file. So please can anyone help me with this.
... or change your
echo %ip:~1%
to
echo %ip:~1% >>IPlog.txt
to run your batch without the additional " >>IPlog.txt "
Please remove the pause command from your code and run the batch-file like this
mybatch.bat >> IPlog.txt
This will append the resulting ip address on to the log file IPLog.txt every time you run this batch file.

Resources