递归文件搜索 [英] recursive file search
问题描述
我想弄清楚如何工作这个东西出于某些原因,它结束在某一点..我不是很好的递归,我确定问题在某处有。
I'm trying to figure out how to work this thing out .. For some reason, it ends at a certain point.. I'm not very good at recursion and I'm sure the problem lies somewhere there..
此外,即使我检查了cFileName!=..,它仍然显示在结尾,不知道为什么,但。不再显示..
Also, even if I checked for cFileName != "..", it still shows up at the end, not sure why but the "." doesn't show up anymore..
void find_files( wstring wrkdir )
{
wstring temp;
temp = wrkdir + L"\\" + L"*";
fHandle = FindFirstFile( temp.c_str(), &file_data );
if( fHandle == INVALID_HANDLE_VALUE )
{
return;
}
else
{
while( FindNextFile( fHandle, &file_data ) )
{
if( file_data.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY &&
wcscmp(file_data.cFileName, L".") != 0 &&
wcscmp(file_data.cFileName, L"..") != 0 )
{
find_files( wrkdir + L"\\" + file_data.cFileName );
}
else if( file_data.dwFileAttributes != FILE_ATTRIBUTE_HIDDEN &&
file_data.dwFileAttributes != FILE_ATTRIBUTE_SYSTEM )
{
results << wrkdir << "\\" << file_data.cFileName << endl;
}
}
}
}
更改这些,程序不枚举剩余的文件..
After changing those, the program doesn't enumerate the remaining files left..
例如,如果有一个名为test的子文件夹,它枚举测试中的所有内容,
For example, if there is a sub folder named test, it enumerates everything inside test but doesn't finish enumerating the files inside the original directory specified.
推荐答案
从 FindFirstFile 文档:
如果函数失败或失败,
从
中的搜索字符串中找到文件lpFileName参数,则返回
的值为INVALID_HANDLE_VALUE,$ p
的内容为$ p
不确定。
If the function fails or fails to locate files from the search string in the lpFileName parameter, the return value is INVALID_HANDLE_VALUE and the contents of lpFindFileData are indeterminate.
您应该只退出一次迭代而不是整个程序:
You should only exit from the one iteration not the whole program:
if( fHandle == INVALID_HANDLE_VALUE )
{
return;
}
这可能会解决您的其他问题:
And this may solve your other problem:
else if( file_data.dwFileAttributes != FILE_ATTRIBUTE_HIDDEN &&
file_data.dwFileAttributes != FILE_ATTRIBUTE_SYSTEM &&
wcscmp(file_data.cFileName, L".") != 0 &&
wcscmp(file_data.cFileName, L"..") != 0
)
{
results << wrkdir << "\\" << file_data.cFileName << endl;
}
另见@ fretje的答案。它给你的代码有另一个问题。
Also see @fretje's answer as well. It gives another problem that your code has.
已更新:您还需要将fHandle用作局部变量,而不是全局变量。
Updated new: You need to use fHandle as a local variable as well, not global variable.
更改为:
HANDLE fHandle = FindFirstFile( temp.c_str(), &file_data );
这篇关于递归文件搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!