how can we use cscript.exe or wscript.exe to run javascripts?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chakriroxx
    New Member
    • Jan 2008
    • 6

    how can we use cscript.exe or wscript.exe to run javascripts?

    hi all,

    i want to run some javascript file from command line.
    On searching the net i came to know that we can do it by using cscript.exe or with wscript.exe.

    1. can some one provide me some example code(javascript )?
    2. can we pass some arguments to the functions in the script file? or can we make it interactive in the commandline to read some values?
    3. where can i get a complete reference to these problems ..

    It ll be good if the example code can demonstrate all the important capabilities.

    looking forward for a soon reply . .
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    See this thread (from the newsgroup archives). It's more JScript than JavaScript.

    Comment

    • rnd me
      Recognized Expert Contributor
      • Jun 2007
      • 427

      #3
      if you have the .net framework installed, not only can you use the command line, you can turn your scripts into actual EXEs!

      you have to find jsc.exe on your computer, and copy it to a work folder, or add the path of jsc.exe to your path environ variable.

      just use the windows search to find it.

      then have fun, and try some code:

      save this as eval.bat
      Code:
      @echo off
      echo print(%1%2%3%4%5%6%7%8%9) > try.tmp.js
      jsc /nologo try.tmp.js
      try.tmp.exe
      then, you can do stuff like:

      C:\Temp\jscript >eval 400 + 20
      420


      you can pass thing by the command line to your exes as well:
      save this text as args.js, and run jsc.exe args.js

      Code:
      import System;
      import System.IO;
      import System.Net;
      import System.Text;
      import System.Windows.Forms;
      var url="";
      var addy="";
      var dump="";
      var CMDarguments : String = Environment.GetCommandLineArgs() || ",";
      print (CMDarguments )
      C:\Temp\jscript \simples>args.e xe this seems to work
      args.exe,this,s eems,to,work




      this one will let you "pipe" stdout to your js exe:
      in this case, it pastes the text from the command line to the clipboard, another IO point you can use.

      Code:
      import System;
      import System.Windows.Forms;
      
      var Console = System.Console;
      var cb= System.Windows.Forms.Clipboard;
      var scc = String.fromCharCode;
      
      
      //var chars : String = "";
      var chars=[];
      var len : int = 0;
      var i : int;
      var c : String;
      
      
      while (true)
          {
               i = Console.Read ();
               if (i == -1) break;
               c = scc(i);
               chars[len++]=c;
          }
      
      
      cb.SetDataObject( chars.join("") , true );
      Console.WriteLine(chars.join(""));
      if i compile above as paste.exe for example:

      Code:
      echo %date% | paste.exe
      will place today's date on the clipboard.


      if you are looking for something specific, ask me, but this should get you started.
      also, you can find full .NET documentation online.
      although it is slightly more complicated than browser javascript, it has much the same syntax and many of my functions have been pasted from browser code without any editing.

      good luck!

      Comment

      • chakriroxx
        New Member
        • Jan 2008
        • 6

        #4
        hi,

        actually i am using javascripts and html to generate some text files.
        the entire thing is purely client side and it resides in the users hard disk.

        there are some 10 files that are to be generated and i have 10 html pages with the generate file button in each page to generate the 10 files.

        now i want to generate it from command line so that the user need not open the ten pages and click the generate file button on each page.

        if i can do this with the help of batch fies then the job becomes simple. (in future it may be requried to genrate more files).

        steps i have planned to do this are :

        1. write a .js which contains the script that genrates the files ..
        2. place the location of this .js file in a textfile.
        3. write a batch file that reads the .js file location from this text file and then execute it with the help of cscript .exe or something ..
        4. i don want any user interaction in the command line. if the .js file location is changed on the hard disk then it will be updated in the text file.

        please help . . .i am a newbie ..

        Comment

        Working...