最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to use JavaScript to determine whether a directory exists? - Stack Overflow

programmeradmin0浏览0评论

I have written the following code to write a file on my local file system:

writeToFile : function(msg) {
    var fso  = new ActiveXObject("Scripting.FileSystemObject");
    fh = fso.CreateTextFile("c:\\QHHH\\myXML.xml", true);
    fh.WriteLine(msg);
    fh.Close();
}

What I want now is to check if the directory(the one I have specified in code snippet above) even exists or not already? I want to throw an exception or simply show an alert to the user that "Please specify a directory you want to store your file into" and anything like this.
So my questions are:
1.Is it possible to check if the specified directory exists or not ?
2.Is it possible to create the directory on the fly and store the file in there automatically?

Please don't bother that accessing local file system is bad or anything else. I am creating this for my own personal use and I am well aware of this fact.
Please try to answer in native javascript, I am not using JQuery or any other framework.

Many Thanks

I have written the following code to write a file on my local file system:

writeToFile : function(msg) {
    var fso  = new ActiveXObject("Scripting.FileSystemObject");
    fh = fso.CreateTextFile("c:\\QHHH\\myXML.xml", true);
    fh.WriteLine(msg);
    fh.Close();
}

What I want now is to check if the directory(the one I have specified in code snippet above) even exists or not already? I want to throw an exception or simply show an alert to the user that "Please specify a directory you want to store your file into" and anything like this.
So my questions are:
1.Is it possible to check if the specified directory exists or not ?
2.Is it possible to create the directory on the fly and store the file in there automatically?

Please don't bother that accessing local file system is bad or anything else. I am creating this for my own personal use and I am well aware of this fact.
Please try to answer in native javascript, I am not using JQuery or any other framework.

Many Thanks

Share Improve this question asked Aug 4, 2011 at 11:15 EMMEMM 1,8129 gold badges36 silver badges58 bronze badges 2
  • activeX might do the trick, but only works in IE, consider to do applet? – ajreal Commented Aug 4, 2011 at 11:18
  • I am using IE8.Any code specific to IE will be perfect for me. – EMM Commented Aug 4, 2011 at 11:24
Add a comment  | 

2 Answers 2

Reset to default 10

This should work:

var sFolderPath = "c:\\QHHH";
if (!fso.FolderExists(sFolderPath)) {
    alert("Folder does not exist!");
    return;
}

fh = fso.CreateTextFile(sFolderPath + "\\myXML.xml", true);
//....

To create a directory all you need is :

var fso = new ActiveXObject("Scripting.FileSystemObject");
fso.CreateFolder("fully qualified name of the forlder u want 2 create");
发布评论

评论列表(0)

  1. 暂无评论