2

How can I read input from the command line in JScript, similar to Pascal's readln?

Charles
  • 48,924
  • 13
  • 96
  • 136
Michael Uskov
  • 29
  • 1
  • 2
  • 2
    JavaScript doesn't have any built in functions for performing any kind of I/O, they come from the host environment. It would help if you specified which environment you were running in (e.g. Windows Scripting Host). – Quentin Nov 18 '11 at 11:27
  • This question is answered here: https://stackoverflow.com/questions/21015121/get-command-line-arguments-with-jscript-net – Aaron Cicali Jul 29 '20 at 01:16

3 Answers3

4

It sounds like you're asking about Windows Script Host. If you're using cscript.exe to run your scripts, you can work with WScript.StdIn:

WScript.Echo("Enter something");
WScript.Echo("You entered " +WScript.StdIn.ReadLine());

http://msdn.microsoft.com/en-us/library/skwz6sz4(v=VS.85).aspx

Andy E
  • 311,406
  • 78
  • 462
  • 440
  • I'm not attempting to be funny here, but it only sounds like WSH because you added the tag for it... Kind of putting words in the questioner's mouth there a bit? – Matt Fellows Nov 18 '11 at 11:31
  • 1
    It sounded like WSH before then (since it mentions "command line" and "JScript" rather than "JavaScript"). – Quentin Nov 18 '11 at 11:33
  • @MattFellows: I made the same assumption as Alex, in the absence of detail provided by the OP. I think it's a safe assumption, though, for two reasons. The OP explicitly referred to JScript. Normally, when talking about a browser *"JavaScript"* is the word used — even in older versions of IE which include the JScript engine. Secondly, the command line was also explicitly mentioned. The default way to run JScript from the command line in Windows is through *cscript.exe* and *wscript.exe*, which are Windows Script Host. Because of this, it made sense to add the tag (after I'd already answered). – Andy E Nov 18 '11 at 11:35
  • It was tagged javascript though, not JScript, and based on the OPs original language it was ambiguous as to his actual meaning of "command line". It's tenuous at best to retag that with WSH, especially as, even if you assume he meant command line, there are other command line javascript tools out there. I'm just saying that i'd be cautious about making that assumption. Ultimately we need more information from the OP to answer this completely... – Matt Fellows Nov 18 '11 at 11:39
  • @MattFellows: that's an unfortunate side effect of having [tag:jscript] as a synonym of [tag:javascript]. The OP likely specified the former and it was automatically changed to the latter. In fact, I was just considering raising this issue on meta. – Andy E Nov 18 '11 at 11:43
  • @MattFellows - JScript and the rest of the question is a dead give away that this a WSH question. – Kev Nov 18 '11 at 12:29
3

Assuming cscript the.js a1 a2 ... you can;

var args = WScript.Arguments;
for (var i= 0; i < args.length; i++) {
    WScript.Echo(args(i))
} 
Alex K.
  • 159,548
  • 29
  • 245
  • 267
  • That was my first thought too, but that isn't like Pascal's `readln`, which is what he asked in the question. – Andy E Nov 18 '11 at 11:30
  • Coincidentally though, it's what I was looking for, so that's a useful unintended side-effect ;-). – rossmcm Nov 08 '19 at 04:44
0

It has been like forever that I have looked into Pascal, so I'm not quite sure what ReadLn() exactly does. If you just want to get a line from the user in the command line you may use the WScript.StdIn.ReadLine() method as described here.

But if you want to read from a file, then you may try:

var myFileSysObj = new ActiveXObject("Scripting.FileSystemObject");
var myInputTextStream = myFileSysObj.OpenTextFile("c:\\temp\\test.txt", 1, true);
var myString = myInputTextStream.ReadLine();
myInputTextStream.Close();

from here.

Foad
  • 7,892
  • 8
  • 38
  • 126