Is there a way, in JavaScript, to call an external routine (shell script, executable, etc.) and use the results? Say I want to iterate over the files in a directory, something like this:
[some JS command/function] (“ls > file.list”)
var file = new Array([get the file.list contents]);
for (var x=0, x <= [need length of file Array here]; x++)
{
do useful stuff with file
}
It depends. If the page is loaded from the local file system, you can get out of the ‘sandbox’ and access the file system. For example, you can use the Microsoft FileSystem Scripting Object as an ActiveX control and do it.
But if the page is being loaded from a web server on another machine, nope. You aren’t allowed to interact with the file system, the registry, or anything else on your local machine from Javascript.
As Sam notes, Microsoft’s JScript has objects that can access the file system, external resources, etc. (and it’s almost trivial for any VC++/VB programmer to create an ActiveX object that can be used from any WSH [Windows Scripting Host] scripting language) from server-side [Active Server Pages] scripts. However IIRC, with recent updates to IE, even locally loaded pages won’t let you use those objects on the client side.
And, of course, to ensure browser independance, you want not to be using features that are not 100% ECMAscript compliant anyway.
Hmmm…not the result I was hoping for. My company makes a product that supports the use of JavaScript but not in Web pages. It’s just the local scripting language. I’ve now been told there’s a “symlib.load” function that’ll get info from that file. I’ll have to research that. Still don’t know how to call an OS command.
It’s possible write ActiveX controls and then script them in web pages, and IE provides some objects for file access (FileSystemSomething…? Haven’t used it in a while). However, both these approaches have the considerable disadvantage of only working in Internet Explorer, and being pretty big hassles even in IE (unless the user has all their security options completely turned off, they will get confirmation/warning dialogs out the wazoo, even if the page is loaded locally, or the objects will just be blocked entirely).
If you’re using JavaScript as a scripting language in your own applications, though (which is what it sounds like you’re doing), you would have to provide your own file access code, as no such functionality exists in JavaScript itself. That is, you could write methods in your host application that provide file access, and provide means to call them from scripts through your JavaScript engine.
The file system object is “Scripting.FileSystemObject” and can be instantiated from any Active Server Pages script (using Sytem.CreateObject) or local script (using CreateObject).
Since I actually bothered to look it up this time, I realized there is a way to execute an aribtrary command from a Windows Scripting Host script: the Run method of the WScript.Shell object will allow you to execute a command as if from the Desktop Shell.