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: December 18, 2014 The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this webcast and join industry experts as …

  • On-Demand Webcast APIs can be a great source of competitive advantage. The practice of exposing backend services as APIs has become pervasive, however their use varies widely across companies and industries. Some companies leverage APIs to create internal, operational and development efficiencies, while others use them to drive ancillary revenue channels. Many companies successfully support both public and private programs from the same API by varying levels of access to different constituents. Nearly all …

Most Popular Programming Stories

More for Developers

RSS Feeds