Javascript 如何读取文件夹中的文件夹,从每个文件夹中获取index.html,并将其作为列表(li)标记中的链接传递

Javascript 如何读取文件夹中的文件夹,从每个文件夹中获取index.html,并将其作为列表(li)标记中的链接传递,javascript,node.js,Javascript,Node.js,由于我的nodejs自学仍在进行中,我当然在为一些任务而挣扎,我再次哭着寻求帮助。 我打算创建一个任务,读取特定文件夹中的文件夹,其中每个文件夹都有一个index.html文件。 因此,在查找index.html文件时,它会将指向该文件的路径作为链接传递给标记。 示例:mainFolder-subfolder-index.html <ul><li><a href="to the folder where the index.html is located">.

由于我的nodejs自学仍在进行中,我当然在为一些任务而挣扎,我再次哭着寻求帮助。 我打算创建一个任务,读取特定文件夹中的文件夹,其中每个文件夹都有一个index.html文件。 因此,在查找index.html文件时,它会将指向该文件的路径作为链接传递给
  • 标记。 示例:mainFolder-subfolder-index.html

    <ul><li><a href="to the folder where the index.html is located">./mainFolder/subfolder/index.html</a></li></ul>
    
    任何帮助都将不胜感激

    亲切问候,


    费尔南多

    您可以使用以下代码-

    • 读取主文件夹中的所有文件和文件夹
    • 遍历文件和文件夹
    • 跳过所有文件
    • 最后在所有子文件夹中构造index.html文件的文件路径
    此代码段打印index.html文件的绝对文件路径

    如果需要,还可以将其设置为相对文件路径

    const path = require("path");
    const fs = require("fs");
    
    const index_files = [];
    const parent_directory_path = "";
    
    const directoryPath = path.join(__dirname, parent_directory_path);
    
    fs.readdir(directoryPath, function (error, files) {
    
        if (!error) {
            files.forEach(function (file) {
                const currDirectoryPath = path.join(__dirname, file);
                if (fs.existsSync(currDirectoryPath) && fs.lstatSync(currDirectoryPath).isDirectory()) {
                    index_files.push(currDirectoryPath + "/index.html");
                }
            });
    
            console.log(index_files);
    
        }
    
    });
    
    更新

    由于
    fs.readdir
    API提供了主文件夹中的所有文件和文件夹,我们需要一个跳过文件的条件

    fs.existsSync(curredirectorypath)和&fs.lstatSync(curredirectorypath).isDirectory()
    检查当前文件/文件夹是否存在且是否为文件夹


    此外,此代码段假定父目录是当前目录。如果要提供不同的父目录路径,请在
    parent\u directory\u path
    变量中设置父目录路径值。

    您可以使用以下代码-

    • 读取主文件夹中的所有文件和文件夹
    • 遍历文件和文件夹
    • 跳过所有文件
    • 最后在所有子文件夹中构造index.html文件的文件路径
    此代码段打印index.html文件的绝对文件路径

    如果需要,还可以将其设置为相对文件路径

    const path = require("path");
    const fs = require("fs");
    
    const index_files = [];
    const parent_directory_path = "";
    
    const directoryPath = path.join(__dirname, parent_directory_path);
    
    fs.readdir(directoryPath, function (error, files) {
    
        if (!error) {
            files.forEach(function (file) {
                const currDirectoryPath = path.join(__dirname, file);
                if (fs.existsSync(currDirectoryPath) && fs.lstatSync(currDirectoryPath).isDirectory()) {
                    index_files.push(currDirectoryPath + "/index.html");
                }
            });
    
            console.log(index_files);
    
        }
    
    });
    
    更新

    由于
    fs.readdir
    API提供了主文件夹中的所有文件和文件夹,我们需要一个跳过文件的条件

    fs.existsSync(curredirectorypath)和&fs.lstatSync(curredirectorypath).isDirectory()
    检查当前文件/文件夹是否存在且是否为文件夹


    此外,此代码段假定父目录是当前目录。如果要提供不同的父目录路径,请在
    parent\u directory\u path
    变量中设置父目录路径值。

    Hi planet\u hunter,感谢您的输入和精彩的代码。运行脚本后,结果是给我一个空数组,我在主文件夹中有几个文件夹。我们有什么办法可以找回它吗?提前谢谢。我的代码假设脚本在主文件夹中。脚本和主文件夹的目录是否相同?如果是这样,只需将
    parent\u directory\u path
    变量的值设置为主文件夹的名称。如果仍然不起作用,请告诉我。嗨,planet_hunter,谢谢你的输入和很棒的代码。运行脚本后,结果是给我一个空数组,我在主文件夹中有几个文件夹。我们有什么办法可以找回它吗?提前谢谢。我的代码假设脚本在主文件夹中。脚本和主文件夹的目录是否相同?如果是这样,只需将
    parent\u directory\u path
    变量的值设置为主文件夹的名称。如果仍然不起作用,请告诉我。