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

  • The impact of a data loss event can be significant. Real-time data is essential to remaining competitive. Many companies can no longer afford to rely on a truck arriving each day to take backup tapes offsite. For most companies, a cloud backup and recovery solution will eliminate, or significantly reduce, IT resources related to the mundane task of backup and allow your resources to be redeployed to more strategic projects. The cloud - can now be comfortable for you – with 100% recovery from anywhere all …

  • Given today's threat environment and the increasing connectivity of digital infrastructures, security teams now realize that they must assume their IT environments are subject to periodic compromise. Gone are the days when preventive measures to secure the perimeter or trying to detect malware problems using signature-match technologies were enough. New practices based on an understanding of the phases of an attack, continuous threat monitoring, and rapid attack detection and remediation are necessary. This …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds