[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

C++ Windows App Directory Traversing

Posted on 2009-04-20
6
Medium Priority
?
1,411 Views
Last Modified: 2013-12-14
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?
0
Comment
Question by:Cyber-Dragon
  • 2
  • 2
  • 2
6 Comments
 
LVL 40

Accepted Solution

by:
evilrix earned 2000 total points
ID: 24187242
0
 

Author Comment

by:Cyber-Dragon
ID: 24188039
I think you misunderstood my question.

I want to essentially display all the sub-folders in the currently folder.
0
 
LVL 86

Expert Comment

by:jkr
ID: 24190282
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
}

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 40

Expert Comment

by:evilrix
ID: 24191576
>>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.
0
 

Author Closing Comment

by:Cyber-Dragon
ID: 31572380
Finally understood how you meant for me to apply the FindFirstFile and FindNextFile.

See below for example :)
0
 
LVL 86

Expert Comment

by:jkr
ID: 24216076
Sorry, but didn't I contribute the missing part?
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question