// class name CShellFileOp / SHELLFILEOP.H // # 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 overce # Endif //_MSC_VER> = 1000
// constats for use in isrcdest arguementsconst int sh_src_file = 0; const INT 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 deleteFiles (); // // function to perform move operation // long movefiles (); // // Function to Perform Rename Operation // LONG RENAMEFILES (); // // adds another filename to the end isrcdest, const cstring sfile; // // clears a list of Files // void ClearFiles (const ISRCDEST); / / / FUNCTION TO prevent // lots of re-allocations // void setMaxcount (const length); // // 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; //function to set the parent HWnd of the dialog // void setparent (const hwnd pHWND); // Function to get Abort flag // Bool anyopertonsaborted () const; // // functions to get / set the Operation Flags // // FLA gs 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 Deposite. // 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;
#ndif //! defined (AFX_SHELLFILEOP_H__DA3A3661_1EF3_11D2_9E56_444553540000__included _) // shellfileop.cpp // # include "stdafx.h" #include "shellfileop.h"
#ifdef _debug # undef this_filestatic char this_file [] = __ file __; # Define new debug_new # Endif
//// construction / destruction //
//// CShellFileOp () //// Basic constructor // CShellFileOp :: CShellFileOp () {m_lMaxSrcCount = 0; m_lMaxDestCount = 0; m_lCurSrcCount = 0; m_lCurDestCount = 0; m_pSrc = NULL; m_pDest = NULL; m_pTo = NULL; m_pFrom = NULL; m_pTitle = NULL; m_FileOp.hwnd = NULL; m_FileOp.wFunc = 0; m_FileOp.pFrom = NULL; m_FileOp.pTo = NULL; m_FileOp.fFlags = 0; m_FileOp.fAnyOperationsAborted = FALSE; m_FileOp.hNameMappings = NULL; m_fileop.lpszprogresstitle = null;}
//// CShellFileOp //// Complex constructor - performs the operation straight away // CShellFileOp :: CShellFileOp (const HWND phWnd, const UINT wFunc, const CString sFrom, const CString sTo, const FILEOP_FLAGS fFlags, const CString sProgressTitle) {m_lMaxSrcCount = 0; m_lmaxdestCount = 0; m_lcursrccount = 0; m_lcurdestCount = 0; m_psrc = null; m_pdest = null; m_pto = null; m_pFrom = null; m_ptitle = null;
m_FileOp.hwnd = NULL; m_FileOp.wFunc = 0; m_FileOp.pFrom = NULL; m_FileOp.pTo = NULL; m_FileOp.fFlags = 0; m_FileOp.fAnyOperationsAborted = FALSE; m_FileOp.hNameMappings = NULL; m_FileOp.lpszProgressTitle = NULL;
SetParent (PH_SRC_FILE, SFROM); AddFile (SH_DEST_FILE, STO); setFlags (fflags); setTitle (SprogressTitle);
Switch (wfunc) {case fo_copy: CopyFiles (); break; case fo_delete: deletefiles (); break; case fo_move: movefiles (); break; case fo_rename: renamefiles (); break; default: Break;}}
//// ~ cshellfileop () /// Deconstructor - kill all memory before object destruction // cshellfileop :: ~ cshellfileop () {// free all malloc'd blocks if (m_pto) free ((void *) m_pto) ; If (m_pfrom) free ((void *) m_pfrom); if (m_ptitle) free ((void *) m_ptitle);} /// long copyfiles () /// Function to Perform Copy Operation // long cshellfileop: : CopyFiles () {m_fileop.fanyopertonsAborted = false; m_fileop.wfunc = fo_copy; return shfileopert:}
//// long DeleteFiles () //// Function to perform delete operation // long CShellFileOp :: DeleteFiles () {m_FileOp.fAnyOperationsAborted = FALSE; m_FileOp.wFunc = FO_DELETE; return SHFileOperation (& m_FileOp);}
//// long MoveFiles () //// Function to perform move operation // long CShellFileOp :: MoveFiles () {m_FileOp.fAnyOperationsAborted = FALSE; m_FileOp.wFunc = FO_MOVE; return SHFileOperation (& m_FileOp);}
//// long RenameFiles () //// Function to perform rename operation // long CShellFileOp :: RenameFiles () {m_FileOp.fAnyOperationsAborted = FALSE; m_FileOp.wFunc = FO_RENAME; return SHFileOperation (& m_FileOp);}
//// Void Clearfiles (const ISRCDEST) / /// CLIARS A List of files // void cshellfileop :: CLEARFILES (ISRCDEST == sh_src_file) {if (m_pfrom) {free ((void) *) m_pFrom); m_pFrom = NULL; m_FileOp.pFrom = m_pFrom; m_lMaxSrcCount = 0; m_lCurSrcCount = 0;}} else {if (m_pTo) {free ((void *) m_pTo); m_pTo = NULL; m_FileOp.pTo = m_pTo M_LmaxDestCount = 0; m_lcurdestcount = 0;}}}
//// void AddFile (const int iSrcDest, CString sFile) //// Adds another filename to the end of the current string // void CShellFileOp :: AddFile (const int iSrcDest, const CString sFile) {int iLength = sFile. GetLength () 1; // 1 for the nullif (isrcdest == sh_rc_file) {// check enough located space ... IF ((M_LCursrccount ilength 1)> m_lmaxsrccount) // 1 for the double null termination ... {// Have to Get More Meth. Grabmem (ISRCDEST, (M_LCURSRCCVOUNT ILENGTH 1));} // Now theres Enough Memory! Yay. // Now Copy The FileName In StRCPY (M_PSRC, (LPCTSTSTSTSTSTSTSTSTSTSTSTSTSTS SFILE );
// go to end of this files null term. m_psrc = ilngth; m_lcursrcount = ingngth;
// ALWAYS Keep It Double Null Terminated, But Don't // Increment Past IT INCASE WANT To Add More Files M_PSRC [0] = 0;} else {// Check Enough Instatated Space ... IF ((M_LCurDestCount Ilength 1)> m_lmaxdestcount) // 1 for the double null terminal ... {// Have to get more mem. Grabmem (isrcdest, (M_LCurDestCount ilength 1));} // now theres Enough memory! Yay. // Now copy the filename in strcpy (m_pdest, (lpctstr) sfile;
// go to end of this files null term m_pDest = iLength;. M_lCurDestCount = iLength; // always keep it double null terminated, but do not // increment past it incase we want to add more files m_pDest [0] = 0;}}
//// SetMaxCount (const int iSrcDest, long lMax) //// Function to pre-allocate string memory to prevent // lots of re-allocations // void CShellFileOp :: SetMaxCount (const int iSrcDest, const long lMax) { if (iSrcDest == SH_SRC_FILE) {m_lMaxSrcCount = lMax 1;. // 1 for double null term GrabMem (iSrcDest, m_lMaxSrcCount);} else {m_lMaxDestCount = lMax 1; // 1 for double null term GrabMem (. ISRCDEST, M_LMAXDESTCOUNT);}} //// Function to set the dialog title from a string // void cshellfileop :: settitle (cstract stitle) {int tent1ngth; char * PBUF;
// Free MEM OF CURRENT TITLE IF ((void *) m_ptitle; m_ptitle = null; m_fileop.lpszprogresstitle = null;}
Ilength = stitle.getLength () 1;
IF (ilength> 1) {// grab more mem m_ptitle = (char *) malloc (ilength); // copy the title pbuf = stitle.getBuffer; strcpy (m_ptitle, pbuf); // Now Point the structure To the title m_fileop.lpszprogresstitle = m_ptitle;}}
//// sett ntitle //// Function to set the dialog title from a resource Identifier // Void cshellfileop :: setTitle; Stitle; Stitle.LoadString (Ntitle); SetTitle }
//// setparent (const hwnd phwnd) //// Function to set the parent dialog // void cshellfileop :: setparent (const hwnd pHWND) {m_fileop.hwnd = phwnd;}
/// BOOL AnyOperationsaborted () const //// Function to get Abort flag // Bool cshellfileop :: anyopertonsaborted () const {return m_fileop.fanyopertyOperationsAborted;
/// fileop_flags getflags () const //// Function to return the Operation Flags // fileop_flags cshellfileop :: getflags () const {return m_fileop.fflags;}
//// SetFlags (const FILEOP_FLAGS fNewFlags) //// Function to set the operation flags // void CShellFileOp :: SetFlags (const FILEOP_FLAGS fNewFlags) {m_FileOp.fFlags = fNewFlags;} //// GrabMem (const int iSrcDest, Const long lnum) //// Function to Grab Some String Space Memory // Void CshellFileop :: Grabmem (const {RCDEST, Const long lun) {char * PMEM; long loffset
// get current ptr if (isrcdest == sh_src_file) {PMEM = m_pfrom;} else {PMEM = m_pto;}
IF (PMEM) // Some Mem IS Already Allocated! {// Have To Make Sure Our Offset PTRS DONT GET SCRED UP ... IF (ISRCDEST == SH_SRC_FILE) {Loffset = (M_PSRC - PMEM);} else {Loffset = (m_pdest - pmem);
// Get More! PMEM = (char *) Realloc ((void *) PMEM, LNUM);
// reassign offset ptr, and max counts if (iSrcDest == SH_SRC_FILE) {m_pSrc = pMem lOffset; m_lMaxSrcCount = lNum;} else {m_pDest = pMem lOffset; m_lMaxDestCount = lNum;}} else {// get first block pMem = (char *) malloc (lnum);
// assign offset ptr to start of block, and max counts if (iSrcDest == SH_SRC_FILE) {m_pSrc = pMem; m_lMaxSrcCount = lNum;} else {m_pDest = pMem; m_lMaxDestCount = lNum;} pMem [0] = 0; // Ensune Null Terminated} // Assign PTRS in SH structure. if (isrcdest == sh_src_file) {m_pfrom = PMEM; m_fileop.pfrom = m_pfrom
} else {m_pto = pmem; m_fileop.pto = m_pto;}}
Example example ///
Long ccopymovedlg :: DOCOPYMOVEOP (CString sthisfolder) {cshellfileop shop; long lnamelength; int i, icount; cstract sdestdir, ssrcdir; cstring scache
Ssrcdir = sthisfolder;
shOp.SetParent (m_hWnd); shOp.SetFlags (FOF_FILESONLY | FOF_RENAMEONCOLLISION | FOF_NOCONFIRMMKDIR | FOF_MULTIDESTFILES); iCount = m_lstFiles.GetItemCount (); // create the string of filenames to do: fname NULL fname sCache = ((CCacheApp *) AFXGetApp ()) -> Scachedir;
// Calc Number of Chars in Total String LnameLength; For (i = 0; I // now add in all the strings for (i = 0; i // Now add destination directory shop.addfile (sh_dest_file, sthisfolder "//"; // now do the operation return.com .copyFiles ();