Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

JScript From CMD Script Without External Files

14 views
Skip to first unread message

Frank P. Westlake

unread,
Nov 22, 2009, 1:36:28 PM11/22/09
to
Here is a method of executing JScript from CMD without first writing the
JScript to an external file. It is very similar to the same procedure
from HTML source.

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


Frank P. Westlake

unread,
Nov 22, 2009, 1:45:50 PM11/22/09
to
"Frank P. Westlake" news:hec0et$2n6$1...@news.albasani.net...

> 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


Frank P. Westlake

unread,
Nov 22, 2009, 2:51:57 PM11/22/09
to
"Frank P. Westlake" news:hec0et$2n6$1...@news.albasani.net...

> 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


01MDM

unread,
Nov 22, 2009, 3:07:48 PM11/22/09
to
This is very old method:

@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")

Frank P. Westlake

unread,
Nov 22, 2009, 3:23:24 PM11/22/09
to
"01MDM"
news:a4fd32f3-2bdb-4999...@g27g2000yqn.googlegroups.com...

> This is very old method:


Thanks. I wish I knew it very old ago.

Frank


T3X

unread,
Nov 22, 2009, 4:02:57 PM11/22/09
to
On Nov 22, 6:36 pm, "Frank P. Westlake" <frank.westl...@yahoo.com>
wrote:

>   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".

You can also use @if, which doesn't require setting any variables. See
my message from August for an example:

http://groups.google.com/group/alt.msdos.batch.nt/browse_thread/thread/49d6901df4c4a841/6f4a942dd953db8f?hl=en#6f4a942dd953db8f

Cheers,

Tomek

sw0rdfish

unread,
Nov 22, 2009, 8:50:35 PM11/22/09
to
On Nov 23, 2:36 am, "Frank P. Westlake" <frank.westl...@yahoo.com>
wrote:

> Here is a method of executing JScript from CMD without first writing the
> JScript to an external file. It is very similar to the same procedure
> from HTML source.

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.

Frank P. Westlake

unread,
Nov 23, 2009, 3:58:51 AM11/23/09
to
"sw0rdfish"
news:b06b991d-1274-48c0...@g10g2000pri.googlegroups.com...

> 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�


Dr J R Stockton

unread,
Nov 23, 2009, 11:22:13 AM11/23/09
to
In alt.msdos.batch.nt message <hec0et$2n6$1...@news.albasani.net>, Sun, 22
Nov 2009 10:36:28, Frank P. Westlake <frank.w...@yahoo.com> posted:

> 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.

Frank P. Westlake

unread,
Nov 23, 2009, 2:31:07 PM11/23/09
to
"Dr J R Stockton"
news:cwhDiCJ1...@invalid.uk.co.demon.merlyn.invalid...

> 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


0 new messages