SoFunction
Updated on 2025-03-10

bat, vbs, js native mixed compilation (a bat can execute vbs, js code)

I found that mshta would parse the file pointed to by file:// protocol as html (Note: IUnknown and happyxxdhaha reminded me that absolute paths must be used here, otherwise they will not be executed). I suddenly had ten thousand grass horses running by in my heart. It turned out that such a simple answer was right by my side, but I missed it for four years.

Basic framework:

Copy the codeThe code is as follows:

<!-- : bathome
@echo off
echo I'm Batch!
mshta "file://%~f0"
pause&exit
Use comment tags to encompass the batch part, assuming that the batch part cannot have the ending character of the comment tag.
-->
<script language=vbs>
Msgbox "I'm VBScript!"
</script>
<script>
alert("I'm JavaScript!")
</script>
<script>close()</script>

In fact, the file:// protocol name can be omitted, and if the interface is not needed, you can do without commenting on the tag:

Copy the codeThe code is as follows:

@echo off
echo I'm Batch!
mshta "%~f0" <nul
pause&exit
After the batch processing part, a string of > should be added, and the number should be more than the < mentioned in the previous article. mshta can distinguish which labels are
Moreover, when you get redirect input from a file, it is recommended to add double quotes, such as <"script"
>>>>>>>>>>>>>>
<script language=vbs>
Msgbox "I'm VBScript!"
</script>
<script>
alert("I'm JavaScript!")
</script>
<script>close()</script>

Another way of writing with goto may be more intuitive:

Copy the codeThe code is as follows:

@goto :bat
<script language=vbs>
Msgbox "I'm VBScript!"
</script>
<script>
alert("I'm JavaScript!")
</script>
<script>close()</script>
:bat
@echo off
echo I'm Batch!
mshta "%~f0" <nul
pause&exit

It should be noted that the host here is mshta, so the methods and properties of WSH hosts are not supported (see the following article for the alternatives to some properties or methods).
but! What's wrong with mshta!
Native support setTimeout
Native support for iframe
Native support dom
Native support for javascript, vbscript barrier-free interaction
Native support Ajax
Native support for loading external scripts
Native support to select files in window
Natively support complex page interactions
...
With so many conveniences, what else should I care about?
First published in Batch Processing Home
------------------------------------------------------------------------------------------------------------
For some knowledge about mshta hosts, please refer to:/en-us/library/ms536495(VS.85).aspx
Thanks to xiaopo for literacy, I realized that the alliance has already had the prototype of the mshta plan:/forum/?tid=39655, go back to the mirror site to see if there is any further development

Copy the codeThe code is as follows:

:<!--
::::::::::::::::::::::::::::::::BAT::::::::::::::::::::::::::::::::

:::::::1.BAT code before execution of HTML code:::::::::
@echo off
call :e Starting mshta...
pause
:::::::1.BAT code before execution of HTML code:::::::::

::Execute HTML code:
start mshta %0

::::::::2.BAT code after execution of HTML code:::::::::
call :e Mshta is executing HTML codes...
pause
::::::::2.BAT code after execution of HTML code:::::::::

::Exit BAT:
exit/b

::::::::BAT function definition part::::::
:e
echo %*
goto :eof
::::::::BAT function definition part::::::

::::::::::::::::::::::::::::::::BAT::::::::::::::::::::::::::::::::
-->

<!--This sentence is used to clear the first line:->
<script>=""</script>

<!--------------------------HTML-------------------------->
<body onkeypress=()>
<hr color=red>
<marquee><font color=green>HTML Codes</font></marquee>
<hr color=red>
<!--------------------------HTML-------------------------->

<!-- BAT & HTML {s11ss@/forum 2008-4-22}
Idea: When this file is executed as a BAT file, it exits without executing the HTML code part;
When this file is executed as an HTML file, the BAT code part is commented and will not be executed.
-->