+ Reply to Thread
Results 1 to 2 of 2

Thread: How to enumerate files and diredtory in a specified directory?

  1. #1

    How to enumerate files and diredtory in a specified directory?

    Hi All,

    I am creating an MFC application.

    I have a tree of directory and I want to display all the files and directory in specified directory.

    How to achieve this?

  2. #2
    You can use following code to enumerate a directory in recursive manner:

    Code:
    void EnumerateDirectory(char szPathName[])
    {
    	if( (strcmp(szPathName, ".") == 0) || (strcmp(szPathName, "..") == 0) )
    	{
    		return ;
    	}
    	else if(GetFileAttributes(szPathName) == FILE_ATTRIBUTE_DIRECTORY)
    	{
    		WIN32_FIND_DATA objFindData ;
    		char szSearchPath[MAX_PATH] ;
    
    		OutputDebugString(szPathName) ;
    		OutputDebugString("\n") ;
    
    		strcpy(szSearchPath, szPathName) ;
    		strcat(szSearchPath, "\\*.*") ;
    				
    		HANDLE hFind = FindFirstFile(szSearchPath, &objFindData) ;
    		if (hFind != INVALID_HANDLE_VALUE)
    		{
    			do
    			{
    				if( (strcmp(objFindData.cFileName, ".") == 0) || (strcmp(objFindData.cFileName, "..") == 0) )
    				{
    					continue ;
    				}
    				char szFile[MAX_PATH] ;
    				sprintf(szFile, "%s\\%s", szPathName, objFindData.cFileName) ;
    				
    				EnumerateDirectory(szFile) ;
    			}while (FindNextFile(hFind, &objFindData)) ;
    		}
    	}
    	else
    	{
    		OutputDebugString(szPathName) ;
    		OutputDebugString("\n") ;
    	}
    }

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts