如何使用JavaScript来确定目录是否存在? [英] How to use JavaScript to determine whether a directory exists?

查看:109
本文介绍了如何使用JavaScript来确定目录是否存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了以下代码来在我的本地文件系统上编写文件:

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();
}

我现在想要的是检查目录(我指定的目录)在上面的代码片段中)是否存在甚至已经存在?我想抛出异常或只是向用户显示请指定您要将文件存储到的目录以及类似的内容。
所以我的问题是:

1.是否可以检查指定的目录是否存在?

2.是否可以动态创建目录并自动将文件存储在那里?

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?

请不要打扰访问本地文件系统是坏事还是别的。我正在为自己的个人用途创建这个,我很清楚这个事实。


请尝试使用原生javascript回答,我不使用JQuery或任何其他框架。



非常感谢

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

推荐答案

这应该有效:

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

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

这篇关于如何使用JavaScript来确定目录是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆