1

I know it sounds strange, but my idea is creating a single text file which contains a VBScript and a Batch Script within it, i.e. if I run that script as a VBScript, it calls itself as a Batch script, and that batch script do some cmd.exe tasks.

I know you can use the .ShellExecute method and then run multiple commads using CMD /C [...], but this is a little difficult to read.

My try was:

If False Then
  goto batchLines
End If
[...VBScript continues here...]
Wscript.Quit
:batchLines
[...batch Script starts here...]

But as spected, it does not worked at all.

Pablo
  • 33
  • if there was then it might be more tricky than how shell execute with cmd /c looks Maybe it's possible to put strings across multiple lines, and then do shell execute cmd /c str or build up some string str that'd make up a batch script, write it to a temp bat file, then do shell execute cmd /c batchscript. It's not what you ask for exactly, but it's just another way of getting the batch and the vbscript – barlop Jul 08 '15 at 22:06
  • It can easily be done in reverse. See http://stackoverflow.com/q/9074476/1012053 - in particular, pay attention to the WSF solution. Perhaps you can turn that on its head to have the VBS call itself as a batch script, but the script must have a .BAT (or .CMD) extension if you want it to run as batch. This means you must invoke the VBS using CSCRIPT with the //E:VBS option. – dbenham Jul 09 '15 at 04:09
  • Here is a hybrid jscript/batch written by dbenham/dave benham. jrepl.bat http://www.dostips.com/forum/viewtopic.php?f=3&t=6044 – barlop Jul 13 '15 at 05:44

1 Answers1

-1

The opposite is possible (making a batch which runs VB script) this is an example.

@set @junk=1 /*
@echo off
REM Add Batch Stuff Here
if "%1" == "MOARBATCH" goto :MOARBATCH
cscript //nologo //E:jscript %~f0 %*
goto :eof */
// Add VBS stuff here
Wscript.Quit
/*
:MOARBATCH
REM More Batch Stuff Here
REM */

You can go nuts with this but AFAIK you need to begin with a batch file in order to take advantage of the Batch file handling quirks which this uses. BTW, this is often called a hybrid batch file.

krowe
  • 5,523
  • thank you. I wanted to make a script like this, because I needed to run a batch requesting higher privileges (run as administrator). So, could the vbScript request higher privileges, then run "console commands" (without opening a new console window)? – Pablo Jul 09 '15 at 00:17
  • 1
    By the way, I noticed that you use Jscript and also a feature of this language (multiline comment), so it seems this script should be written in javascript only. – Pablo Jul 09 '15 at 00:23
  • VBScript hybrid batch files are possible but they are even more hackish. If at all possible I'd go with JScript. You may prefer to use an all Batch solution. I was going to write one for you when I found one already made: http://stackoverflow.com/a/28467343/932549 – krowe Jul 09 '15 at 11:51
  • Since it is calling the JScript engine, does this even work with VBScript? – NetMage Mar 25 '19 at 21:21