- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我的代码是这样的:
DIR* pDir = opendir("/path/to/my/dir");
struct dirent pFile = NULL;
while ((pFile = readdir())) {
// Check if it is a .zip file
if (subrstr(pFile->d_name,".zip") {
// It is a .zip file, delete it, and the matching log file
char zipname[200];
snprintf(zipname, sizeof(zipname), "/path/to/my/dir/%s", pFile->d_name);
unlink(zipname);
char* logname = subsstr(zipname, 0, strlen(pFile->d_name)-4); // Strip of .zip
logname = appendstring(&logname, ".log"); // Append .log
unlink(logname);
}
closedir(pDir);
(此代码未经测试,纯属示例)
关键是:是否允许在使用 readdir() 遍历目录时删除目录中的文件?还是 readdir() 仍会找到已删除的 .log 文件?
最佳答案
引自 POSIX readdir :
If a file is removed from or added to the directory after the most recent call to opendir() or rewinddir(), whether a subsequent call to readdir() returns an entry for that file is unspecified.
所以,我的猜测是......这取决于。
这取决于操作系统、一天中的时间、添加/删除文件的相对顺序、...
此外,在 readdir()
函数返回和您尝试 unlink()
文件之间,某些其他进程可能已经删除了该文件文件和你的 unlink()
失败。
编辑
我用这个程序测试过:
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
int main(void) {
struct dirent *de;
DIR *dd;
/* create files `one.zip` and `one.log` before entering the readdir() loop */
printf("creating `one.log` and `one.zip`\n");
system("touch one.log"); /* assume it worked */
system("touch one.zip"); /* assume it worked */
dd = opendir("."); /* assume it worked */
while ((de = readdir(dd)) != NULL) {
printf("found %s\n", de->d_name);
if (strstr(de->d_name, ".zip")) {
char logname[1200];
size_t i;
if (*de->d_name == 'o') {
/* create `two.zip` and `two.log` when the program finds `one.zip` */
printf("creating `two.zip` and `two.log`\n");
system("touch two.zip"); /* assume it worked */
system("touch two.log"); /* assume it worked */
}
printf("unlinking %s\n", de->d_name);
if (unlink(de->d_name)) perror("unlink");
strcpy(logname, de->d_name);
i = strlen(logname);
logname[i-3] = 'l';
logname[i-2] = 'o';
logname[i-1] = 'g';
printf("unlinking %s\n", logname);
if (unlink(logname)) perror("unlink");
}
}
closedir(dd); /* assume it worked */
return 0;
}
在我的计算机上,readdir()
找到已删除的文件,但找不到在 opendir()
和 readdir()
之间创建的文件。但是在另一台计算机上可能会有所不同;如果我用不同的选项编译,它在我的电脑上可能会有所不同;如果我升级内核可能会有所不同; ...
关于c - 使用 readdir() 读取目录时删除文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1676522/
DIR *dir_ptr; struct dirent *dir_entery; dir_ptr = opendir("/tmp"); while (dir_ptr&&(dir_entery = re
我惊讶地发现手册页中包含 readdir 的两个冲突变体的条目。 在 READDIR(2) 中,它特别指出你不想使用它: This is not the function you are intere
我在 Julia 1.7 的 readdir 上使用 isdir 过滤器时遇到了一些问题。运行以下命令: println(readdir("data/")) println(isdir("data/S
我有一个当前正在使用 while 循环处理的目录。文件名实际上是这样的日期: updates.20130831.1445.hr updates.20140831.1445.hr updates.201
int indent = 0; int listDir(const char* dirname){ DIR* dir; struct dirent* d; if(!(dir =
我使用以下代码循环遍历目录以打印出文件的名称。但是,并未显示所有文件。我尝试过使用 clearstatcache 但没有效果。 $str = ''; $ignore = array('
我使用以下代码循环遍历目录以打印出文件的名称。但是,并未显示所有文件。我尝试过使用 clearstatcache 但没有效果。 $str = ''; $ignore = array('
到目前为止,我知道在 Perl 中打开和读取目录的两种方法。您可以使用 opendir、readdir 和 closedir 或者您可以简单地使用 glob 来获取目录的内容。 示例: 使用 op
我正在编写一个点文件存储库管理器,但删除存储库的命令不起作用。它转到存储库文件夹,然后它必须列出所有文件和目录,以便我能够删除它们。问题是,它列出了我需要删除的每个文件或目录,但它排除了非空的 .gi
使用server and client代码。我尝试添加通过 readdir() 实现的 ls 功能 我的 *connection_handler 看起来像这样: void *connection_ha
我有一个使用 readdir() 的函数。我使用 stat() 来获取有关我作为参数提供的目录中的文件的信息。 folder_name[] is actually the absolute path
这个问题已经有答案了: Crash or "segmentation fault" when data is copied/scanned/read to an uninitialized point
嗨,我正在尝试编写一个使用 readdir 的程序,但我读取的条目未按排序顺序。我还应该寻找什么。 最佳答案 尝试scandir,它可以一次读取整个目录并为您对名称进行排序。 关于c - readdi
在 main 中,我使用 deletefolder() 调用以下函数: void deletefolder(){ struct dirent *next_file; DIR *folder
我有一段相当简单的代码来获取 C++ 目录中的文件列表。令人费解的是,目录中的 135 个文件中只有 68 个最终存储在 vector 文件名中。发生了什么事? DIR* pDIR = opendir
我正在尝试读取和处理目录中的所有文件。 如果只使用 readdir() 并计算目录中的文件数,一切都很好。但是,如果我复制文件名 strcpy() 并传递给不同的函数,readdir() 会在读取 4
所以我循环调用 readdir() 函数并将生成的文件名添加到链表中的新节点。通过设置 file_list = add_file_node() 解决问题后,我遇到了 dir_list 循环在访问目录时
我写了一个简单的图片库脚本女巫检查文件夹并显示文件夹中的所有图像我有代码删除 .和 .. 但它不起作用,我不明白为什么任何帮助都会很棒,因为我是 PHP 中 dir 函数的新手。我在链接周围有灯箱标签
这个问题在这里已经有了答案: List regular files only (without directory) problem (2 个答案) 关闭 10 年前。 我的目标是计算目录中的文件数
这是我希望打印的 Perl 脚本 found执行时: #!/usr/bin/perl use warnings; use strict; use utf8; use Encode; use const
我是一名优秀的程序员,十分优秀!