I asked about finding in subdirs with criteria. First answer was use FindFirstFileEx(). It seems the function is no good for this purpose or I'm using it wrong.
我询问在标准中找到子目录。第一个答案是使用FindFirstFileEx()。似乎这个功能对这个目的没有好处,或者我用错了。
So can someone explain how I would go about searching in a folder, and all it's subfolders for files that match (to give some sample criteria) .doc;.txt;*.wri; and are newer than 2009-01-01?
那么有人可以解释我将如何在一个文件夹中搜索,以及所有匹配文件的子文件夹(以提供一些样本标准).doc; .txt; * .wri;并且比2009-01-01更新?
Please give a specific code example for those criteria so I know how to use it.
请给出这些标准的具体代码示例,以便我知道如何使用它。
If it isn't possible, is there an alternative for doing this not-at-all-obscure task??? I am becoming quite baffled that so far there aren't well known/obvious tools/ways to do this.
如果不可能,是否有替代方法可以做到这一点并非完全模糊的任务?我变得非常困惑,到目前为止还没有众所周知/明显的工具/方法来做到这一点。
4 个解决方案
#1
From MSDN:
If you refer to the code fragment in that page:
如果您引用该页面中的代码片段:
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
void _tmain(int argc, TCHAR *argv[])
{
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
if( argc != 2 )
{
_tprintf(TEXT("Usage: %s [target_file]\n"), argv[0]);
return;
}
_tprintf (TEXT("Target file is %s\n"), argv[1]);
hFind = FindFirstFileEx(argv[1], FindExInfoStandard, &FindFileData,
FindExSearchNameMatch, NULL, 0);
if (hFind == INVALID_HANDLE_VALUE)
{
printf ("FindFirstFileEx failed (%d)\n", GetLastError());
return;
}
else
{
_tprintf (TEXT("The first file found is %s\n"),
FindFileData.cFileName);
FindClose(hFind);
}
}
You'll see that you can call FindFirstFileEx, where argv1 is a string (LPCSTR) pattern to look for, and &FindFileData is a data structure that contains file info of the found data.. hFind is the handle you use on subsequent calls with FindNextFile.. I think you can also add more search parameters by using the fourth and sixth parameter to FindFirstFileEx.
您将看到可以调用FindFirstFileEx,其中argv1是要查找的字符串(LPCSTR)模式,而&FindFileData是包含找到的数据的文件信息的数据结构.hFind是您在使用FindNextFile的后续调用中使用的句柄..我认为您还可以使用FindFirstFileEx的第四个和第六个参数添加更多搜索参数。
Good luck!
EDIT: BTW, I think you can check a file or dir's attributes by using GetFileAttributes() .. Just pass the filename found in FileFindData.. (filename can refer to a file's name or a directory name I think)
编辑:BTW,我认为你可以通过使用GetFileAttributes()检查文件或目录的属性..只需传递FileFindData中找到的文件名..(文件名可以引用文件的名称或我认为的目录名称)
EDIT: MrVimes, here's what you could do (in pseudocode)
编辑:MrVimes,这是你能做的(伪代码)
find the first file (match with *)
找到第一个文件(与*匹配)
- Check the file find data if it is ".", ".." (these are not really directories or files)
- if check passed, check file find data if it has the attributes you are looking for (i.e. check filename, file attributes, even file creation time can be checked in the file find data, and what not) and do whatever with it
- if check passed, do whatever you need to do with the file
如果检查通过,请执行您需要处理的任何文件
- if check failed, either call findnextfile or end, up to you
如果检查通过,检查文件查找数据是否具有您要查找的属性(即检查文件名,文件属性,甚至文件创建时间可以在文件查找数据中检查,什么不是)如果检查通过则对其执行任何操作,做你需要做的任何文件
如果检查失败,请调用findnextfile或end,由您决定
- if check passed, check file find data if it has the attributes you are looking for (i.e. check filename, file attributes, even file creation time can be checked in the file find data, and what not) and do whatever with it
检查文件查找数据是否为“。”,“..”(这些不是真正的目录或文件)如果检查通过,检查文件查找数据是否具有您要查找的属性(即检查文件名,文件属性,甚至文件创建时间可以在文件查找数据中检查,什么不是)并且如果检查通过就做任何事情,如果检查失败你做任何你需要处理的文件,调用findnextfile或end,由你决定
Something like that..
这样的东西......
#2
I think you use FindFirstFile
to find all files and ignore the ones whose WIN32_FIND_DATA
values don't match your search criteria.
我认为您使用FindFirstFile查找所有文件并忽略其WIN32_FIND_DATA值与您的搜索条件不匹配的文件。
#3
Well you could use it to search for *.doc, *.txt and *.wri by passing those values as the name to search for:
那么你可以用它来搜索* .doc,* .txt和* .wri,方法是将这些值作为名称来搜索:
FindFirstFileEx("*.doc", FindExInfoStandard, &fileData, FindExSearchNameMatch, NULL, 0);
To search by date is a little more complicated, but not overly so:
按日期搜索有点复杂,但并不过分:
SYSTEMTIME createTime;
SYSTEMTIME searchDate;
FILETIME compareTime;
HANDLE searchHandle;
searchDate.wYear = 2009;
searchDate.wMonth= 1;
searchDate.wDay = 1;
SystemTimeToFileTime(searchDate, &compareTime);
searchHandle FindFirstFileEx("*", FindExInfoStandard, &fileData, FindExSearchNameMatch, NULL, 0);
if(searchHandle != INVALID_HANDLE_VALUE)
{
While(searchHandle != ERROR_NO_MORE_FILES)
{
FileTimeToSystemTime(fileData.ftCreationTime, &createTime);
if((ULARGE_INTEGER)compareTime < (ULARGE_INTEGER)createTime)
printf("%s matches date criteria", fileData.cFileName);
FindNextFile(searchHandle, &fileData);
}
}
#4
You need to do two searches. The first is just to find the subdirs, and you do that without any file spec. The second search for the files uses the file spec.
你需要进行两次搜索。第一个是找到子目录,而你没有任何文件规范。第二次搜索文件使用文件规范。
#1
From MSDN:
If you refer to the code fragment in that page:
如果您引用该页面中的代码片段:
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
void _tmain(int argc, TCHAR *argv[])
{
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
if( argc != 2 )
{
_tprintf(TEXT("Usage: %s [target_file]\n"), argv[0]);
return;
}
_tprintf (TEXT("Target file is %s\n"), argv[1]);
hFind = FindFirstFileEx(argv[1], FindExInfoStandard, &FindFileData,
FindExSearchNameMatch, NULL, 0);
if (hFind == INVALID_HANDLE_VALUE)
{
printf ("FindFirstFileEx failed (%d)\n", GetLastError());
return;
}
else
{
_tprintf (TEXT("The first file found is %s\n"),
FindFileData.cFileName);
FindClose(hFind);
}
}
You'll see that you can call FindFirstFileEx, where argv1 is a string (LPCSTR) pattern to look for, and &FindFileData is a data structure that contains file info of the found data.. hFind is the handle you use on subsequent calls with FindNextFile.. I think you can also add more search parameters by using the fourth and sixth parameter to FindFirstFileEx.
您将看到可以调用FindFirstFileEx,其中argv1是要查找的字符串(LPCSTR)模式,而&FindFileData是包含找到的数据的文件信息的数据结构.hFind是您在使用FindNextFile的后续调用中使用的句柄..我认为您还可以使用FindFirstFileEx的第四个和第六个参数添加更多搜索参数。
Good luck!
EDIT: BTW, I think you can check a file or dir's attributes by using GetFileAttributes() .. Just pass the filename found in FileFindData.. (filename can refer to a file's name or a directory name I think)
编辑:BTW,我认为你可以通过使用GetFileAttributes()检查文件或目录的属性..只需传递FileFindData中找到的文件名..(文件名可以引用文件的名称或我认为的目录名称)
EDIT: MrVimes, here's what you could do (in pseudocode)
编辑:MrVimes,这是你能做的(伪代码)
find the first file (match with *)
找到第一个文件(与*匹配)
- Check the file find data if it is ".", ".." (these are not really directories or files)
- if check passed, check file find data if it has the attributes you are looking for (i.e. check filename, file attributes, even file creation time can be checked in the file find data, and what not) and do whatever with it
- if check passed, do whatever you need to do with the file
如果检查通过,请执行您需要处理的任何文件
- if check failed, either call findnextfile or end, up to you
如果检查通过,检查文件查找数据是否具有您要查找的属性(即检查文件名,文件属性,甚至文件创建时间可以在文件查找数据中检查,什么不是)如果检查通过则对其执行任何操作,做你需要做的任何文件
如果检查失败,请调用findnextfile或end,由您决定
- if check passed, check file find data if it has the attributes you are looking for (i.e. check filename, file attributes, even file creation time can be checked in the file find data, and what not) and do whatever with it
检查文件查找数据是否为“。”,“..”(这些不是真正的目录或文件)如果检查通过,检查文件查找数据是否具有您要查找的属性(即检查文件名,文件属性,甚至文件创建时间可以在文件查找数据中检查,什么不是)并且如果检查通过就做任何事情,如果检查失败你做任何你需要处理的文件,调用findnextfile或end,由你决定
Something like that..
这样的东西......
#2
I think you use FindFirstFile
to find all files and ignore the ones whose WIN32_FIND_DATA
values don't match your search criteria.
我认为您使用FindFirstFile查找所有文件并忽略其WIN32_FIND_DATA值与您的搜索条件不匹配的文件。
#3
Well you could use it to search for *.doc, *.txt and *.wri by passing those values as the name to search for:
那么你可以用它来搜索* .doc,* .txt和* .wri,方法是将这些值作为名称来搜索:
FindFirstFileEx("*.doc", FindExInfoStandard, &fileData, FindExSearchNameMatch, NULL, 0);
To search by date is a little more complicated, but not overly so:
按日期搜索有点复杂,但并不过分:
SYSTEMTIME createTime;
SYSTEMTIME searchDate;
FILETIME compareTime;
HANDLE searchHandle;
searchDate.wYear = 2009;
searchDate.wMonth= 1;
searchDate.wDay = 1;
SystemTimeToFileTime(searchDate, &compareTime);
searchHandle FindFirstFileEx("*", FindExInfoStandard, &fileData, FindExSearchNameMatch, NULL, 0);
if(searchHandle != INVALID_HANDLE_VALUE)
{
While(searchHandle != ERROR_NO_MORE_FILES)
{
FileTimeToSystemTime(fileData.ftCreationTime, &createTime);
if((ULARGE_INTEGER)compareTime < (ULARGE_INTEGER)createTime)
printf("%s matches date criteria", fileData.cFileName);
FindNextFile(searchHandle, &fileData);
}
}
#4
You need to do two searches. The first is just to find the subdirs, and you do that without any file spec. The second search for the files uses the file spec.
你需要进行两次搜索。第一个是找到子目录,而你没有任何文件规范。第二次搜索文件使用文件规范。