Javascript 如何使用Nodejs将文件夹中的数据作为文件名写入CSV文件

Javascript 如何使用Nodejs将文件夹中的数据作为文件名写入CSV文件,javascript,node.js,csv,Javascript,Node.js,Csv,Anty body确实使用javascript将文件夹中的文件名写入CSV文件 我的文件夹结构是 Data +IMG +test -1.png -2.png +train -3.png -4.png 输出CSV文件如下所示 Data/IMG/test/1.png Data/IMG/train/3.png Data/IMG/test/2.png Data/IMG/train/4.png 您只需循环浏览所有

Anty body确实使用javascript将文件夹中的文件名写入CSV文件

我的文件夹结构是

Data
 +IMG
    +test
       -1.png
       -2.png
    +train
       -3.png
       -4.png
输出CSV文件如下所示

Data/IMG/test/1.png     Data/IMG/train/3.png
Data/IMG/test/2.png     Data/IMG/train/4.png

您只需循环浏览所有文件夹并查找所有文件。 您可以参考以执行此操作

查找所有文件的路径时,可以将这些路径写入csv文件的字符串中:

const fs = require('fs');
const path = require('path');

let csvStr = "";

async function loop(startPath) {
    // Our starting point
    try {
        // Get the files as an array
        const files = await fs.promises.readdir(startPath);

        // Loop them all with the new for...of
        for (const file of files) {
            // Get the full paths
            const currentPath = path.join(startPath, file);

            // Stat the file to see if we have a file or dir
            const stat = await fs.promises.stat(currentPath);

            if (stat.isFile()) {
                console.log("'%s' is a file.", currentPath);
                // put the file into csv string
                csvStr += currentPath + ", "
            } else if (stat.isDirectory()) {
                console.log("'%s' is a directory.", currentPath);
                // enter the dictionary and loop
                await loop(currentPath);
            }

        } // End for...of
    } catch (e) {
        // Catch anything bad that happens
        console.error("We've thrown! Whoops!", e);
    }

}

// Make an async function that gets executed immediately
(async () => {
    // start loop from the path where you run node
    await loop("./");

    fs.writeFileSync("your.csv", csvStr);
})();

您只需循环浏览所有文件夹并查找所有文件。 您可以参考以执行此操作

查找所有文件的路径时,可以将这些路径写入csv文件的字符串中:

const fs = require('fs');
const path = require('path');

let csvStr = "";

async function loop(startPath) {
    // Our starting point
    try {
        // Get the files as an array
        const files = await fs.promises.readdir(startPath);

        // Loop them all with the new for...of
        for (const file of files) {
            // Get the full paths
            const currentPath = path.join(startPath, file);

            // Stat the file to see if we have a file or dir
            const stat = await fs.promises.stat(currentPath);

            if (stat.isFile()) {
                console.log("'%s' is a file.", currentPath);
                // put the file into csv string
                csvStr += currentPath + ", "
            } else if (stat.isDirectory()) {
                console.log("'%s' is a directory.", currentPath);
                // enter the dictionary and loop
                await loop(currentPath);
            }

        } // End for...of
    } catch (e) {
        // Catch anything bad that happens
        console.error("We've thrown! Whoops!", e);
    }

}

// Make an async function that gets executed immediately
(async () => {
    // start loop from the path where you run node
    await loop("./");

    fs.writeFileSync("your.csv", csvStr);
})();

谢谢你的回复,我是js新手。你能解释一下吗?或者让代码
fromPath
是内部循环是文件夹中的当前位置,应该是文件路径或字典路径。你想找到所有文件,所以我们只需要文件的路径。我们可以为CSV收集文件路径的名称,因此我添加了
csvStr+=fromPath+,“
<代码>,用于CSV分隔符。您能提供完整的代码吗?我尝试过,但没有成功CSV(逗号分隔值)文件中总是有逗号,所以我无法理解给定的示例输出(没有逗号)。或者,我猜您只是希望
测试
成为一个列,而
训练
成为另一个列?我认为这并不难,因为您已经知道如何获取所有文件名。你需要做的是添加一些
,如果还有
,你想自己试试吗?谢谢你的回答,我是js新手,你能解释得更详细一点,或者让代码
来自路径
是内部循环是文件夹中的当前位置,应该是文件路径或字典路径。你想找到所有文件,所以我们只需要文件的路径。我们可以为CSV收集文件路径的名称,因此我添加了
csvStr+=fromPath+,“
<代码>,用于CSV分隔符。您能提供完整的代码吗?我尝试过,但没有成功CSV(逗号分隔值)文件中总是有逗号,所以我无法理解给定的示例输出(没有逗号)。或者,我猜您只是希望
测试
成为一个列,而
训练
成为另一个列?我认为这并不难,因为您已经知道如何获取所有文件名。您需要做的是添加一些
,如果还有
,您想自己试试吗?