How to use FileSystemObject to read file in JavaScript

Bryant picture Bryant · Oct 17, 2012 · Viewed 52.2k times · Source

I want to read a file with FileSystemObject. My code is as following:

<!DOCTYPE html>
<html lang="en">
<head>
<title>Read json</title>
</head>

<body>
<script type="text/javascript">

function readFile(filename){
    var fso = new ActiveXObject("Scripting.FileSystemObject");
    var ForReading = 1;
    var f1 = fso.OpenTextFile(filename, ForReading);
    var text = f1.ReadAll();
    f1.close();
    return text;
}

myJSONText = "text.txt";
var myObject = readFile(myJSONText);//eval('(' + myJSONText + ')');

document.write(myObject.name);
</script>
</body>
</html>

Answer

Aaron Thomas picture Aaron Thomas · Aug 8, 2014

First, let me repeat some comments above. I've never seen using ActiveXObject client side extolled as a thing that should be done.

Now, let me say I'm trying to learn how to do this myself. Here are some thoughts (and helpful links, see the bottom) on this question.

The general layout, according to "Much ADO about Text Files" on MSDN's scripting clinic column, is:

  1. Create the object.
  2. Create another object, using the first, that uses a method of the first object (such as getting a file).
  3. Do things to the file.
  4. Close the file.

How do you start? According to IE Dev Center (linked here), use an ActiveXObject in Javascript as follows:

newObj = new ActiveXObject(servername.typename[, location])

You've got that when you declare fso in your code. What about this "servername" thing, isn't the file accessed locally? Instead of "servername etc" you've put in Scripting.FileSystemObject. This is actually fine, if the HKEY_CLASSES_ROOT registry key on the host PC supports it (see ref above).

Once the ActiveXObject is successfully declared, and if the browser allows it (IE only), and if the end user agrees to any warnings that pop up ("An ActiveX control on this page might be unsafe to interact with other parts of the page..." etc), then the object allows you to use any of the methods associated with that object. That's where the power of the Windows Scripting FileSystemObject comes into play.

Any FileSystemObject (fso) method is now available to use, which as its name suggests, means file (and directory) interaction on the local machine. Not just reading, as your question is focused on, but writing and deleting as well. A complete list of methods and properties is available at MSDN here. After being used, close out the file using the .close() method.

So, this is dangerous for obvious reasons. But what wasn't obvious to me at first was that these interactions with the filesystem may happen invisibly. There is a good chance that whatever you do, from reading a file to deleting a directory tree, no warnings or command prompts will come up to let you know what's happening because of your few lines of code.

Let me finish by commenting on the last bits of code above. Using JSON in conjunction with data pulled from the FileSystemObject provides a great way to allow JavaScript interaction (JSON .parse and .stringify come immediately to mind). With this, data could be stored locally, perhaps as an alternative to HTML5 local storage (ref this SO thread, which goes more in-depth with this concept, and another SO question I raised about this here).

Here are some links for further reading:
IE Dev Center, JavaScript Objects, ActiveXObject
MSDN JScript Windows Scripting (including FileSystemObject methods, etc)
MSDN Scripting Clinic (older articles, many broken links, but stil a lot of good info on this stuff)