CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1

    Receiving all the subdirs in a dir

    OK. Let's say I have a directory under "C:\xy" and it's full of other subdirectories. I would like to get all the names of the subdirectories from "C:\xy", put them in ABC order, than display them in a listbox. The listbox part is OK, put whassup with the others?

    Could someone help?

    Thanks: Joe


  2. #2
    Join Date
    Jul 1999
    Posts
    15

    Re: Receiving all the subdirs in a dir

    All you need to do is to arrange every thing in your preffered order Either all files separately or in alphabetic order, and then just recurse through the subdirectories.
    i.e.
    Searchdir(Path){
    iterate through dir and put items in an array(you might want to separate dirs at this time)
    iterate through the array and see if a member is file or dir.
    if member is a directory then Searchdir(thisMember);
    }
    you might want to look at the FindFirstFile in MSDN
    Wyne


  3. #3
    Join Date
    Aug 1999
    Location
    Germany
    Posts
    51

    Re: Receiving all the subdirs in a dir

    Joe,

    The code below will let you recursively go through directories. The code also
    give you the ability to get filenames also. You can comment out what you want!

    As far as the sorted goes sort the List!

    ///////////////////////////////////////////////////////////////////////////////
    // FUNCTION: Recurse
    //
    // PURPOSE: To search a given directory (recursively)
    //
    // PARAMETERS:
    // @param1 - CString strDirName - Path of the file/directory search
    //
    // RETURN: Nothing.
    ///////////////////////////////////////////////////////////////////////////////
    void Recurse(CString strDirName)
    {
    WIN32_FIND_DATA wfd;
    HANDLE hFind;
    CString strDirText; // This will contain your Dir Name (and PATH)
    CString strFileText; // This will contain your File Name (and PATH)

    char Buf[254];
    char DirBuf[254];
    char FileBuf[254];
    CString ShareName, Dir, FName, Ext;
    ULONGLONG uhDirSize; // These only need to be DWORDs I did this for CRC Checking
    ULONGLONG uhFileSize;// These only need to be DWORDs I did this for CRC Checking

    // this is just if I decide to Abort in the middle of a search
    // volatile BOOL m_bAbort;
    if (!m_bAbort) {
    uhDirSize = 0;
    // Check if the last char is a back-slash
    // (If not, put it there)
    if (strDirName.Right(1) != "\\")
    strDirName += _T("\\");

    // set the variable and add an astrix for
    // the beginning of the directory search.
    strText = strDirName + _T("*");

    // Iterate through dirs
    hFind = FindFirstFile(strText, &wfd);
    if (hFind != INVALID_HANDLE_VALUE) {
    do {
    if (m_bAbort) return;

    // Check if "." or "..", if not...
    // Check if its a directory.
    if ((strcmp(wfd.cFileName,_T("."))) && (strcmp(wfd.cFileName,_T(".."))) && (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
    // Set to the directory found.
    strDirText = strDirName + wfd.cFileName;

    if (m_bAbort)
    return;

    // If we need to recursively search, then do so.
    if (m_Recurse) {
    // Call this function again.
    Recurse(strDirText);
    }
    }
    } while (FindNextFile(hFind, &wfd));
    // This is a MUST
    FindClose(hFind);
    }

    // Iterate through files
    //
    // set the variable and add an astrix-dot-astrix "*.*"
    // for the beginning of the file search.
    strText = strDirName + _T("*.*");//strMask;
    hFind = FindFirstFile(strText, &wfd);
    if (hFind != INVALID_HANDLE_VALUE) {
    do {
    if (m_bAbort)
    return;

    // If NOT a directory...
    if ((wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) {
    // Set to the file found.
    strFileText = strDirName + wfd.cFileName;
    strFileText.MakeUpper();
    // Get the size of the file.
    uhFileSize = (wfd.nFileSizeHigh << 32) + wfd.nFileSizeLow;
    uhDirSize += uhFileSize;
    }
    } while (FindNextFile(hFind, &wfd));
    // This is a MUST
    FindClose(hFind);
    }
    } // end abort
    }




    Thanks in advance,

    Dan
    Thanks,
    Dan

    Old Programmers never die, they just program away...

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured