views:

213

answers:

7

Hi,

Is ther any way to execute javascript from a .bat file or embed the javascript in .bat file.

I need a javascript code to write/read to a file in a local folder.This javascript i should be able to execute it using a .bat.

Is it possible?.

Thanks

SNA

+1  A: 

on Windows, in a batch file, try

cscript jsfile.js

Leon van der Walt
A: 

It is, sort of. On Windows only, of course.

That said, there is probably a better way to do this. What do you need the JavaScript to do? Why does it have to be JavaScript?

Pekka
Is "on Windows only" a problem when we're talking about `.bat` files anyway? I don't think anyone is trying to do that on DOS.
Joachim Sauer
@Joachim pointing out OS limitations is never wrong IMO - true, `.bat` is specific to the Windows world, but shell scripting in general is not.
Pekka
A: 

Short answer: no.

Javascript (when run from a browser) can't access the client resources (this is by design, to avoid security risks).

You can use Javascript outside of the browser, to create scripts, but you should provide more details on what you are trying to do (and what Operative System you expect to run this on).

Also, check this page for more details.

p.marino
A: 

You can run a JScript script from a batch-file with the command-line based scripting host CScript.exe.

You would need the script in a separate file though which you pass as an argument to CScript.exe. If you want to keep everything in a sinle file, you can embed the JScript code in you batch-file, dump it to a temporary file which you pass to CScript.exe and delete the temporary script file afterwards.

There might be more elegant solutions (hopefully)

VoidPointer
A: 

You can run a JScript script from a batch-file with the command-line based scripting host CScript.exe.

You would need the script in a separate file though which you pass as an argument to CScript.exe. If you want to keep everything in a single file, you can embed the JScript code in you batch-file, dump it to a temporary file which you pass to CScript.exe and delete the temporary script file afterwards.

There might be more elegant solutions (hopefully)

VoidPointer
+1  A: 

@OP, JScript is not Javascript. If you want to run Javascript "offline", you can try rhino

ghostdog74
A: 

you can install windows script host, an engine to execute scripts on windows. this engine runs JavaScript and provides tools and features that the JavaScript engine in web browsers do not, due to security issues.

More info about windows script host

farzad