使用臨時檔案執行 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