Multiple File Selection Without Any Extra Code

Environment: DOC View

This is in response to the column "Multiple File Select in File Open Dialog" under the section "Document—View/Misc."

Instead of creating a new class (derived from CDocManager), I felt that I could accomplish the same functionality in a simpler way. I have already implemented a code in which I get the file name from the user, using CFileDialog on "OnFileOpen". This is the message handler for ID_FILE_OPEN. This could be done just by adding the style "OFN_ALLOWMULTISELECT" in the CFileDialog Constructor.

Here's the code snippet:

void MYApp::OnFileOpen( )
{
  CString csRootPath( "E:\\" );
  //Display "File Open"/"File SaveAs" Dialog depending
  //upon     //"p_bOpen"
  CFileDialog cfileDlg( p_bOpen ,NULL,NULL,OFN_ALLOWMULTISELECT,
                        "All files (*.*)|*.*|");

  CString csFileDlgTitle;
  csFileDlgTitle.LoadString( "File Open" );

  cfileDlg.m_ofn.lpstrTitle = csFileDlgTitle;
  cfileDlg.m_ofn.lpstrInitialDir = csRootPath;

  int nRet = cfileDlg.DoModal( );
  ASSERT( ( nRet != IDOK ) || ( nRet != IDCANCEL ) );
}

Fine. How do you get the selected file name(s)?

Simple. Use GetStartPosition() and GetNextPathName() of CFileDialog to iterate the selected file names.

Here's the code snippet:

POSITION pos ( cfileDlg.GetStartPosition() );

while( pos )
{
  CString csFileName(  cfileDlg.GetNextPathName( pos ) );
  //implement ur fn to open the file accordingly
}

With Regards,
Sriram



Comments

  • This is insufficient

    Posted by Legacy on 11/05/2002 12:00am

    Originally posted by: Shrishail Rana

    The variable to hold multiple files is limited. So at max it can hold 30 or 40 files depending on the filenames
    
    

    TCHAR FileNameList[8192]= {0};
    CFileDialog fileDlg(T...................);
    // display the file dialog
    //now replace the original buffer with the more length.......
    fileDlg.m_ofn.lpstrFile = FileNameList;
    fileDlg.m_ofn.nMaxFile=8192;

    if(fileDlg.DoModal()==IDOK)
    {
    POSITION p = fileDlg.GetStartPosition();
    int itemcnt = m_cListCtrl.GetItemCount();
    while(p)
    {
    FilePath = fileDlg.GetNextPathName(p);
    ...............}

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • QA teams don't have time to test everything yet they can't afford to ship buggy code. Learn how Coverity can help organizations shrink their testing cycles and reduce regression risk by focusing their manual and automated testing based on the impact of change.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds