gpt4 book ai didi

javascript - 使用 Node.js 在目录中搜索文件

转载 作者:行者123 更新时间:2023-11-30 11:50:37 25 4
gpt4 key购买 nike

我目前正在尝试使用 nodegrunt 在 Windows 上的特定文件夹中搜索一些文件。

我有一个 grunt task,它具有读取包含 JSON 文件的目录的功能,但问题是当我运行该任务时,读取file 没有做任何事情,grunt task 上的其他所有内容都运行完美,但是。我不确定路径的引用是否正确,但我也在使用 path.normalize() 并且它不会引发任何错误。

这是代码片段:

..// Some other code
var fs = require('fs'),
path = require("path");

grunt.registerTask('separate', function() {
var filePath = path.normalize("C:\Users\jbernhardt\Desktop\testkeeper\jenkinsReports");

fs.readdir(filePath, function(err, filenames) {

//This log doesn't show as it the function is not running
grunt.log.writeln("Testing");

if (err) {
grunt.log.writeln("Error");
return;
}
filenames.forEach(function(filename){
grunt.log.writeln("Testing");

});

});
...//Some more code below for the same task
}

有谁知道为什么在我运行任务时会跳过这段代码?我可能会遗漏一些基本的东西。谢谢!

最佳答案

尝试 readdirSync 并检查您的功能是否仍然无法正常工作。我猜你的过程在回调之前就完成了。

关于javascript - 使用 Node.js 在目录中搜索文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39597289/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com