2

Say I have a batch script and I would like to use VBScript to perform a certain task. It is a one liner in VBScript, and I would prefer to only have one file (which is the batch script) without any temporary files. Is this possible?

I know how to create a VBScript file from a batch script, run it and then delete it, but I don't want to do that because that involves creating an extra file.

2

2 Answers 2

2

Here are two ways - you can check for more here. The best way according to me (save whole code bellow as a .bat):

<!-- :
@echo off
echo "Batch code here"
cscript //nologo "%~f0?.wsf" %*
exit /b
-->
<job><script language="VBScript">
  WScript.Echo "VBScript output called by batch"
  'Uncoment if you want to test the command line arguments
  'WScript.Echo (WScript.Arguments.Item(0))
</script></job>

You can also use mshta (mind that this the browser based vbscript and you have no access to the WScript object):

@echo off
setlocal
:: Define simple batch "macros" to implement VBS within batch
set "vbsBegin=mshta vbscript:Execute("createobject(""scripting.filesystemobject"")"
set "vbsBegin=%vbsBegin%.GetStandardStream(1).write("
set ^"vbsEnd=):close"^)"

:: float numbers math
%vbsBegin% 2.5+3.1 %vbsEnd% |more

for /f %%a in ('%vbsBegin% 2.5+3.1 %vbsEnd%') do set result=%%a
echo %result%
Sign up to request clarification or add additional context in comments.

11 Comments

Am I able to replace the WScript.Echo line with my own code? I assume I would. I did that and added that into my own script and it is saying it expected a valid name. I doubt the issue is with the code I added but with the way the code is being called.
@ChristianSirolli - yes you can . Is the error comes from the vbscript code? May be it expects some command line arguments?
I used the msgbox command like I would in a normal VBScript, which I know works in a normal VBScript. The error appears in the command line console, like a normal command line error does, and I believe the error is thrown when it runs the cscript line.
Issue was resolved by adding the <!-- and --> in the script
@ChristianSirolli - sorry haven't check the SO for a while. %~f0 is the path to batch file. with vbscript you can use Wscript.ScriptFullName . If you are running it as admin it will start in system32 . You can try with setting cd /d "%~dp0" at the begining of your script to be sure that directory will be where the script is.
|
0

You Can Use this

<!-- : Begin batch script
@echo off
cscript //nologo "%~f0?.wsf" %1
exit /b

----- Begin wsf script --->
<job><script language="VBScript">
:: Remove this line and Write the Vbscript code here or it will not work
</script></job>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.