Copying File Names from Explorer

This code helps you to get a set of file names & use them any time while coding or documenting. When only one filename is required, you generally tend to manually type that filename yourself. However, when more than one filename is required to be typed, you may either try to type all of the filenames or use some other means like DOS-Prompt's "dir /b" command and copy and paste them into your text. Here is one easy way to copy and paste a selection of filenames from Explorer.

The code is simple and easy to use. You just simply invoke Explorer or the Open File Dialog box. After selecting the desired files, click the right mouse button and select 'Copy'.

Windows does not actually copy the file contents into the clipboard. It just stores name references to those files in the clipboard. This code is then used to extract those filenames.

If you verify the clipboard formats when you select files from Explorer, Windows stores around eight formats with different names. CF_HDROP is one of them. This format gives you a handle with which you can query the names of all the selected files. There are other formats which give you only the name of the first file.


COleDataObject odj;
if( odj.AttachClipboard() )
{
 if( odj.IsDataAvailable( CF_HDROP ) )
 {
  STGMEDIUM StgMed;
  FORMATETC fmte = { CF_HDROP, (DVTARGETDEVICE FAR *)NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
  if( odj.GetData( CF_HDROP, &StgMed, &fmte ) )
  {
   HDROP hdrop = (HDROP)StgMed.hGlobal;
   UINT cFiles = ::DragQueryFile(hdrop, (UINT)-1, NULL, 0);

   CString szText;
   szText.Format( "There are %d files/directories\r\n", cFiles );

   char szFile[MAX_PATH];

   for( UINT count = 0; count < cFiles; count++ )
   {
    ::DragQueryFile(hdrop, count, szFile, sizeof(szFile));
    szText += szFile;
    szText += "\r\n";
   }
   if (StgMed.pUnkForRelease)
   {
    StgMed.pUnkForRelease->Release();
   }
   else
   {
    ::GlobalFree(StgMed.hGlobal);
   }
  }
 }
}


Download demo project - 120 KB

Date Last Updated: February 5, 1999



Comments

  • My bad

    Posted by Balder1978 on 10/04/2005 12:39am

    I though the authour would comment on this, but he/she will not.

    Reply
  • A better utiility would copy the names to the clipboard

    Posted by Legacy on 02/28/2002 12:00am

    Originally posted by: Chris

    It would be nifty if a small utility could be built
    around this code to automatically convert the clipboard
    entry from its magical 'file' format into a simple
    'string of file names'. Then I could select some
    files, hit 'copy', run the utility, then select
    'paste' in my editor to get the filenames.

    Reply
  • How to do the same but without COleDataObject?

    Posted by Legacy on 12/05/2001 12:00am

    Originally posted by: Simon

    Hi
    I'm trying to do the same but without COleDataObject.
    I do in the following way

    void TForm1::fTestProc()
    {
    HANDLE pH;
    if(OpenClipboard(this->Handle))
    {
    pH=GetClipboardData(CF_HDROP);
    CloseClipboard();
    }
    }

    if clipboard is empty or does not contain any data in CF_HDROP format function GetClipboardData returns NULL, otherwise it returns handle.
    My questions are:
    1. do I get correct handler?
    2. if so, then what to do with this handler to get files list and number of files.

    Simon

    Reply
  • In the context menu handler.....

    Posted by Legacy on 06/18/1999 12:00am

    Originally posted by: jijon

    Thank u M Basava Raju.


    I want to know extact path of files (and/or folders)
    which are selected when my context menu handler
    DLL is called.

    for example, I added "pack'em up" menu item to
    default context menu of desktop explorer.

    and when user selects files & folders and
    right click on'em, context menu will pop and
    there also my menuitem will exist.

    and then, if user selects my menuitem,
    my context menu handler will called.


    In my handler, I will(wanna) pack all files & folders
    selected as a file.


    How could I do that?


    Because I use context menu handler,
    there is no 'copy' step..

    Some could help??????


    best regards.


    Reply
  • How to distinguish between copying and moving?

    Posted by Legacy on 06/15/1999 12:00am

    Originally posted by: ChangYoung Jung

    When I want to read the filenames from explore, I don't know which is for copying, or moving.
    how do I know this?

    Reply
  • How to set filenames in the clipboard handle?

    Posted by Legacy on 02/22/1999 12:00am

    Originally posted by: Hemanshu

    When data is set as CF_HDROP format in the clip board 
    
    the clipboard returns a handle using which we can get number of files
    and file names. But when I want to implement copying some files from my
    program how to set the handle in the program.

    Actually I want to implement drag and drop also so that
    files selected from my program are dragged and when dropped on explorer
    the explorer should copy those files.

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

Top White Papers and Webcasts

  • Today's agile organizations pose operations teams with a tremendous challenge: to deploy new releases to production immediately after development and testing is completed. To ensure that applications are deployed successfully, an automatic and transparent process is required. We refer to this process as Zero Touch Deployment™. This white paper reviews two approaches to Zero Touch Deployment--a script-based solution and a release automation platform. The article discusses how each can solve the key …

  • Learn How A Global Entertainment Company Saw a 448% ROI Every business today uses software to manage systems, deliver products, and empower employees to do their jobs. But software inevitably breaks, and when it does, businesses lose money -- in the form of dissatisfied customers, missed SLAs or lost productivity. PagerDuty, an operations performance platform, solves this problem by helping operations engineers and developers more effectively manage and resolve incidents across a company's global operations. …

Most Popular Programming Stories

More for Developers

RSS Feeds