Copying File Names from Explorer | CodeGuru

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Feb 5, 1999
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.