@rem http://computer-technology.hateblo.jp/entry/20131025/p1
setlocal
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
@rem setlocal DISABLEDELAYEDEXPANSION
@rem 説明:
@rem Template of JScript in Batch file.
@rem Usage:
@rem "Hello JScript in Batch file.js.bat" "BAT_Output_Args" " 1 " " 22 二弐 " " 333 333 333 "
@rem "Hello JScript in Batch file.js.bat" "BAT_Output_Hello_Batch" " 1 " " 22 二弐 " " 333 333 333 "
@rem "Hello JScript in Batch file.js.bat" "BAT_Output_Version" " 1 " " 22 二弐 " " 333 333 333 "
@rem "Hello JScript in Batch file.js.bat" "JS_Output_Args" " 1 " " 22 二弐 " " 333 333 333 "
@rem "Hello JScript in Batch file.js.bat" "JS_Output_Hello_JScript" " 1 " " 22 二弐 " " 333 333 333 "
@rem "Hello JScript in Batch file.js.bat" "JS_Output_Version" " 1 " " 22 二弐 " " 333 333 333 "
@rem "Hello JScript in Batch file.js.bat"
@rem "Hello JScript in Batch file.js.bat" "xxxxxxxx" " 1 " " 22 二弐 " " 333 333 333 "
@rem echo errorlevel=%errorlevel%
@rem ####################
@rem Batch.
@rem ####################
@rem ....................
@rem Batch main.
@rem ....................
set rv=11
if "%~1"=="" ( call :L_call_BAT_NoArgs
) else if "%~1"=="BAT_Output_Args" ( call :L_BAT_Output_Args %*
) else if "%~1"=="BAT_Output_Version" ( call :L_BAT_Output_Version %*
) else if "%~1"=="BAT_Output_Hello_Batch" ( call :L_BAT_Output_Hello_Batch %*
) else if "%~1"=="JS_Output_Args" ( call :L_call_JS %*
) else if "%~1"=="JS_Output_Hello_JScript" ( call :L_call_JS %*
) else if "%~1"=="JS_Output_Version" ( call :L_call_JS %*
) else ( call :L_call_BAT_OtherArgs %*
)
@rem ....................
@rem Exit.
@rem ....................
@rem echo.
@rem pause
@rem echo 終了するには何かキーを押してください . . .
@rem pause>nul
exit /b !rv!
goto :eof
@rem ....................
@rem No args.
@rem ....................
:L_call_BAT_NoArgs
echo BAT %0 %* ....................
call :L_call_JS
set /a rv=!errorlevel!+9
goto :eof
@rem ....................
@rem Output Args.
@rem ....................
:L_BAT_Output_Args
echo BAT %0 %* ....................
set /a y=0
for %%z in (
%*
) do (
set /a y=!y!+1
echo BAT %%!y!=%%~z.
)
set /a rv=!errorlevel!+1
goto :eof
@rem ....................
@rem Output Hello Batch.
@rem ....................
:L_BAT_Output_Hello_Batch
echo BAT %0 %* ....................
echo ハロー Batch.
set /a rv=!errorlevel!+2
goto :eof
@rem ....................
@rem Output Version.
@rem ....................
:L_BAT_Output_Version
echo BAT %0 %* ....................
ver
set /a rv=!errorlevel!+3
goto :eof
@rem ....................
@rem Other args.
@rem ....................
:L_call_BAT_OtherArgs
echo BAT %0 %* ....................
call :L_call_JS %*
set /a rv=!errorlevel!+8
goto :eof
@rem ....................
@rem Echo ErrorLevel.
@rem ....................
:L_echoErrorLevel
@rem echo BAT L_echoErrorLevel ....................
echo BAT %0 R errorlevel=!errorlevel!
@rem echo.
goto :eof
@rem ....................
@rem Call JScript.
@rem ....................
:L_call_JS
echo BAT %0 %* ....................
set "jsargs=%*"
if "!jsargs!"=="" (
cscript.exe //nologo //e:JScript "%~f0"
) else (
cscript.exe //nologo //e:JScript "%~f0" !jsargs!
)
call :L_echoErrorLevel
goto :eof
@end
// ####################
// JScript.
// ####################
// ....................
// No args.
// ....................
function NoArgs() {
WScript.Echo("JS NoArgs ....................");
// WScript.Echo("JS Argc=" + WScript.Arguments.length + ".");
WScript.Echo("JS There are no arguments.");
return 9;
}
// ....................
// Output Args.
// ....................
function Output_Args(jsargs) {
WScript.Echo("JS Output_Args ....................");
WScript.Echo("JS [" + jsargs.length + "]" + jsargs + ".");
for(var idx = 0; idx < jsargs.length; ++idx) {
WScript.Echo("JS Args[" + idx + "]=" + jsargs[idx] + ".");
}
return 1;
}
// ....................
// Output Hello JScript.
// ....................
function Output_Hello_JScript() {
WScript.Echo("JS Output_Hello_JScript ....................");
WScript.Echo("JS ハロー, JScript World!!");
return 2;
}
// ....................
// Output Version.
// ....................
function Output_Version() {
WScript.Echo("JS Output_Version ....................");
WScript.Echo("JS ScriptEngineMajorVersion()=" + ScriptEngineMajorVersion());
WScript.Echo("JS @_jscript_version=" + @_jscript_version);
return 3;
}
// ....................
// Other args.
// ....................
function OtherArgs(jsargs) {
WScript.Echo("JS OtherArgs ....................");
WScript.Echo("JS [" + jsargs.length + "]" + jsargs + ".");
for(var idx = 0; idx < jsargs.length; ++idx) {
WScript.Echo("JS Args[" + idx + "]=" + jsargs[idx] + ".");
}
return 8;
}
// --------------------
// JScript main.
// --------------------
var rv = 30;
if (WScript.Arguments.length == 0) {
rv = rv + NoArgs();
} else {
var args = new Array();
for (var idx = 0; idx < WScript.Arguments.length; ++idx) {
args[idx] = WScript.Arguments(idx);
}
var ary = new Array();
ary = args.slice(0, args.length);
if (0 == 1) {
rv = rv + 1;
} else if ((WScript.Arguments.length >= 1) && (WScript.Arguments(0) == "JS_Output_Args")) {
ary = args.slice(1, args.length);
rv = rv + Output_Args(ary);
} else if (WScript.Arguments(0) == "JS_Output_Hello_JScript") {
rv = rv + Output_Hello_JScript();
} else if (WScript.Arguments(0) == "JS_Output_Version") {
rv = rv + Output_Version();
} else {
rv = rv + OtherArgs(ary);
}
}
WScript.quit(rv);