简体   繁体   English

是否可以使用Javascript(不在浏览器中运行)检查磁盘上是否存在文件?

[英]Is it possible to check if a file exists on disk using Javascript (not running in a browser)?

I am using an app that uses only JavaScript as its scripting language. 我使用的应用程序使用JavaScript作为其脚本语言。 I have a .ini file and I need to see if it exists. 我有一个.ini文件,我需要查看它是否存在。 Is this possible? 这可能吗?

Also, if it doesn't exist, how can I halt the execution? 另外,如果它不存在,我该如何停止执行?

Try this: 尝试这个:

var myfile = new File(myfile_path);

if (!myfile.exists) {
    alert(myfile + " could not be found!");
}

"Yes", assuming ActiveX can be used. “是”,假设可以使用ActiveX。

See FileSystemObject aka "FSO" (the FileExists method in particular). 请参阅FileSystemObject aka“FSO”(特别是FileExists方法)。 FSO is part of Windows Scripting. FSO是Windows Scripting的一部分。

It is also possible to use the MSXML load method to access a "file://" and catch the appropriate error. 也可以使用MSXML 加载方法访问“file://”并捕获相应的错误。 (I don't know if a vanilla XmlHttpRequest request of "file://" can be used here, and/or in what contexts... it might differ between the XHR from MSXML and the one baked into IE7/8 as well.) (我不知道这里是否可以使用“file://”的vanilla XmlHttpRequest请求,和/或在什么情况下......在MSXML的XHR和IE7 / 8中的XHR之间可能会有所不同。)

Happy coding. 快乐的编码。


If the JavaScript runs from an HTA/HTML Aplication or Windows Sidgebar Gadget, etc, then it's in "Security Level 0" or "Demigod Mode". 如果JavaScript从HTA / HTML应用程序或Windows Sidgebar小工具等运行,则它处于“安全级别0”或“半部模式”。

On the other hand, an HTA runs as a fully trusted application and therefore has more privileges than a normal HTML file... 另一方面, HTA作为完全受信任的应用程序运行 ,因此具有比普通HTML文件更多的权限......

Ok doing something like this works: 好的做这样的事情:

var file = new File(datafile);
var result = file.open ('r');

if result is true , then the file exists, otherwise false means the file does not exist. 如果resulttrue ,则文件存在,否则为false表示文件不存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM