开发者

How to use JavaScript to determine whether a directory exists?

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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜