有关使用fs.readdir进行异步目录搜索的任何想法吗?我意识到我们可以引入递归并调用读取目录函数与下一个目录来读取,但有点担心它不是异步...
有任何想法吗?我看过node-walk这很棒,但是不给我一个数组中的文件,比如readdir.虽然
寻找像...的输出
['file1.txt', 'file2.txt', 'dir/file3.txt']
Run Code Online (Sandbox Code Playgroud) 这个问题是从这个问题中分拆出来的.一些历史:当我第一次学习Perl时,我总是使用glob而不是opendir+ readdir因为我发现它更容易.后来各种帖子和读物表明这glob很糟糕,所以现在我几乎总是使用readdir.
在思考了这个最近的问题之后,我意识到我选择其中一个或另一个选择的理由可能是无聊的.所以,我将列出一些优点和缺点,我希望更有经验的Perl人可以插入并澄清.概括地说,问题是有没有令人信服的理由,更喜欢glob到readdir或readdir到glob(在某些或所有情况下)?
glob 优点:glob与readdir没有比赛,如果我们的名字单独判断)(从ysth的回答;参见glob下面的参考资料4)可以返回不存在的文件名:
@deck = glob "{A,K,Q,J,10,9,8,7,6,5,4,3,2}{\x{2660},\x{2665},\x{2666},\x{2663}}";
Run Code Online (Sandbox Code Playgroud)glob 缺点:stat每次调用(即stat在大多数情况下无用).(来自brian的回答)可以返回不存在的文件名:
$ perl -le 'print glob "{ab}{cd}"'
Run Code Online (Sandbox Code Playgroud)readdir 优点:opendir返回一个文件句柄,你可以在你的程序中传递(并重用),但glob只返回一个列表readdir是正确的迭代器,并提供功能rewinddir,seekdir,telldirglob特征进行纯粹的猜测.无论如何,我并不是真的担心这种优化水平,但它是理论专家.)glob?0 …我正在使用opendir/readdir获取类似linux的系统上的文件列表.看来目录条目按文件名的字母顺序返回.但是,我在手册页中看不到有关此订单的任何内容.
谁能告诉我readdir是否保证订单?
我遇到了一些关于性能和readdir的文章,这里是php脚本:
function getDirectory( $path = '.', $level = 0 ) {
$ignore = array( 'cgi-bin', '.', '..' );
$dh = @opendir( $path );
while( false !== ( $file = readdir( $dh ) ) ){
if( !in_array( $file, $ignore ) ){
$spaces = str_repeat( ' ', ( $level * 4 ) );
if( is_dir( "$path/$file" ) ){
echo "$spaces $file\n";
getDirectory( "$path/$file", ($level+1) );
} else {
echo "$spaces $file\n";
}
}
}
closedir( $dh );
}
getDirectory( "." );
Run Code Online (Sandbox Code Playgroud)
这正确地回显文件/文件夹. …
我已经开始使用dirent.h库了,我遇到了一个非常有用的"struct dirent"structer成员,我的书中有struct dirent*p-> d_name.但不幸的是,它没有说明这种结构的任何其他成员;
我想知道这个结构的成员还有什么用呢?
问候
C例程opendir(),readdir()和closedir()为我提供了遍历目录结构的方法.但是,readdir()返回的每个dirent结构似乎都没有为我提供一个有用的方法来获取DIR的指针集,我需要将它们递归到目录子目录中.
当然,它们给我文件的名称,所以我可以将该名称附加到目录路径和stat()和opendir()它们,或者我可以通过chdir()和roll更改进程的当前工作目录它通过chdir("..")返回.
第一种方法的问题是,如果目录路径的长度足够大,那么将包含它的字符串传递给opendir()的成本将超过打开目录的成本.如果你有点理论上的话,可以说你的复杂性可能超过线性时间(在目录树中(相对)文件名的总字符数).
而且,第二种方法存在问题.由于每个进程都有一个当前工作目录,因此除了一个线程之外的所有进程都必须在多线程应用程序中进行阻塞.另外,我不知道当前的工作目录是否仅仅是方便(即,在文件系统查询之前将相对路径附加到它).如果是这样,这种方法也会效率低下.
我接受这些功能的替代品.那么如何有效地遍历UNIX目录树(其下的文件总字符数的线性时间)?
我使用以下PHP代码列出当前目录下的所有文件和文件夹:
<?php
$dirname = ".";
$dir = opendir($dirname);
while(false != ($file = readdir($dir)))
{
if(($file != ".") and ($file != "..") and ($file != "index.php"))
{
echo("<a href='$file'>$file</a> <br />");
}
}
?>
Run Code Online (Sandbox Code Playgroud)
问题是列表不是按字母顺序排序的(也许它按创建日期排序?我不确定).
如何确保按字母顺序排序?
这是我写的一个C程序,用于递归导航和输出目录和常规文件.它在我的Linux机器上编译并运行良好.但是在Solaris上,dit->d_type == 8检查和其他类似检查不起作用,因为没有d_type字段.我已经阅读过这个问题的答案是使用S_ISREG()和S_ISDIR()宏,但它们并不能完全按照我目前的代码中的方式工作.我注释掉了在我的Linux机器上运行的行.
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <dirent.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
void helper(DIR *, struct dirent *, struct stat, char *, int, char **);
void dircheck(DIR *, struct dirent *, struct stat, char *, int, char **);
int main(int argc, char *argv[]){
DIR *dip;
struct dirent *dit;
struct stat statbuf;
char currentPath[FILENAME_MAX];
int depth = 0; /*Used to correctly space output*/
/*Open Current Directory*/ …Run Code Online (Sandbox Code Playgroud) 我正在制作一个在Linux shell中运行的程序,并接受一个参数(一个目录),并显示目录中的所有文件及其类型.
输出应该是这样的:
<< ./Program testDirectory
Dir directory1
lnk linkprogram.c
reg file.txt
Run Code Online (Sandbox Code Playgroud)
如果没有参数,则使用当前目录.这是我的代码:
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
int main(int argc, char *argv[])
{
struct stat info;
DIR *dirp;
struct dirent* dent;
//If no args
if (argc == 1)
{
argv[1] = ".";
dirp = opendir(argv[1]); // specify directory here: "." is the "current directory"
do
{
dent = readdir(dirp);
if (dent)
{
printf("%c ", dent->d_type);
printf("%s \n", dent->d_name);
/* if (!stat(dent->d_name, &info))
{
//printf("%u bytes\n", (unsigned int)info.st_size);
}*/ …Run Code Online (Sandbox Code Playgroud) 我想扫描文件夹,但忽略其中包含的所有文件夹/目录.我在(C:/文件夹/)中的所有内容都是.txt文件和其他文件夹,我只想扫描txt文件,并忽略这些文件夹.
app.get('/generatE', function (req, res) {
const logsFolder = 'C:/folder/';
fs.readdir(logsFolder, (err, files) => {
if (err) {
res.send("[empty]");
return;
}
var lines = [];
files.forEach(function(filename) {
var logFileLines = fs.readFileSync (logsFolder + filename, 'ascii').toString().split("\n");
logFileLines.forEach(function(logFileLine) {
if(logFileLine.match(/.*AUDIT*./)) {
lines.push(logFileLine+'\n');
}
})
})
Run Code Online (Sandbox Code Playgroud)