Click here to Skip to main content
15,887,214 members
Articles / Programming Languages / C++
Article

SHFileOperation Demo

Rate me:
Please Sign up or sign in to vote.
2.76/5 (16 votes)
3 May 20052 min read 99.3K   10   12   10
A simple intro to shell programming.

Introduction

This article is a very simple article, illustrating the basic use of SHFileOperation and how it can be used to provide a multi copying.

Using the code

The code is quite simple, it is an initial explanation of SHFileOperation to illustrate how to copy multi files and multi folders from multi sources to multi destinations. Before starting to explain the code, let me show you the prototype of SHFileOperation along with the structure that is passed to it:

int WINAPI SHFileOperation(LPSHFILEOPSTRUCT lpFileOp);

LPSHFILEOPSTRUCT is as you assumed a pointer to SHFILEOPSTRUCT which is defined as follows:

typedef struct _SHFILEOPSTRUCT
{
    HWND hwnd;
    UINT wFunc;
    LPCSTR pFrom;
    LPCSTR pTo;
    FILEOP_FLAGS fFlags;
    BOOL fAnyOperationsAborted;
    LPVOID hNameMappings;
    LPCSTR lpszProgressTitle;
} SHFILEOPSTRUCT, FAR* LPSHFILEOPSTRUCT;

I won't go into the details of this structure, it can be easily found in the MSDN. The two elements that really concern us in this structure are the pFrom and pTo, which point to the strings containing the source files/folders and destination files/folders respectively. Honestly, I would like to consider them as lists of strings because they can carry several source files/folders separated by '\0' (null) and the last element should be followed by '\0\0' (double null).

To keep this as simple as possible, all the code revolves around filling two string lists.

LPCSTR pFrom;
LPCSTR pTo;

pFrom is a string pointer of the source paths separated by '\0' and terminated by '\0\0'.

pTo is a string pointer of the destination paths separated by '\0' and terminated by '\0\0'.

So, basically what we have to do is fill these two lists and pass the structure to SHFileOperation and the function handles all the details. Of course, we have to set the appropriate flags to get it working as we like. A typical flag setting would be as follows:

FOF_MULTIDESTFILES|FOF_NOCONFIRMMKDIR|FOF_NOCONFIRMATION

The code has been kept as simple as possible. (All the code has been added in the interface- yeah I know bad design :-P.) To keep this as simple as possible and not to get lost in side details, the code has been commented well, however most of the code written is just interface adjusting code. The main action occurs in the following function:

void CMultiCopyDlg::OnBnClickedBtnCopy()
{
    //Declirations and finlizations :-P
    //finilization is basically 
    //adding the second null char '\0' 
    //to the final termination of the string
    szDisFileLst+='\0';
    szFileLst+='\0';
    szDisDireLst+='\0';
    szDirLst+='\0';
    SHFILEOPSTRUCT SHFileOp,SHDirOp;

    if(bFileInList)
    {
        //The File Operation Structure
        ZeroMemory(&SHFileOp, sizeof(SHFILEOPSTRUCT));
        SHFileOp.hwnd = NULL;
        SHFileOp.wFunc = FO_COPY;
        SHFileOp.pFrom = szFileLst;
        SHFileOp.pTo = szDisFileLst;
        SHFileOp.fFlags = FOF_MULTIDESTFILES;    

        //The Copying Function
        SHFileOperation(&SHFileOp);
    }
    //case it was directory
    //this is hear just to show 
    //the diffrence between flags for directories and files
    //and for future extenstions
    if(bDirInList)
    {
        //Directory Copying 
        ZeroMemory(&SHDirOp, sizeof(SHFILEOPSTRUCT));
        SHDirOp.hwnd = NULL;
        SHDirOp.wFunc = FO_COPY;
        SHDirOp.pFrom = szDirLst;
        SHDirOp.pTo = szDisDireLst;
        SHDirOp.fFlags = 
          FOF_MULTIDESTFILES|FOF_NOCONFIRMMKDIR|FOF_NOCONFIRMATION;

        //The Copying Function
        SHFileOperation(&SHDirOp);
    }
}

After the structures have been filled out all we do is fill the structure SHFILEOPSTRUCT and send it to the function for execution.

Where to go from here

Well there is a lot that hasn't been explored by this function plus there is some sort of bug that hasn't been handled while copying folders. The function only copies the files in the source folder to the destination folder and not the whole folder into the destination folder.

Thanks

To Dino Esposito for his wonderful book Visual C++ Windows Shell programming and Ahmad Ismail's article about multi copying (basically the same but I wanted to focus on the main details :-P).

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Saudi Arabia Saudi Arabia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Suggestionretcode = 2? Solution! Pin
djhellita12-Mar-13 7:35
djhellita12-Mar-13 7:35 
GeneralMy vote of 5 Pin
djhellita12-Mar-13 7:33
djhellita12-Mar-13 7:33 
Questionin win XP? Pin
zon_cpp12-Dec-10 19:25
zon_cpp12-Dec-10 19:25 
AnswerRe: in win XP? Pin
zon_cpp14-Dec-10 1:03
zon_cpp14-Dec-10 1:03 
QuestionCan't copy from mutiple sources Pin
User 547427514-Apr-10 1:04
professionalUser 547427514-Apr-10 1:04 
GeneralMy vote of 1 Pin
dafmeister28-Nov-08 1:54
dafmeister28-Nov-08 1:54 
Generalnice and easy Pin
lilesh6-Jul-07 0:29
lilesh6-Jul-07 0:29 
GeneralCommon coding mistakes using SHFileOperation Pin
IAteTheWholeThing23-Oct-06 14:55
IAteTheWholeThing23-Oct-06 14:55 
GeneralDetailed explanation of SHFileOperation error codes Pin
IAteTheWholeThing220-Sep-06 22:52
IAteTheWholeThing220-Sep-06 22:52 
GeneralSample project would be nice Pin
peterchen4-May-05 2:54
peterchen4-May-05 2:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.