如何按修改日期列出目录中的文件
How can I list files in a directory by modification date?
目前我正在使用readdir
,它工作得很好。现在,我的文件夹已经很混乱了,按字母顺序搜索列表(虽然不能保证,但它是目录顺序)可能会令人沮丧。那么,我如何修改下面的代码以按修改日期排序,而不是按当前顺序排序呢?
static cell AMX_NATIVE_CALL n_dir_list( AMX* amx, cell* params)
{
DIR *dir = (DIR*)params[1];
struct dirent *ent;
if ((ent = readdir (dir)) != NULL)
{
cell *buf, *addr;
amx_GetAddr(amx, params[3], &addr);
switch (ent->d_type)
{
case DT_REG:
*addr = 2;
break;
case DT_DIR:
*addr = 1;
break;
default:
*addr = 0;
}
amx_GetAddr(amx, params[2], &buf);
amx_SetString(buf, ent->d_name, 0, 0, params[4]);
return true;
}
return false;
}
readdir函数来自不同的头文件,如下所示:
static struct dirent *readdir(DIR *dirp)
{
DWORD attr;
if (dirp == NULL) {
/* directory stream did not open */
DIRENT_SET_ERRNO (EBADF);
return NULL;
}
/* get next directory entry */
if (dirp->cached != 0) {
/* a valid directory entry already in memory */
dirp->cached = 0;
} else {
/* get the next directory entry from stream */
if (dirp->search_handle == INVALID_HANDLE_VALUE) {
return NULL;
}
if (FindNextFileA (dirp->search_handle, &dirp->find_data) == FALSE) {
/* the very last entry has been processed or an error occured */
FindClose (dirp->search_handle);
dirp->search_handle = INVALID_HANDLE_VALUE;
return NULL;
}
}
/* copy as a multibyte character string */
DIRENT_STRNCPY ( dirp->curentry.d_name,
dirp->find_data.cFileName,
sizeof(dirp->curentry.d_name) );
dirp->curentry.d_name[MAX_PATH] = ' ';
/* compute the length of name */
dirp->curentry.d_namlen = strlen (dirp->curentry.d_name);
/* determine file type */
attr = dirp->find_data.dwFileAttributes;
if ((attr & FILE_ATTRIBUTE_DEVICE) != 0) {
dirp->curentry.d_type = DT_CHR;
} else if ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0) {
dirp->curentry.d_type = DT_DIR;
} else {
dirp->curentry.d_type = DT_REG;
}
return &dirp->curentry;
}
FindNextFile
的Microsoft文档说,"如果数据必须排序,应用程序必须在获得所有结果后进行排序。"因此,如果你想按照修改日期对目录进行排序,你必须自己全部读取并排序。
相关文章:
- CMake-按正确顺序将项目与C运行时对象文件链接
- 如何按文件扩展名引用文件夹中的文件
- 按单词搜索文件,然后在C++中打印特定行数
- 按下 QPushbutton 时将文本保存到文件
- 如何在 CMake 中"override"文件的按扩展名类型
- 按价格对文件中的产品进行排序
- 确实可以提高directory_iterator在Windows上按字母顺序访问文件和文件夹
- 如何按升序对输入文件中的数字进行排序,并找到它们的范围和中值
- 如何从输入的文本文件中按姓氏字母顺序对图表进行排序?
- 如何保存在文本文件中按下的键
- 使用相同的文件对象按顺序写入和读取文件
- 如何从文本文件中按升序对数据进行排序
- C++在文本文件中按升序对数字进行排序
- C++:使用get()读取文件;没有按预期工作
- 如何从文件中按行读取C++中的字符串.txt
- 检查文本文件是否按字母顺序排列
- 生成文件未按正确的顺序注册依赖项
- 在TXT文件中按顺序写入二叉树
- 如何使用Boost Filesystem Library v3确定文件是否按路径包含
- 从带有数字占位符的文件中按字母顺序排序