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

  • 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 …

  • "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.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds