C++ 如何在计算机中搜索文件和文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3365182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to search the computer for files and folders
提问by blood
i need a way to search the computer for files like Windows Explorer. i want my program to search lets say hard drive c:. i need it to search C:\ for folders and files (just the ones you could see in c:\ then if the user clicks on a file on the list like the folder test (C:\test) it would search test and let the user see what files/folders are in it.
我需要一种方法来搜索计算机中的文件,例如 Windows 资源管理器。我想让我的程序搜索让我们说硬盘驱动器 c:。我需要它在 C:\ 中搜索文件夹和文件(只是您可以在 c:\ 中看到的那些,然后如果用户单击列表中的文件,例如文件夹 test (C:\test),它将搜索 test 并让用户查看其中包含哪些文件/文件夹。
回答by monoceres
Since you mentioned windows, the most straight forward winapi way to do it is with FindFirstFileand FindNextFilefunctions.
既然你提到了 windows,那么最直接的 winapi 方法就是使用FindFirstFile和FindNextFile函数。
edit: Here's an example that shows you how to enumerate all files/folders in a directory.
编辑:这是一个示例,向您展示如何枚举目录中的所有文件/文件夹。
#include <Windows.h>
#include <iostream>
int main()
{
WIN32_FIND_DATA file;
HANDLE search_handle=FindFirstFile(L"C:\*",&file);
if (search_handle)
{
do
{
std::wcout << file.cFileName << std::endl;
}while(FindNextFile(search_handle,&file));
FindClose(search_handle);
}
}
回答by Jacob
This will be OS dependent. The SO question
这将取决于操作系统。SO问题
How can I get a list of files in a directory using C or C++?
handles this problem well. You can download DIRENThere.
很好地处理了这个问题。你可以在这里下载DIRENT。
Now that you have this, I'd recommend recursively searching for a file with a DFS/BFSalgorithm. You can assume the whole directory structure is a treewhere each file is a leaf node and each subdirectory is an internal node.
既然您有了这个,我建议您使用DFS/ BFS算法递归搜索文件。您可以假设整个目录结构是一棵树,其中每个文件都是一个叶节点,每个子目录都是一个内部节点。
So all you have to do is,
所以你所要做的就是,
- Get the list of files/folders in a directory with a function such as:
void getFilesFolders(vector<string> & dir_list, const string & folder_name)
- If it's a directory, go to 1 with the directory name
- If it's a file, terminate if it's the file you're looking for, else move on to the next file.
- 使用以下函数获取目录中的文件/文件夹列表:
void getFilesFolders(vector<string> & dir_list, const string & folder_name)
- 如果是目录,请使用目录名称转到1
- 如果它是一个文件,则在它是您要查找的文件时终止,否则继续下一个文件。
回答by Amardeep AC9MF
You can use Directory
class members to do this with C# or managed C++. See the following MSDN article:
您可以使用Directory
类成员通过 C# 或托管 C++ 执行此操作。请参阅以下 MSDN 文章:
http://support.microsoft.com/kb/307009
http://support.microsoft.com/kb/307009
If you wish to use C++ with MFC you can use CFileFind
如果您希望将 C++ 与 MFC 一起使用,您可以使用 CFileFind
http://msdn.microsoft.com/en-us/library/f33e1618%28v=VS.80%29.aspx
http://msdn.microsoft.com/en-us/library/f33e1618%28v=VS.80%29.aspx
You'll have to supply your own browse window to present the file system tree.
您必须提供自己的浏览窗口来显示文件系统树。
Or you can use one of the directory/file controls to do both for you.
或者,您可以使用目录/文件控件之一为您执行这两项操作。
回答by Ben Usman
boost::filesystem can be a cross-platform solution for that (check out for such functions in it).
boost::filesystem 可以是一个跨平台的解决方案(查看其中的此类功能)。
回答by Elixir Techne
#include <Windows.h>
#include <iostream>
int FindF(char* pDirectory)
{
char szFindPath[MAX_PATH] = {0};
strcpy(szFindPath, pDirectory);
strcat(szFindPath, "\*");
WIN32_FIND_DATA file;
HANDLE search_handle=FindFirstFile(szFindPath,&file);
if (search_handle)
{
do
{
if(file.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
{
strcpy(szFindPath, pDirectory);
strcat(szFindPath, "\");
strcat(szFindPath, file.cFileName);
FindF(szFindPath);
}
std::wcout << file.cFileName << std::endl;
}while(FindNextFile(search_handle,&file));
CloseHandle(search_handle);
}
}
回答by Brackets
There really is no need to use 3rd party library to accomplish this. This is a short, independent function which lists all files (with their paths) in a directory, including subdiretories' files. std::string folderName
has to finish with \
, and if you want to list all files on computer, just create a loop in calling function along with GetLogicalDriveStrings(It returns strings with \
, so it couldn't be more convenient in this case).
确实没有必要使用 3rd 方库来实现这一点。这是一个简短的独立函数,它列出目录中的所有文件(及其路径),包括子目录的文件。std::string folderName
必须以 结束\
,如果要列出计算机上的所有文件,只需在调用函数和GetLogicalDriveStrings 中创建一个循环(它返回带有 的字符串\
,因此在这种情况下再方便不过了)。
void FindAllFiles(std::string folderName)
{
WIN32_FIND_DATA FileData;
std::string folderNameWithSt = folderName + "*";
HANDLE FirstFile = FindFirstFile(folderNameWithSt.c_str(), &FileData);
if (FirstFile != INVALID_HANDLE_VALUE) {
do {
if (strcmp(FileData.cFileName, ".") != 0 && strcmp(FileData.cFileName, "..") != 0)
{
if(FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
std::string NewPath = folderName + FileData.cFileName;
NewPath = NewPath + "\";
FindAllFiles(NewPath);
}
else
{
std::cout /*<< folderName*/ << FileData.cFileName << std::endl;
}
}
} while(FindNextFile(FirstFile, &FileData));
}
}
This is ASCII version, remember that files and folders can be named in Unicode
这是 ASCII 版本,记住文件和文件夹可以用 Unicode 命名