Enumerating all paths on a drive?
-
Hello everyone! I'm trying to enumerate all the paths on my C:\ drive. I want this: C: C:\folder C:\folder\subfolder C:\folder\subfolder\subsubfolder C:\folder\subfolder\subsubfolder\etc ...but instead I get this: C: C:\folder C:\folder\. C:\folder\.\. C:\folder\.\.\. What's the correct way of doing this? I'm using FindFirstFile and FindNextFile, not using MFC. Thanks people!
Windows Calculator told me I will die at 28. :(
-
Hello everyone! I'm trying to enumerate all the paths on my C:\ drive. I want this: C: C:\folder C:\folder\subfolder C:\folder\subfolder\subsubfolder C:\folder\subfolder\subsubfolder\etc ...but instead I get this: C: C:\folder C:\folder\. C:\folder\.\. C:\folder\.\.\. What's the correct way of doing this? I'm using FindFirstFile and FindNextFile, not using MFC. Thanks people!
Windows Calculator told me I will die at 28. :(
See this illustration from MSDN, the part you are missing in colored in red...
#include
#includeusing namespace std;
void Recurse(LPCTSTR pstr)
{
CFileFind finder;// build a string with wildcards
CString strWildcard(pstr);
strWildcard += _T("\\*.*");// start working for files
BOOL bWorking = finder.FindFile(strWildcard);while (bWorking)
{
bWorking = finder.FindNextFile();**`// skip . and .. files; otherwise, we'd // recur infinitely!`** `if (finder.IsDots()) continue;` // if it's a directory, recursively search it if (finder.IsDirectory()) { CString str = finder.GetFilePath(); cout << (LPCTSTR) str << endl; Recurse(str); }
}
finder.Close();
}void main()
{
if (!AfxWinInit(GetModuleHandle(NULL), NULL, GetCommandLine(), 0)
cout << "panic!" << endl;
else
Recurse(_T("C:"));
}A dot(.) indicates current directory and a dot dot(..) indicates parent directory. So you need to ignore these two entries.
Nibu thomas A Developer Code must be written to be read, not by the compiler, but by another human being. http:\\nibuthomas.wordpress.com
-
See this illustration from MSDN, the part you are missing in colored in red...
#include
#includeusing namespace std;
void Recurse(LPCTSTR pstr)
{
CFileFind finder;// build a string with wildcards
CString strWildcard(pstr);
strWildcard += _T("\\*.*");// start working for files
BOOL bWorking = finder.FindFile(strWildcard);while (bWorking)
{
bWorking = finder.FindNextFile();**`// skip . and .. files; otherwise, we'd // recur infinitely!`** `if (finder.IsDots()) continue;` // if it's a directory, recursively search it if (finder.IsDirectory()) { CString str = finder.GetFilePath(); cout << (LPCTSTR) str << endl; Recurse(str); }
}
finder.Close();
}void main()
{
if (!AfxWinInit(GetModuleHandle(NULL), NULL, GetCommandLine(), 0)
cout << "panic!" << endl;
else
Recurse(_T("C:"));
}A dot(.) indicates current directory and a dot dot(..) indicates parent directory. So you need to ignore these two entries.
Nibu thomas A Developer Code must be written to be read, not by the compiler, but by another human being. http:\\nibuthomas.wordpress.com
Is there any way to do this without MFC and C++ (just plain C & Win32)? Thanks!
Windows Calculator told me I will die at 28. :(
-
Is there any way to do this without MFC and C++ (just plain C & Win32)? Thanks!
Windows Calculator told me I will die at 28. :(
Lord Kixdemp wrote:
Is there any way to do this without MFC and C++ (just plain C & Win32)?
Yeah, the source code of CFileFind::IsDots is as follows, now maybe you know how to do it...
BOOL CFileFind::IsDots() const
{
ASSERT(m_hContext != NULL);
ASSERT_VALID(this);// return TRUE if the file name is "." or ".." and // the file is a directory BOOL bResult = FALSE; if (m\_pFoundInfo != NULL && IsDirectory()) { LPWIN32\_FIND\_DATA pFindData = (LPWIN32\_FIND\_DATA) m\_pFoundInfo; if (pFindData->cFileName\[0\] == '.') { if (pFindData->cFileName\[1\] == '\\0' || (pFindData->cFileName\[1\] == '.' && pFindData->cFileName\[2\] == '\\0')) { bResult = TRUE; } } } return bResult;
}
Nibu thomas A Developer Code must be written to be read, not by the compiler, but by another human being. http:\\nibuthomas.wordpress.com
-
Hello everyone! I'm trying to enumerate all the paths on my C:\ drive. I want this: C: C:\folder C:\folder\subfolder C:\folder\subfolder\subsubfolder C:\folder\subfolder\subsubfolder\etc ...but instead I get this: C: C:\folder C:\folder\. C:\folder\.\. C:\folder\.\.\. What's the correct way of doing this? I'm using FindFirstFile and FindNextFile, not using MFC. Thanks people!
Windows Calculator told me I will die at 28. :(
When you're enumerating, skip any dirs named "." and ".."
--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ