[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 297
  • Last Modified:

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
0
TomMonkey
Asked:
TomMonkey
  • 2
  • 2
  • 2
1 Solution
 
MSE-dwellsCommented:
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?
0
 
TomMonkeyAuthor Commented:
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?
0
 
SteveGTRCommented:
I'd uncomment out this:

WScript.echo( strFolder );

And run this:

@echo off

setlocal

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

echo %var%
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
MSE-dwellsCommented:
Sheesh, nobody likes you Steve ;0)  ...  teasing of course.

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

Thanks to both of you for quick responses.

Tom
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now