如何将文件写入每个读取的目录?

发布于 2025-01-11 00:29:16 字数 1194 浏览 0 评论 0原文

我有以下文件夹结构,并希望在每个目录中创建一个文件来指示读取了哪些文件。创建文件后,我想使用该文件作为条件来确定是否应该跳过或从该目录中读取。

问题

  1. 如何将文件写入已读取所有文件的目录?
  2. 如何在条件逻辑中使用该文件来读取或跳过该文件目录?

当前文件夹结构(运行nodejs之前)

  • 子文件夹1 -- 文件 1 -- 文件 2 -- 文件 3 子
  • 文件夹 2 -- 文件 3 -- 文件 4
  • 子文件夹 3 -- 文件 5 -- 文件 6

所需输出(运行 nodejs 文件后)

  • 子文件夹 1 -- 文件 1 -- 文件 2 -- 文件 3 -- files-read.txt
  • 子文件夹 2 -- 文件 3 -- 文件 4 -- files-read.txt
  • 子文件夹 3 -- 文件 5 -- 文件 6 -- files-read.txt

我尝试过但卡住了

const fs = require('fs');
var targetDirectory = '../my/path/to/target';
var filesRead = [];

fs.readdir(targetDirectory, function (err, files) {

  if(err) return console.log('error: ' + err);

  files.forEach(function(file) {
   // HELP - how do i not read if files-read.txt already exists in directory?
   filesRead.push(file);
  });

});

var fileName = 'files-read.txt';

fs.writeFile(fileName, filesRead, function (err) {
  if(err) return console.log('error: ' + err);
  /// help -- how do i only write if files-read.text doesn't exist?
  console.log('finished writing');
});

I have the following folder structure below and wanted to create a file in each directory indicating which files were read. After the file is created, I want to use that file as a conditional to determine if I should skip or read from that directory.

Question(s)

  1. How can I write a file to the directory with all the files read?
  2. How can I use that file in conditional logic to read or skip that file directory?

Current Folder Structure (before running nodejs)

  • sub folder 1
    -- file 1
    -- file 2
    -- file 3
  • sub folder 2
    -- file 3
    -- file 4
  • sub folder 3
    -- file 5
    -- file 6

Desired Output (after running nodejs file)

  • sub folder 1
    -- file 1
    -- file 2
    -- file 3
    -- files-read.txt
  • sub folder 2
    -- file 3
    -- file 4
    -- files-read.txt
  • sub folder 3
    -- file 5
    -- file 6
    -- files-read.txt

What I have tried but got stuck

const fs = require('fs');
var targetDirectory = '../my/path/to/target';
var filesRead = [];

fs.readdir(targetDirectory, function (err, files) {

  if(err) return console.log('error: ' + err);

  files.forEach(function(file) {
   // HELP - how do i not read if files-read.txt already exists in directory?
   filesRead.push(file);
  });

});

var fileName = 'files-read.txt';

fs.writeFile(fileName, filesRead, function (err) {
  if(err) return console.log('error: ' + err);
  /// help -- how do i only write if files-read.text doesn't exist?
  console.log('finished writing');
});

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

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

发布评论

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

评论(1

野味少女 2025-01-18 00:29:16

您需要循环浏览子文件夹。
在你的情况下,它看起来像这样:

const fs = require('fs');
var targetDirectory = __dirname + '/target';
var filesRead = [];
var fileName = 'files-read.txt';

const { readdirSync } = fs;

const getDirectories = (source) =>
  readdirSync(source, { withFileTypes: true })
    .filter((dirent) => dirent.isDirectory())
    .map((dirent) => dirent.name);

const directories = getDirectories(targetDirectory);

directories
  .map((dir) =>
    fs.readdir(targetDirectory + '/' + dir, function (err, files) {
      if (err) return console.log('error: ' + err);
      if(files.includes(fileName)) {
        // Check to see if the file already exists
        return null
      }
      fs.writeFile(fileName, filesRead, function (err) {
        if (err) return console.log('error: ' + err);
        console.log(files); // or do what ever you want here
        console.log('finished writing');
      });
    })
  );

You need to cycle through the sub folders.
In your case it would look like this:

const fs = require('fs');
var targetDirectory = __dirname + '/target';
var filesRead = [];
var fileName = 'files-read.txt';

const { readdirSync } = fs;

const getDirectories = (source) =>
  readdirSync(source, { withFileTypes: true })
    .filter((dirent) => dirent.isDirectory())
    .map((dirent) => dirent.name);

const directories = getDirectories(targetDirectory);

directories
  .map((dir) =>
    fs.readdir(targetDirectory + '/' + dir, function (err, files) {
      if (err) return console.log('error: ' + err);
      if(files.includes(fileName)) {
        // Check to see if the file already exists
        return null
      }
      fs.writeFile(fileName, filesRead, function (err) {
        if (err) return console.log('error: ' + err);
        console.log(files); // or do what ever you want here
        console.log('finished writing');
      });
    })
  );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文