SHFileOperation Wrapper Class

This is the CShellFileOp class. I wrote this so i could use the standard windows explorer copy/move/delete/rename file methods. The idea is to create a CShellFileOp object, add all the source path/filenames and destination path/filenames and then delete/copy/move/rename.

A sample function has been included which shows a multiple file copy operation.



// ShellFileOp.h: interface for the CShellFileOp class.
//	
//	Tim Johnson		timj@progsoc.uts.edu.au
//	Written : July 1998
//
//	Copyright 1998
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_SHELLFILEOP_H__DA3A3661_1EF3_11D2_9E56_444553540000__INCLUDED_)
#define AFX_SHELLFILEOP_H__DA3A3661_1EF3_11D2_9E56_444553540000__INCLUDED_

#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000


//constats for use in iSrcDest arguements
#define SH_SRC_FILE  0
#define SH_DEST_FILE 1

class CShellFileOp  
{
public:

	//
	//simple constructor
	//
	CShellFileOp();
	
	//
	//complex constructor - performs the operation straight away
	//
	CShellFileOp( const HWND phWnd, 
				  const UINT wFunc,
				  const CString sFrom,
				  const CString sTo,
				  const FILEOP_FLAGS fFlags,
				  const CString sProgressTitle);

	//
	//destructor
	//
	virtual ~CShellFileOp();

public: //operations

	//
	// Function to perform copy operation
	//
	long CopyFiles();
	
	//
	// Function to perform delete operation
	//
	long DeleteFiles();
	
	//
	// Function to perform move operation
	//
	long MoveFiles();
	
	//
	// Function to perform rename operation
	//
	long RenameFiles();
	
	//
	// Adds another filename to the end of the current string
	//
	void AddFile(const int iSrcDest, const CString sFile);
	
	//
	// Clears a list of files
	//
	void ClearFiles(const int iSrcDest);
	
	//
	// Function to pre-allocate string memory to prevent 
	//  lots of re-allocations
	//
	void SetMaxCount(const int iSrcDest, const long lMax);
	
	//
	// Function to set the dialog title
	//  sTitle is a string to be used
	//  nTitle is a resource ID to get the string from
	void SetTitle(CString sTitle);
	void SetTitle(const int nTitle);
	
	//
	// Function to set the parent HWND of the dialog
	//
	void SetParent(const HWND phWnd);
	
	//
	// Function to get Abort flag
	//
	BOOL AnyOperationsAborted() const;
	
	//
	// Functions to get/set the operation flags
	//
	//	Flags that control the file operation. This member can be a combination of the 
	//	following values: 
	//
	//	FOF_ALLOWUNDO			Preserves undo information, if possible. 
	//
	//	FOF_CONFIRMMOUSE		Not implemented. 
	//
	//	FOF_FILESONLY			Performs the operation only on files if a wildcard 
	//							filename (*.*) is specified. 
	//
	//	FOF_MULTIDESTFILES		Indicates that the pTo member specifies multiple destination 
	//							files (one for each source file) rather than one directory 
	//							where all source files are to be deposited. 
	//
	//	FOF_NOCONFIRMATION		Responds with "yes to all" for any dialog box that is 
	//							displayed. 
	//
	//	FOF_NOCONFIRMMKDIR		Does not confirm the creation of a new directory if the 
	//							operation requires one to be created. 
	//
	//	FOF_RENAMEONCOLLISION	Gives the file being operated on a new name (such as 
	//							"Copy #1 of...") in a move, copy, or rename operation if a 
	//							file of the target name already exists. 
	//
	//	FOF_SILENT				Does not display a progress dialog box. 
	//
	//	FOF_SIMPLEPROGRESS		Displays a progress dialog box, but does not show the 
	//							filenames. 
	FILEOP_FLAGS GetFlags() const;
	void SetFlags(const FILEOP_FLAGS fNewFlags);

private:	//operations
	
	// function to grab some string space memory
	void GrabMem(const int iSrcDest, const long lNum);

private:	//attributes
	
	//max no. char in source string
	long m_lMaxSrcCount;
	
	//max no. char in dest string
	long m_lMaxDestCount;
	
	//current no. char in source string
	long m_lCurSrcCount;
	
	//current no. char in dest string
	long m_lCurDestCount;
	
	//structure for shell call
	SHFILEOPSTRUCT m_FileOp;
	
	//pointer to start of source string
	char * m_pTo;
	
	//pointer to start of dest string
	char * m_pFrom;
	
	//current pointer in source string
	char * m_pSrc;
	
	//current pointer in dest string
	char * m_pDest;	
	
	//title to be used on dialog
	char * m_pTitle;
};

#endif // !defined(AFX_SHELLFILEOP_H__DA3A3661_1EF3_11D2_9E56_444553540000__INCLUDED_)


Download source - 5 KB

Last updated: November 21, 1998


Comments

  • How to self-delete?

    Posted by Legacy on 06/01/2000 12:00am

    Originally posted by: Nick

    Does anybody have knowledge about:
    How to self-delete the (exe) file of a running app?
    Trying to write an uninstaller which would be nice to delete itself at the end, but it doesn't.
    Any help is appreciated.

    Reply
  • project creation of the expamle

    Posted by Legacy on 05/10/2000 12:00am

    Originally posted by: d-man

    I'm interested in the class but lack the knowledge of how to create a project and implement this in a WIN NT / W9x box environment. Anf info would be GREATLY appreciated.

    thanks ...

    Reply
  • how to handle the end of fucntion?

    Posted by Legacy on 01/03/2000 12:00am

    Originally posted by: lgf

    when you call the SHFileOperation() function ,
    How to handle the end of function?

    Reply
  • SHFileOperation Error Codes

    Posted by Legacy on 12/15/1999 12:00am

    Originally posted by: Erich Hermann

    An operation failed if the function returns non-zero.

    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?

    Regards,
    Erich

    • Meaning of SHFileOperation error codes

      Posted by iatethewholething on 09/21/2006 04:12am

      Check out: http://shellrevealed.com/blogs/shellblog/archive/2006/09/11/Common-Questions-Concerning-the-SHFileOperation-API_3A00_-Part-1.aspx Explains why the error codes returned from SHFileOperation are funky.

      Reply
    Reply
  • Sending to Recycle Bin

    Posted by Legacy on 04/26/1999 12:00am

    Originally posted by: Michael Simons

    Your comments in the source code should have said that FOF_ALLOWUNDO with a DeleteFiles operation will send the file to the recycle bin. The most useful Shell File Operation.

    Reply
  • SHFileOperation wrapper

    Posted by Legacy on 03/31/1999 12:00am

    Originally posted by: Nir Ofer

    As long as you have gone to all that trouble to wrap the
    SHFileOperation why not make it add the NULL seperators by
    itself (whoever is using the class shouldn't mess around
    with NULLs). I did this, and it looks cleaner.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • Packaged application development teams frequently operate with limited testing environments due to time and labor constraints. By virtualizing the entire application stack, packaged application development teams can deliver business results faster, at higher quality, and with lower risk.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds