Cyber-Dragon
asked on
C++ Windows App Directory Traversing
I'm making a program where I would like to change the directory up one folder as well as be able to look at the folders in the current directory and I am unsure about how to do this.
Can anyone point me toward resources that would help me?
Can anyone point me toward resources that would help me?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
evilrix already pointed you in the right direction, but the main part missing in that context might be distinguishing a file entry from a directory entry. See the following example on to traverse a directory tree where you'd implement all the information gathering code in 'HandleFile()', which will be called for each file found. BTW, a straight C++ approach would be Boost's Filesystem Library, check out the docs at http://www.boost.org/libs/filesystem/doc/index.htm ("Boost Filesystem Library")
#include <windows.h>
#include <stdio.h>
void HandleFile ( WIN32_FIND_DATA* pw32fd);
void WalkTree ( char* pszPath, char* pszBase);
void main ( int argc, char** argv)
{
if ( argc != 2) return;
WalkTree ( *( argv + 1), NULL);
}
void WalkTree ( char* pszPath, char* pszBase)
{
WIN32_FIND_DATA w32fd;
HANDLE hFind;
DWORD dwAtt;
char acPath [ MAX_PATH];
char acBase [ MAX_PATH];
printf ( "WalkTree():\tcalled with '%s' '%s'\n", pszPath, pszBase);
if ( '.' == * (pszPath + lstrlen ( pszPath) - 1))
return;
if ( pszBase)
sprintf ( acPath, "%s\\%s", pszBase, pszPath);
else
lstrcpy ( acPath, pszPath);
printf ( "path is %s\n", acPath);
lstrcpy ( acBase, acPath);
dwAtt = GetFileAttributes ( acPath);
if ( 0xffffffff == dwAtt)
{
// error ...
}
if ( FILE_ATTRIBUTE_DIRECTORY & dwAtt)
{
if ( '\\' == acPath [ lstrlen ( acPath) - 1])
lstrcat ( acPath, "*.*");
else
lstrcat ( acPath, "\\*.*");
printf ( "path is now %s\n", acPath);
}
hFind = FindFirstFile ( acPath, &w32fd);
if ( INVALID_HANDLE_VALUE == hFind)
{
// error
printf ( "ERROR %d\n", GetLastError ());
return;
}
// recurse if directory...
if ( FILE_ATTRIBUTE_DIRECTORY == w32fd.dwFileAttributes)
{
WalkTree ( w32fd.cFileName, acBase);
}
else
HandleFile ( &w32fd);
while ( FindNextFile ( hFind, &w32fd))
{
// recurse if directory...
if ( FILE_ATTRIBUTE_DIRECTORY == w32fd.dwFileAttributes)
{
WalkTree ( w32fd.cFileName, acBase);
}
else
HandleFile ( &w32fd);
}
if ( ERROR_NO_MORE_FILES != GetLastError())
{
// error
}
FindClose ( hFind);
}
void HandleFile ( WIN32_FIND_DATA* pw32fd)
{
// handle file here
}
>>I think you misunderstood my question.
>> I want to essentially display all the sub-folders in the currently folder.
Nope, I didn't. :) You need to traverse the files system to do this. The tools you use to do this are those I have mentioned. Since I see jkr has provide a good example of how to use these I'm sure you've now got all you need to do this, but please to let us know if you need more info or help.
>> I want to essentially display all the sub-folders in the currently folder.
Nope, I didn't. :) You need to traverse the files system to do this. The tools you use to do this are those I have mentioned. Since I see jkr has provide a good example of how to use these I'm sure you've now got all you need to do this, but please to let us know if you need more info or help.
ASKER
Finally understood how you meant for me to apply the FindFirstFile and FindNextFile.
See below for example :)
See below for example :)
Sorry, but didn't I contribute the missing part?
ASKER
I want to essentially display all the sub-folders in the currently folder.