Click here to Skip to main content
Licence 
First Posted 26 Nov 1999
Views 140,886
Downloads 2,828
Bookmarked 57 times

CShellFileOp - Wrapper for SHFileOperation

By Michael Dunn | 26 Nov 1999
An easy-to-use wrapper for the Win32 SHFileOperation function
  4.78 (32 votes)

1
1 vote, 5.0%
2
1 vote, 5.0%
3
2 votes, 10.0%
4
16 votes, 80.0%
5
4.78/5 - 32 votes
Sponsored Links
  • Download demo project - 23 Kb
  • Download source files - 9 Kb

    The CShellFileOp class is designed to be an easy-to-use wrapper for the Win32 SHFileOperation() API. This function is powerful, but using it requires lots of bookkeeping.

    CShellFileOp hides the complex details of the API, and instead lets the programmer use familiar C++ constructs. It also does extensive error-checking of the parameters you use. This decreases headaches, reduces the amount of time the programmer loses wading through the docs, and in general makes everyone happy.

    CShellFileOp was written with MSVC 5.0 and tested with 6.0. The sample project is now in 6.0 format. I have also tested it in Unicode on NT 4.

    Example - Using CShellFileOp to copy files

    Below is example code showing the way CShellFileOp covers SHFileOperation(). I have included complete documentation for the class in each of the zip files accompanying this article. The docs are in the file CShellFileOp_docs.html.

    void CSomeDlg::CopySomeFiles()
    {
    CShellFileOp sfo;
    BOOL         bAPICalled;
    int          nAPIReturnVal;
    
        // This example copies a few files to the A: drive.
    
    
        // Pass the full paths to the files to be copied.
    
    
        sfo.AddSourceFile ( _T("c:\\windows\\command\\format.com") );
        sfo.AddSourceFile ( _T("c:\\windows\\command\\fdisk.exe") );
        sfo.AddSourceFile ( _T("c:\\*.com") );
    
        // Pass the destination directory
    
    
        sfo.AddDestFile ( _T("A:\\") );
    
        // Set up a few flags that control the operation.
    
        sfo.SetOperationFlags
        ( FO_COPY,         // the operation type (copy in this case)
          AfxGetMainWnd(), // pointer to parent window
          FALSE,           // flag - silent mode?
          FALSE,           // flag - allow undo?
          FALSE,           // flag - should wild cards affect files only?
          TRUE,            // flag - suppress confirmation messages?
          TRUE,            // flag - suppress confirmation messages 
                           // when making directories?
          FALSE,           // flag - rename files when name collisions occur?
          FALSE );         // flag - simple progress dialog?
              
        // Start the operation.
    
        if ( sfo.Go ( &bAPICalled, &nAPIReturnVal ) )
            {
            // The operation succeeded!
            }
        else
            {
            if ( !bAPICalled )
                {
                // SHFileOperation() wasn't called - check the info you passed
                // in to the CShellFileOp object.  The DEBUG version will
                // throw ASSERTs and/or show TRACE messages to help you out.
                }
            else
                {
                // SHFileOperation() returned nonzero (failure).  That return
                // value is now in nAPIReturnVal.
                }
            }
    }

    Revision History

    October 11, 1998: Version 1.0. First release.
    February 27, 2000: Version 1.1. Fixed a bug in CShellFileOp::Go() that allocated too much memory in Unicode builds.

    You can get the latest updates to this and my other articles at http://home.inreach.com/mdunn/code/

  • 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

    About the Author

    Michael Dunn

    Software Developer (Senior)
    VMware
    United States United States

    Member
    Michael lives in sunny Mountain View, California. He started programming with an Apple //e in 4th grade, graduated from UCLA with a math degree in 1994, and immediately landed a job as a QA engineer at Symantec, working on the Norton AntiVirus team. He pretty much taught himself Windows and MFC programming, and in 1999 he designed and coded a new interface for Norton AntiVirus 2000.
    Mike has been a a developer at Napster and at his own lil' startup, Zabersoft, a development company he co-founded with offices in Los Angeles and Odense, Denmark. Mike is now a senior engineer at VMware.

    He also enjoys his hobbies of playing pinball, bike riding, photography, and Domion on Friday nights (current favorite combo: Village + double Pirate Ship). He would get his own snooker table too if they weren't so darn big! He is also sad that he's forgotten the languages he's studied: French, Mandarin Chinese, and Japanese.
     
    Mike was a VC MVP from 2005 to 2009.

    Sign Up to vote for this article
    Add a reason or comment to your vote: x
    Votes of 3 or less require a comment

    Comments and Discussions

    You must Sign In to use this message board.
    FAQ FAQ 
     
        Noise level  Layout  Per page   
     Msgs 18 to 27 of 31 (Total in Forum: 31) (Refresh)FirstPrevNext
    GeneralRe: The source of my files to copy is a network drivememberIHR5:32 13 Aug '03  
    I resolved this issue by using scripting code. I really found KiXtart very useful. KiXtart is an enhanced DOS scripting language with Windows support. Eventually, I combined my Visual C++ 6.0 applications with a couple of KiXtart scripts to perform my network drive remote copy.
     
    IHR
    GeneralRe: The source of my files to copy is a network drivememberveraart1:57 24 Feb '06  
    Sleepy | :zzz: Took a while Smile | :)
     
    Use in VS2005 a unicode string like L"c:\temp\temp.txt"
    As far as I know you cannot read from network source.
    GeneralConstant Created datesussNaju1:25 9 Sep '02  
    Hi,
    I have a requirement of copying files from one folder to another. But my problem is, after copying the file to another folder, I need the destination file which I copied to have the same created date as the source created date. Can you please suggest what to be done for that.
     
    Thanks
    GeneralRe: Constant Created datememberBlake Miller6:12 26 Nov '02  
    You might need to handle the dates yourself, once the files are in the target directory.
    You might need to capture the information prior to the move operation.
    For an example of preserving the file date/time and security settings of a file, look at the source for the CMirrorFile class in the MFC source code.

     
    C++/MFC/InstallShield since 1993
    GeneralSHFileOperation Error CodesmemberAnonymous14:58 7 Jul '02  
    SHFileOperation will return a non-zero if it has failed ;
     
    Where can I find constants for and meanings of the numbers?
     
    Is there a text-conversion function like FormatMessage to convert the number into a string for output/logging?

    GeneralRe: SHFileOperation Error Codesmembernoirs26:00 13 Apr '05  
    I'm also looking for this, lemme know if you find anything!
    GeneralRe: SHFileOperation Error CodesmemberBartosz Bien2:13 5 Sep '05  
    GetLastError will answer your question. Smile | :)
     
    Regards,
    BB
    AnswerRe: SHFileOperation Error CodesmemberIAteTheWholeThing223:27 20 Sep '06  
    Many of the errors returned from SHFileOperation are in fact old DOS error codes that overlap with current error codes in winerror.h. So you will get the wrong error if you use FormatMessage. Also, SHFileOperation does not set the last error - so calling GetLastError() is not supported.
     
    For a detailed list of what this old error codes actually mean, see the below blog post on http://shellrevealed.com.
     
    http://shellrevealed.com/blogs/shellblog/archive/2006/09/11/Common-Questions-Concerning-the-SHFileOperation-API_3A00_-Part-1.aspx[^]
    GeneralRe: SHFileOperation Error CodesmemberIAteTheWholeThing215:53 3 Oct '06  
    Below is another blog I wrote that lists the common coding errors in using SHFileOperation. I hope you find it useful.
     
    http://shellrevealed.com/blogs/shellblog/archive/2006/09/28/Common-Questions-Concerning-the-SHFileOperation-API_3A00_-Part-2.aspx[^]
    GeneralRe: SHFileOperation Error CodesmemberMick Leong23:09 12 Jul '09  
    Hi
    Is there any chance you can post the links again - that page is not found. Thanks.

    General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    Permalink | Advertise | Privacy | Mobile
    | Web21 | 2.3.110614.1
    Article Copyright 1999 by Michael Dunn
    Everything else Copyright © CodeProject, 1999-2011
    Terms of Use
    Layout: fixed | fluid

    See Also...
    The Daily Insider