Three simple steps:
1. Begin the CMD script with a command which is common to both scripts
and which makes the remainder of the line inconsequential to CMD. I have
found two: 'REM', which performs as documented for both scripts, and
'@set', which works on my system but it seems that it should be
preceeded by '@cc_on'. 'REM' is undesirable because that first line is
printed to standard output. I use '@set @JScript=1;'. CMD sets
"@JScript" with the value "1;", and JScript sets "@JScript" to the value
"1".
2. Enclose the entire CMD script in JScript comment markers, beginning
at the end of that first line.
3. Follow the CMD script and the JScript comment markers with all
JScript formatted normally as it would be for a normal JScript file.
@set @JScript=1;/*
CMD script in here.
*/
JScript here and below
CSCRIPT.EXE is invoked as normal with the CMD file as the source.
Following is a demonstration which uses a simple tool to call the
JScript code from CMD: the :JScript routine. Call :JScript with the name
of the JScript function to invoke and any parameters for that function
following the name. The function prints its return value; :JScript reads
that and sets the line into an environment variable named after the
function. Two examples are provided; one which accepts parameters and
the other which does not.
@set @JScript=1;/*
:: demo.cmd
:: Demonstrates single source, dual host scripts of CMD/JScript.
:: Frank P. Westlake, 2009-11-22
::
@Echo OFF
SetLocal ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION
Call :JScript GetFormatedDate
Set GetFormatedDate
:: Month and day may be preceeded by '0':
Call :JScript FormatLocaleDate 1970 01 1
Set FormatLocaleDate
Goto :EOF
:: JScript ::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:JScript Function Parameters
SetLocal ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION
For /F "delims=" %%a in (
'CSCRIPT /NOLOGO /E:JScript "%~f0" %*') Do Set "jsReturn=%%a"
EndLocal&Set "%~1=%jsReturn%"
Goto :EOF
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
*/
main:
eval(WScript.Arguments(0)+"()");
// GetFormatedDate ///////////////////////////
//Prints local date.
function GetFormatedDate()
{
var n, s, obj=new Date();
s=obj.getFullYear()+"-";
n=obj.getMonth()+1; if(n<10) s+="0"+n; else s+=n; s+="-";
n=obj.getDate(); if(n<10) s+="0"+n; else s+=n;
WScript.Echo(s);
}
// FormatLocaleDate ////////////////////////
//Prints locale date.
function FormatLocaleDate()
{
var n, s, obj;
switch(WScript.Arguments.length)
{
default:
case 1: obj=new Date(); break;
case 2: obj=new Date(WScript.Arguments(1)); break;
case 4: obj=new Date(WScript.Arguments(1)
, WScript.Arguments(2)-1
, WScript.Arguments(3));
break;
}
WScript.Echo(obj.toLocaleDateString());
}
Frank
> I have found two: 'REM', which performs as documented for
> both scripts...
In would be used as a variable in JScript and REMARK in CMD:
REM=1;/*
It could also be used as a label in JScript:
REM:/*
Frank
Topli otnoshenie
> 1. Begin the CMD script with a command which is common to
> both scripts and which makes the remainder of the line
> inconsequential to CMD.
> I use '@set @JScript=1;'. CMD sets "@JScript" with the
> value "1;"...
I was mistaken. CMD sets "@JScript" to the rest of the line: "1;\*".
That makes it possible to add CMD script to that line following an
ampersand:
@set @JScript=1;/*&CMD script here.
CMD script in here too.
*/
JScript here and below
The line containing the closing JScript comment marker can also be
shared if seperated by "&REM" or "&::".
@set @JScript=1;/*&CMD script here.
CMD script in here too.
CMD script here too.&::*/JScript here too.
JScript here and below
So it can be real busy if you want it to be.
Frank
@set @x=0 /*
@echo off
:: start batch part
echo in batch
cscript //nologo //e:jscript %~nx0
exit /b */
// start Jscript part
WScript.Echo("in script")
> This is very old method:
Thanks. I wish I knew it very old ago.
Frank
You can also use @if, which doesn't require setting any variables. See
my message from August for an example:
Cheers,
Tomek
i think this is trouble some and untidy to maintain. first i would
need to learn batch, then i would need to learn jscript.
I would rather just learn jscript and write everything with jscript
and forget about batch commands.
> i think this is trouble some and untidy to maintain.
It is the same as writing asm within C.
> I would rather just learn jscript and write everything
> with jscript and forget about batch commands.
That is not prohibited. Some people would rather forget C and do
everything in asm. But in general the higher level language requires
less work.
Frank
Una afectuosa salutaci�
> WScript.Echo(obj.toLocaleDateString());
There is a problem. It's not safe, except in an intranet where such
things are checked before installing new versions, to assume that any
system-supplied or imported locale routine will necessarily always give
the format that the actual user wants as "locale".
And the same goes for your own "locale" routine, which gives a specific
format.
Where possible, I suggest naming such routines by the explicit format
that they actually give.
Every place has two times (which may be the same, such as here now);
Local Civil Time and UTC/GMT. I suggest care in using "local" in a
context where it might be misread as or a typo for "locale", and /vice
versa/.
If a document first contains "Local Civil Time (LCT)", then "LCT" can
safely be used thereafter - in most fields - to indicate ordinary clock
time without regard to format, leaving "locale" for format without
regard to offset.
"JScript From CMD Script Without External Files" seems a good FAQ topic,
and would serve as a reminder that something generally better than
VBScript is available.
--
(c) John Stockton, nr London UK. ?@merlyn.demon.co.uk IE7 FF3 Op9 Sf3
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
> There is a problem.
Problems noted. Anyone using the script should view it only as a
demonstration of the JScript encapsulation method and not as containing
good usable JScript routines.
Frank