使用fs.readdir和fs.statSync返回ENOENT,没有此类文件或目录错误 [英] Using fs.readdir and fs.statSync returns ENOENT, no such file or directory error

查看:86
本文介绍了使用fs.readdir和fs.statSync返回ENOENT,没有此类文件或目录错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这有效:

    var promise = new Future(),
        dirs = [],
        stat;
        

    Fs.readdir(Root + p, function(error, files){
        _.each(files, function(file) {
            //stat = Fs.statSync(file);
            //if ( stat.isDirectory() ) {
                dirs.push(file);
            //}
        });

        promise.return(dirs);
    });

这不是:

    var promise = new Future(),
        dirs = [],
        stat;
        

    Fs.readdir(Root + p, function(error, files){
        _.each(files, function(file) {
            stat = Fs.statSync(file);
            if ( stat.isDirectory() ) {
                dirs.push(file);
            }
        });

        promise.return(dirs);
    });

导致错误:ENOENT,没有这样的文件或目录'fonts'",而是"fonts".是树中的第一个目录,并且确实存在.

Resulting in "Error: ENOENT, no such file or directory 'fonts'", but "fonts" is the first directory in the tree, and it does exist.

我一定很想念一些愚蠢的东西.我正在尝试仅返回文件夹/目录名称.

I've got to be missing something silly. I'm trying to return folder/directory names only.

当我在这里的时候,有人知道如何返回所有级别的目录吗?

While I'm at it, does anyone know how to return all levels of directories?

例如,结果可能是:

[
    "fonts",
    "fonts/font-awesome",
    "images",
    "images/somepath",
    "images/somepath/anotherpath"
]

在弄清楚我做错了什么之后,这是我的下一个目标.

That is my next goal, after figuring out what I'm doing wrong.

推荐答案

readdir将为您提供文件夹中条目的名称,而不是整个路径.这将起作用:

readdir will give you the names of the entries in the folder, not the whole path. This will work:

stat = Fs.statSync(Root + p + "/" + file);

整个代码:

var promise = new Future(),
    dirs = [],
    stat,
    fullPath;


Fs.readdir(Root + p, function(error, files){
    _.each(files, function(file) {
        fullPath = Root + p + "/" + file;
        stat = Fs.statSync(fullPath);
        if ( stat.isDirectory() ) {
            dirs.push(fullPath);
        }
    });

    promise.return(dirs);
});

这篇关于使用fs.readdir和fs.statSync返回ENOENT,没有此类文件或目录错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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