Board index » cppbuilder » Iterating files and dirs.

Iterating files and dirs.

I am trying to iterate through some dirs while listing all the files in its
path, i have figured out the files part but i cant seem to get the dir part
of the code.

    HANDLE hFind;
    WIN32_FIND_DATA FileData;
    String SearchPath = GetSpecialFolder(CSIDL_INTERNET_CACHE) + "\\*.*";
    String SearchNext = GetSpecialFolder(CSIDL_INTERNET_CACHE) + "\\";

    hFind = FindFirstFile( SearchPath.c_str(), &FileData );

    if( hFind != INVALID_HANDLE_VALUE )
    {
      do
      {
        ListBoxINIFiles->Items->Add( SearchNext + FileData.cFileName );
        Application->ProcessMessages();
        }
        while( FindNextFile( hFind, &FileData ));
        FindClose( hFind );
    }

What do i have to do to apply the same to the subdirs while listing my files
in the listbox??

Thanks

 

Re:Iterating files and dirs.


Hi John,

Quote
> I am trying to iterate through some dirs while listing all the files in its
> path, i have figured out the files part but i cant seem to get the dir part
> of the code.
> What do i have to do to apply the same to the subdirs while listing my files
> in the listbox??

You can use a little recursion to help you out here.  Create a function that
iterates over the contents of the target folder, then test the dwFileAttributes
member of the WIN32_FIND_DATA structure for the FILE_ATTRIBUTE_DIRECTORY flag,
calling your search function recursively.  Here's an example...

// in header...
    void __fastcall FillPath(AnsiString SearchPath);

// in source...
void __fastcall TForm1::FillPath(AnsiString SearchPath)
{
    if (SearchPath[SearchPath.Length()] != '\\')
    {
        SearchPath = SearchPath + "\\";
    }

    char pszPath[MAX_PATH];
    strcpy(pszPath, SearchPath.c_str());
    strcat(pszPath, "*.*");

    WIN32_FIND_DATA FileData;
    HANDLE hFind = FindFirstFile(pszPath, &FileData);
    if (hFind != INVALID_HANDLE_VALUE)
    {
        try
        {
            while (FindNextFile(hFind, &FileData))
            {
                AnsiString NewPath(SearchPath + FileData.cFileName);
                ListBox1->Items->Add(NewPath);

                if (FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY &&
                    strcmp(FileData.cFileName, ".."))
                {
                    FillPath(NewPath);
                }
            }
        }
        catch (...)
        {
            FindClose(hFind);
        }
        FindClose(hFind);
    }

Quote
}

When you want to call the above function, use a technique as such...

    ListBox1->Items->BeginUpdate();
    FillPath(GetSpecialFolder(CSIDL_INTERNET_CACHE));
    ListBox1->Items->EndUpdate();

Hope this helps.

AnsiString __fastcall GetSpecialFolder(int nFolder)
{
    char path[MAX_PATH];
    ZeroMemory(path, MAX_PATH);

    LPMALLOC lpMalloc;
    if (SUCCEEDED(SHGetMalloc(&lpMalloc)))
    {
        LPITEMIDLIST lpidl;
        if (SUCCEEDED(SHGetSpecialFolderLocation(Handle, nFolder, &lpidl)))
        {
            SHGetPathFromIDList(lpidl, path);
            lpMalloc->Free(lpidl);
        }
        lpMalloc->Release();
    }

    return AnsiString(path);
 }

--
Damon Chandler
http://bcbcaq.freeservers.com

Re:Iterating files and dirs.


I'm not sure if I understand the question.  Are you asking how to iterate
thru all files in the sub-tree rooted at a given directory?  If so, you use
findfirst/findnext recursively on each directory.  The following function
will do that (it uses the VCL versions of findfirst/findnext):

IterateFiles( const String& path )
{
    TSearchRec sr;
    if  ( FindFirst( path + "\\*.*", faAnyFile, sr ) == 0 )
    {
        do
        {
            if  ( sr.Attr & faDirectory && sr.Name != "." && sr.Name !=
".." )
            {
                result = IterateFiles( path + "\\" + sr.Name );
            }
            else if  (  ! (sr.Attr & faDirectory)  )
                //  Process this file
        }
        while  ( FindNext( sr ) == 0 );
        FindClose( sr );
    }

Quote
}

Re:Iterating files and dirs.


Thanks as usual Damon.

Other Threads