Link to home
Start Free TrialLog in
Avatar of TomMonkey
TomMonkey

asked on

Getting Variable from JScript in Batch File

Hi Guys,

I am trying to write some batch files and have got a bit stuck.  The batch program calles a JScript prgram to get the name of a folder created three days ago:

// Create Date object
objToday = new Date();

// Get today's year, month and day
year  = objToday.getFullYear();
month = objToday.getMonth() + 1;
if ( month < 10 )
      {
      month = "0" + month;
      };
day   = objToday.getDate();
if ( day < 10 )
      {
      day = "0" + day;
      };

// Format output for today
strToday = "Today:      " + year + month + day + "  (" + day;
strToday = strToday + "/" + month + "/" + year + ")";

// Get current date in milliseconds since January 1, 1970
today = objToday.valueOf();

// Subtract 3 days
threeDay = 1000 * 24 * 60 * 60 * 3;
yesterday = today - threeDay;
objToday.setTime(yesterday);

// Get threeDay's year, month and day
year  = objToday.getFullYear();
month = objToday.getMonth() + 1;
if ( month < 10 )
      {
      month = "0" + month;
      };
day   = objToday.getDate();
if ( day < 10 )
      {
      day = "0" + day;
      };

// Format output
strFolder = "" + year + month + day;
//display output
//WScript.echo( strFolder );
WScript.quit( strFolder );

The problem is I can't get the variable strFolder to be returned to the batch script.  I have tried:

OldFolder.js %*

echo %strFolder%

and

for /f "tokens=*" %%i in ('OldFolder.js') do set var=%%i

echo %var%

But neither have worked.  What am I doing wrong please?

Cheers
Tom
Avatar of MSE-dwells
MSE-dwells
Flag of Yemen image

There's no means I'm aware of to _return_ the value in the classic sense of that term.  You should be able to capture the output by having the JScript write it to the console (something that appears commented right now) or to a file.

If you run oldfolder.js at the command prompt, what do you receive?
Avatar of TomMonkey
TomMonkey

ASKER

Hi MSE-dwells,

I have tried running it at the command prompt and the output always appears in a dialog box.  How do I get it to appear just in the console?
ASKER CERTIFIED SOLUTION
Avatar of SteveGTR
SteveGTR
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sheesh, nobody likes you Steve ;0)  ...  teasing of course.

Do exactly as SteveGTR describes ... it reduced my typing anyway ... hehehe!
TomMonkey tapped into what little knowledge I know about these scripts ;)
Brilliant, works like a charm.

Thanks to both of you for quick responses.

Tom