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:39amI though the authour would comment on this, but he/she will not.
ReplyA better utiility would copy the names to the clipboard
Posted by Legacy on 02/28/2002 12:00amOriginally 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:00amOriginally 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
ReplyIn the context menu handler.....
Posted by Legacy on 06/18/1999 12:00amOriginally 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.
ReplyHow to distinguish between copying and moving?
Posted by Legacy on 06/15/1999 12:00amOriginally posted by: ChangYoung Jung
When I want to read the filenames from explore, I don't know which is for copying, or moving.
Replyhow do I know this?
How to set filenames in the clipboard handle?
Posted by Legacy on 02/22/1999 12:00amOriginally posted by: Hemanshu
Reply