如何判断给定路径是文件还是文件夹?

发布于 2024-10-31 15:24:09 字数 42 浏览 4 评论 0原文

使用 Jscript 确定给定路径是文件夹还是文件的最简单方法是什么?

what is the simplest way to determine if a given path is a folder or file using Jscript?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倦话 2024-11-07 15:24:09

你没有说你在什么环境下工作。如果您的意思是 JScript 中的 < a href="http://msdn.microsoft.com/en-us/library/ms950396.aspx" rel="nofollow">Windows 脚本 环境,那么您可以使用 Scripting.FileSystemObject 对象:

var fso = new ActiveXObject('Scripting.FileSystemObject');
if (fso.FileExists(path)) {
    WScript.Echo("It's a file!");
} else if (fso.FolderExists(path)) {
    WScript.Echo("It's a folder!");
} else {
    WScript.Echo("It's superman!");
}

You didn't say in what environment you're working. If you mean JScript in a Windows Scripting environment, then you can use the Scripting.FileSystemObject object:

var fso = new ActiveXObject('Scripting.FileSystemObject');
if (fso.FileExists(path)) {
    WScript.Echo("It's a file!");
} else if (fso.FolderExists(path)) {
    WScript.Echo("It's a folder!");
} else {
    WScript.Echo("It's superman!");
}
许一世地老天荒 2024-11-07 15:24:09

仅靠 JavaScript 我知道你做不到。但是,如果您知道文件的扩展名,则可以对文件进行一些验证。这里看一个例子。

JS

var pathX = "[?:[a-zA-Z0-9-_\.]+(?:.txt|.sql)"; /* File validation using extension*/

function testRegx(frm){
    var path = frm.testfile.value;

    if(path.toString().match(pathX)){
        alert("Valid");
    } else {
        alert("Invalid");
    }
}

HTML

<form name="testupload">
    <p>
    <input type="file" name="testfile">
    <input type="button" onClick="testRegx(this.form);" value="test">
    </p>
</form>

这种技术仍然不是最好的方法,因为您可以篡改文件以更改其扩展名,或者您可以通过基于给定的有效扩展名添加有效扩展名来重命名文件文件。

With JavaScript alone I know you cant. But you can do some validation with a file if you know its extension. Here look an example.

JS

var pathX = "[?:[a-zA-Z0-9-_\.]+(?:.txt|.sql)"; /* File validation using extension*/

function testRegx(frm){
    var path = frm.testfile.value;

    if(path.toString().match(pathX)){
        alert("Valid");
    } else {
        alert("Invalid");
    }
}

HTML

<form name="testupload">
    <p>
    <input type="file" name="testfile">
    <input type="button" onClick="testRegx(this.form);" value="test">
    </p>
</form>

This techniques still not the best way as in you can tampered a file to change its extension or you can rename a file by adding a valid extension base on the given valid file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文