How to loop through a directory
-
Hi Friends, I need to get all the files from a directory in an array. But i cant use CFileDialog.. The path is constant like "C:\\JPEG". From this folder i need to get all the files. Could any one help me find out. Thanks in advance...
Thanks and Regards. SANTHOSH V
-
Hi Friends, I need to get all the files from a directory in an array. But i cant use CFileDialog.. The path is constant like "C:\\JPEG". From this folder i need to get all the files. Could any one help me find out. Thanks in advance...
Thanks and Regards. SANTHOSH V
FindFirstFile/FindNextFile
Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_
-
Hi Friends, I need to get all the files from a directory in an array. But i cant use CFileDialog.. The path is constant like "C:\\JPEG". From this folder i need to get all the files. Could any one help me find out. Thanks in advance...
Thanks and Regards. SANTHOSH V
See the sample from MSDN. Hope you can use MFC
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(); TRACE(\_T("%s\\n"), (LPCTSTR)str); Recurse(str); }
}
finder.Close();
}void PrintDirs()
{
Recurse(_T("C:\\JPG"));
}-Sarath. "Great hopes make everything great possible" - Benjamin Franklin
My blog - Sharing My Thoughts, An Article - Understanding Statepattern
-
Hi Friends, I need to get all the files from a directory in an array. But i cant use CFileDialog.. The path is constant like "C:\\JPEG". From this folder i need to get all the files. Could any one help me find out. Thanks in advance...
Thanks and Regards. SANTHOSH V
Use the
CFileFind
class. 1) UseCFileFind::FindFile()
andCFileFind::FindNextFile()
to iterate through the directories and files. 2) UseCFileFind::IsDirectory()
function to determine whether its file/folder and just add it to your array. 3) You can get the file name by usingCFileFind::GetFileName()
and file path by callingCFileFind::GetFilePath()
. if you need the windows api itself, have a look atFindFirstFile()
andFindNextFile()
. Regards, Jijo._____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.
-
See the sample from MSDN. Hope you can use MFC
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(); TRACE(\_T("%s\\n"), (LPCTSTR)str); Recurse(str); }
}
finder.Close();
}void PrintDirs()
{
Recurse(_T("C:\\JPG"));
}-Sarath. "Great hopes make everything great possible" - Benjamin Franklin
My blog - Sharing My Thoughts, An Article - Understanding Statepattern
alternate methods are to use CRT _findfirst/_findnext[^] or Win32 FindFirstFile/FindNextFile[^] Functions
-Sarath. "Great hopes make everything great possible" - Benjamin Franklin
My blog - Sharing My Thoughts, An Article - Understanding Statepattern
-
Use the
CFileFind
class. 1) UseCFileFind::FindFile()
andCFileFind::FindNextFile()
to iterate through the directories and files. 2) UseCFileFind::IsDirectory()
function to determine whether its file/folder and just add it to your array. 3) You can get the file name by usingCFileFind::GetFileName()
and file path by callingCFileFind::GetFilePath()
. if you need the windows api itself, have a look atFindFirstFile()
andFindNextFile()
. Regards, Jijo._____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.
-
Little late.. Some body didn't like it :(
Regards, Sandip.
modified on Wednesday, August 6, 2008 6:59 AM
-
Little late.. Some body didn't like it :(
Regards, Sandip.
modified on Wednesday, August 6, 2008 6:59 AM
-
Hi Friends, I need to get all the files from a directory in an array. But i cant use CFileDialog.. The path is constant like "C:\\JPEG". From this folder i need to get all the files. Could any one help me find out. Thanks in advance...
Thanks and Regards. SANTHOSH V
santhoshv84 wrote:
I need to get all the files from a directory in an array. But i cant use CFileDialog..
Even if you could, why would you want/need to? :confused:
"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch