batch-file Batch and VBS hybrids Run VBS with temporary file(s)


The old-school method for running another script from batch is to echo the script into another location, and then run it.

This method can be represented like this:

@echo off
rem VBS below
    echo your vbs > TempVBS.vbs
    echo other vbs>>TempVBS.vbs
rem End of VBS

cscript //nologo TempVBS.vbs
del /f /s /q TempVBS.vbs

The method above would require lots of echo (vbs) >> TempVBS.vbs, so here's a way to shorten it. (code by Aacini)

@echo off

rem Get the number of the "<resource>" line
for /F "delims=:" %%a in ('findstr /N "<resource>" "%~F0"') do set "start=%%a"

rem Skip such number of lines and show the rest of this file
(for /F "usebackq skip=%start% delims=" %%a in ("%~F0") do echo %%a) > Program.vbs

cscript //nologo Program.vbs
del /f /s /q Program.vbs
exit /b

your vbs
another line of vbs

The last method is by using streams. A file can have a few streams. And every stream can contain different information.

@echo off

    echo vbs >%0:stream1
    rem This command redirect "vbs" into the stream1 of this script, then we can call it later

cscript %0:stream1 //nologo
   rem if you like, you can clear the stream1 of this file by:
   type nul>%0:stream1