Click to See Complete Forum and Search --> : Getting subfolder names


ashokvishnu
July 24th, 2006, 05:21 AM
Hi Gurus,

I want to retrieve the sub directory names from a directory. I dont want the file names if any are present. Presented below is code I am working on. However FindFileData.cFileName is giving only the first letter of the directory. How do I correct this? Since I have to use the code for productions, any corrections or better sample code will be most welcome.

void GetSubFolderNames( LPCTSTR szFolderPath )
{
WIN32_FIND_DATA FindFileData;
HANDLE hFind = INVALID_HANDLE_VALUE;
DWORD dwError;

hFind = FindFirstFile(szFolderPath, &FindFileData);

if (hFind == INVALID_HANDLE_VALUE)
{
printf ("Invalid file handle. Error is %u\n", GetLastError());
return (-1);
}
else
{
do
{
if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if ( !lstrcmp( FindFileData.cFileName, L"." )
|| !lstrcmp( FindFileData.cFileName, L".."))
{
//Do nothing for "." and ".." folders
}
else
{
CString fileName = FindFileData.cFileName;
printf ("First file name is %s\n", fileName);
}

}
} while (FindNextFile(hFind, &FindFileData) != 0) ;

dwError = GetLastError();
FindClose(hFind);
if (dwError != ERROR_NO_MORE_FILES)
{
printf ("FindNextFile error. Error is %u\n", dwError);
return (-1);
}
}
return (0);
}

Thanks

Viorel
July 24th, 2006, 05:49 AM
I think one of your if statement should be changed to:


if ( ! lstrcmp( FindFileData.cFileName, _T("."))
|| ! lstrcmp( FindFileData.cFileName, _T(".."))
{
//Do nothing for "." and ".." folders
}
else
{
CString fileName = FindFileData.cFileName;
_tprintf("File name is %s\n", fileName);
}

Now all of the filename's characters should appear, and it should work in both ANSI and Unicode modes.

I hope it helps.

wildfrog
July 24th, 2006, 05:57 AM
Small update to Viorels post:

_tprintf(_T("File name is %s\n"), fileName);

- petter

ashokvishnu
July 24th, 2006, 06:17 AM
Thanks for your replies. Is the code I posted good enough to be used for production code? I dont want the client to come back saying that this is very bad coding or low performance coding style.

ovidiucucu
July 24th, 2006, 07:28 AM
Thanks for your replies. Is the code I posted good enough to be used for production code? I dont want the client to come back saying that this is very bad coding or low performance coding style.
Depends on the client. ;)

OK, I'm the client. I come back and say: "No, I don't like it.
You have used MFC (I saw a CString) but not used CFileFind which can make that code more readable, maintainable, etc".

PS. I have ignored the compiler errors like 'void' function returning a value which you can fix after the first build.

ashokvishnu
July 24th, 2006, 07:39 AM
You are right. But my problem is that I am new to VC. I would have liked to use CFile class, but I didnt find any identical functions to FindFirstFile and FindNextFile. So I am confused how to do it using CFile. Any suggestions?

ovidiucucu
July 24th, 2006, 09:45 AM
I didn't say CFile but CFileFind (http://msdn2.microsoft.com/en-us/library/f33e1618.aspx).
Here is an example:
void CFoo::GetSubFolderNames(LPCTSTR szFolderPath, CStringArray& arrSubFolders)
{
arrSubFolders.RemoveAll();
CString strToFind;
strToFind.Format(_T("%s\\*.*"), szFolderPath);

CFileFind ff;
BOOL bFound = ff.FindFile(strToFind);
while(bFound)
{
bFound = ff.FindNextFile();
if(ff.IsDirectory() && !ff.IsDots())
{
CString strFolder = ff.GetFileName();
arrSubFolders.Add(strFolder);
}
}
}
// example of using GetSubFolderNames
CStringArray arrSubFolders;
GetSubFolderNames(_T("c:\\temp"), arrSubFolders);


See also THIS FAQ (http://www.codeguru.com/forum/showthread.php?t=312461).

ashokvishnu
July 24th, 2006, 11:48 PM
Thanks pal.