Everything here below is correct. However during discussions some parts of this message became obsolete.
The resulting message accumulating all good points of this thread is located by the following link:
viewtopic.php?p=65390#p65390
Briefly. In this thread is discussed the script generating scripts "two-in-one".
Such kind of scripts is called "hybrids", because they unite both batch and another language, so these scripts are executed by CMD and other interpreters.
Also I'd like to introduce the term "chimera". Chimera differs from hybrid in that it requires additional settings to run chimera (for instance, temp files, some environment variables, some undocumented features and/or tricks).
* * * * * *
js in cmd/bat
these solutions are world-wide and well-known.
Code: Select all
@set @x=0 /*
@echo off
cscript /nologo /e:javascript %0
goto:EOF */
WScript.Echo('Hello from JScript');
Code: Select all
@if (true == false) @end /*
@echo off
cscript //nologo //e:javascript "%~dpnx0" %*
goto :EOF */
WScript.Echo('Hello from JScript');
this is the first version. It uses the trick to clean the resulting page on from the wrong markups.
Code: Select all
:<nul>nul
@echo off
start mshta "%~f0"
goto :EOF
<script type="text/javascript">
document.body.innerHTML = '';
</script>
<html>
<head>
<title>HTML-in-BATCH</title>
</head>
<body>
<h1>Hello, world!</h1>
</body>
</html>
Code: Select all
<!-- :
@echo off
start "" mshta.exe "%~f0"
exit /b
-->
<html>
<head><title>HTA window</title></head>
<body><h1>Hello, world!</h1></body>
</html>
Code: Select all
@if (true == false) @end /*!
@echo off
mshta "about:<script src='file://%~f0'></script><script>close()</script>" %*
goto :EOF */
alert("Hello, world!");
This is not true hybrid because it uses a js-layer between cmd and vbs. In fact this is js-in-cmd solution running vbs within js. Take a look: two ways similar each other (taken from http://forum.script-coding.com/viewtopi ... 691#p79691)
Code: Select all
@if (true == false) @end /*
@echo off
set "SYSDIR=SysWOW64"
if "%PROCESSOR_ARCHITECTURE%" == "x86" if not defined PROCESSOR_ARCHITEW6432 set "SYSDIR=System32"
"%WINDIR%\%SYSDIR%\cscript.exe" //nologo //e:javascript "%~f0" %*
goto :EOF */
(function(readFile, code)
{
var e;
try {
var vb = new ActiveXObject('MSScriptControl.ScriptControl');
vb.Language = 'VBScript';
vb.AddObject('WScript', WScript, true);
vb.AddCode(code);
} catch(e) {
var file = readFile();
var prologLen = file.slice(0, file.indexOf(code)).split('\n').length;
var vbe = vb.Error;
WScript.Echo(
WScript.ScriptFullName +
'(' + ( prologLen + vbe.Line - 1 ) + ', ' + vbe.Column + ') ' +
vbe.Source + ': ' + vbe.Description);
}
})(
function()
{
var fso = new ActiveXObject('Scripting.FileSystemObject');
var f = fso.OpenTextFile(WScript.ScriptFullName, 1, true);
var file = f.ReadAll();
f.Close();
return file;
},
(function()
{
return arguments.callee.toString().replace(/^[\s\S]+\/\*|\*\/[\s\S]+$/g, '');
/* ' VBScript
WScript.Echo "Hello, world!"
*/
})());
Code: Select all
@if (true == false) @end /*
@echo off
set "SYSDIR=SysWOW64"
if "%PROCESSOR_ARCHITECTURE%" == "x86" if not defined PROCESSOR_ARCHITEW6432 set "SYSDIR=System32"
"%WINDIR%\%SYSDIR%\cscript.exe" //nologo //e:javascript "%~f0" %*
goto :EOF */
var prologLen = 0;
var vb = new ActiveXObject('MSScriptControl.ScriptControl');
vb.Language = 'VBScript';
vb.AddObject('WScript', WScript, true);
try {
vb.AddCode((function () {
var fso = new ActiveXObject("Scripting.FileSystemObject");
var f = fso.OpenTextFile(WScript.ScriptFullName, 1, false);
do prologLen++; while (f.ReadLine() != "/* ' VBScript");
var code = f.ReadAll();
f.Close();
return code;
})());
} catch (e) {
var vbe = vb.Error;
WScript.StdErr.WriteLine(
WScript.ScriptFullName +
'(' + ( prologLen + vbe.Line ) + ', ' + vbe.Column + ') ' +
vbe.Source + ': ' + vbe.Description);
}
/* ' VBScript
WScript.Echo "Hello, world!"
'*/