使用临时文件运行 VBS

batch 运行另一个脚本的老派方法是将脚本转换到另一个位置,然后运行它。

此方法可以表示如下:

@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

上面的方法需要很多 echo (vbs) >> TempVBS.vbs,所以这是一种缩短它的方法。 (Aacini 代码)

@echo off
setlocal

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

<resource>
your vbs
another line of vbs

最后一种方法是使用 streams。一个文件可以有几个流。每个流都可以包含不同的信息。

@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