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.

  • https://stackoverflow.com/questions/9074476/is-it-possible-to-embed-and-execute-vbscript-within-a-batch-file-without-using-a?noredirect=1 – npocmaka Feb 23 '18 at 14:12
  • Possible duplicate of [Is it possible to embed and execute VBScript within a batch file without using a temporary file?](https://stackoverflow.com/questions/9074476/is-it-possible-to-embed-and-execute-vbscript-within-a-batch-file-without-using-a) –  Feb 23 '18 at 21:43

2 Answers2

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%
npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • 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. – Christian Sirolli Feb 23 '18 at 14:20
  • @ChristianSirolli - yes you can . Is the error comes from the vbscript code? May be it expects some command line arguments? – npocmaka Feb 23 '18 at 14:22
  • 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. – Christian Sirolli Feb 23 '18 at 23:22
  • @ChristianSirolli - is this line so secret? Have you tried it in a `.vbs` file to see if the problem is not in the vbscript itself? – npocmaka Feb 23 '18 at 23:30
  • As I said, the line works fine in a VBS file. Now I just noticed an edit you make to the code block, where you added a `%*`. I don't think I saw that earlier when I tested it. – Christian Sirolli Feb 24 '18 at 01:10
  • Note: the line I am using is this: `msgbox "Thanks for using this script.", vbInformation+VbMsgBoxSETForeground, "EXIT"`. It works in a VBScript file. – Christian Sirolli Feb 24 '18 at 01:12
  • Here is the error I am getting: ` – Christian Sirolli Feb 24 '18 at 01:35
  • here is the code I am using that it is throwing an error for: `@ECHO OFF cscript //nologo "%~f0?.wsf" %* @EXIT /B ` – Christian Sirolli Feb 24 '18 at 01:36
  • 1
    Issue was resolved by adding the in the script – Christian Sirolli Feb 24 '18 at 01:42
  • Now, from experimenting, it seems that `%~f0` from CD. Is there a better way that gets the true file path? This broke it at one point until I set the original directory at the beginning of the script as a variable, which became the current directory when the script exited and ran the vbscript. – Christian Sirolli Feb 24 '18 at 01:49
  • 1
    @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. – npocmaka Feb 24 '18 at 21:42
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>