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

  • "Security" is the number one issue holding business leaders back from the cloud. But does the reality match the perception? Keeping data close to home, on premises, makes business and IT leaders feel inherently more secure. But the truth is, cloud solutions can offer companies real, tangible security advantages. Before you assume that on-site is the only way to keep data safe, it's worth taking a comprehensive approach to evaluating risks. Doing so can lead to big benefits.

  • Hybrid cloud platforms need to think in terms of sweet spots when it comes to application platform interface (API) integration. Cloud Velocity has taken a unique approach to tight integration with the API sweet spot; enough to support the agility of physical and virtual apps, including multi-tier environments and databases, while reducing capital and operating costs. Read this case study to learn how a global-level Fortune 1000 company was able to deploy an entire 6+ TB Oracle eCommerce stack in Amazon Web …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds