Click here to Skip to main content
15,867,308 members
Articles / Desktop Programming / MFC
Article

Copy, Move and Delete files and directories without using SHFileOperation

Rate me:
Please Sign up or sign in to vote.
4.16/5 (27 votes)
30 Mar 20042 min read 283.3K   12.7K   61   37
Class that allows file operations without using SHFileOperation

Image 1

Introduction

There are no methods in VS for copying files and directories (with subdirectories and files) based on API functions. It's necessary to use SHFileOperation function from shell to do it. Sometimes it's inconvenient and this code is designed to fill this space.

Background

This code is a simple wrapper over API functions that allow file and dir operations. Recursion methods are the base of this code. MFC was used for simplification of process (only CString and CFileFind classes used from). If you don't want to use MFC in your project you can change MFC calls to API calls (use STL string and API functions FindFirstFile and FindNextFile instead of CFileFind class).

Features

  1. OverwriteMode is set: If you copy file to the existing file or to the folder where exist file with the same name, it will overwrite it.
  2. OverwriteMode is not set: If you copy file to the existing file or to the folder where exist file with the same name, it will create new file with name 'Copy of ORIGINAL_FILE_NAME'. If file 'Copy of ORIGINAL_FILE_NAME' already exists too, it will create new file with name 'Copy (2) of ORIGINAL_FILE_NAME' and so on.
  3. AskIfReadonly is set: If you try to delete file with readonly attribute the warning message will be shown. During 'replace' operation this flag is ignored.
  4. AskIfReadonly is not set: If you try to delete file with readonly attribute it will delete without any question.
  5. Path presentation: It is unimportant how you represent the path with '\' on end or without it. For example you can set 'c:\\1' or 'c:\\1\\' it's the same. You can copy file to file, file to folder or folder to folder. Just set the source path and destination path.

Using the code

  1. Add files FileOperations.cpp and FileOpearations.h to your project.
  2. In the file where you want to use this class add
    #include "FileOpearations.h"
  3. Create CFileOperation object and use it.

Sample code

#include "stdafx.h"
#include "FileOperations.h"
//
// this code copy 'c:\source' directory and 
// all it's subdirectories and files
// to the 'c:\dest' directory. 
//
CFileOperation fo;      // create object
fo.SetOverwriteMode(false); // reset OverwriteMode flag (optional)
if (!fo.Copy("c:\\source", "c:\\dest")) // do Copy
{
    fo.ShowError(); // if copy fails show error message
}
//
// this code delete 'c:\source' directory and 
// all it's subdirectories and files.
//
fo.SetAskIfReadOnly();   // set AskIfReadonly flag (optional)
if (!fo.Delete("c:\\source")) // do Copy
{
    fo.ShowError(); // if copy fails show error message
}

If some operation failed you can get error code or error string or show error message (see functions, GetErrorCode(), GetErrorString() and ShowError() accordingly).

For more information you can see demo project.

Available methods

  • bool Delete(CString sPathName); // delete file or folder
  • bool Copy(CString sSource, CString sDest); // copy file or folder
  • bool Replace(CString sSource, CString sDest); // move file or folder
  • bool Rename(CString sSource, CString sDest); // rename file or folder
  • CString GetErrorString(); // return error description
  • DWORD GetErrorCode(); // return error code
  • void ShowError(); // show error message
  • void SetAskIfReadOnly(bool bAsk = true); // sets behavior for readonly files(folders)
  • bool IsAskIfReadOnly(); // return current behavior for readonly files(folders)
  • void SetOverwriteMode(bool bOverwrite = false); // sets overwrite mode on/off
  • bool IsOverwriteMode(); // return current overwrite mode
  • bool IsAborted(); // return true if operation was aborted

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
Web Developer
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionLicense question Pin
Member 1407659410-Apr-19 19:32
Member 1407659410-Apr-19 19:32 
QuestionLicense Details Pin
Member 1095292828-Apr-15 1:02
Member 1095292828-Apr-15 1:02 
QuestionI found a bug,when doing move file F:\xfolder to F:\, failed. Pin
mszengg18-Jan-13 23:01
mszengg18-Jan-13 23:01 
SuggestionCould use some polishing Pin
Mattias G17-Aug-12 0:54
Mattias G17-Aug-12 0:54 
GeneralMy vote of 5 Pin
Igor SPb12-Aug-12 23:12
Igor SPb12-Aug-12 23:12 
GeneralMy vote of 5 Pin
Sivaprasad Tamatam5-Jun-12 21:01
Sivaprasad Tamatam5-Jun-12 21:01 
QuestionCatching Exception in Copy() Pin
Anandi.VC5-Mar-12 22:27
Anandi.VC5-Mar-12 22:27 
QuestionCan't copy subFolder? Pin
Robillhood13-Feb-11 15:51
Robillhood13-Feb-11 15:51 
Generalsaved my drive! thank you Pin
Bahram Ettehadieh28-Aug-08 8:57
Bahram Ettehadieh28-Aug-08 8:57 
GeneralRe: saved my drive! thank you Pin
joyjjjz18-Sep-08 20:16
joyjjjz18-Sep-08 20:16 
GeneralWarning !! Pin
sebmahe9-Mar-07 6:05
sebmahe9-Mar-07 6:05 
Questionwithout MFC ? Pin
spacedoudou25-Jan-07 12:02
spacedoudou25-Jan-07 12:02 
QuestionCan you copy to a remote server Pin
AYcoder28-Jul-06 6:14
AYcoder28-Jul-06 6:14 
GeneralBoost's FileSystem library is probably a better choice Pin
Joe Montgomery15-Jun-05 17:56
sussJoe Montgomery15-Jun-05 17:56 
GeneralRe: Boost's FileSystem library is probably a better choice Pin
SevenCat27-Mar-06 16:16
SevenCat27-Mar-06 16:16 
QuestionBug? Pin
Bob Palin7-Jun-05 18:27
Bob Palin7-Jun-05 18:27 
AnswerRe: Bug? Pin
Fess7-Jun-05 23:03
Fess7-Jun-05 23:03 
GeneralRe: Bug? Pin
Bagpuss30-Dec-05 3:37
Bagpuss30-Dec-05 3:37 
GeneralNot supporting _UNICODE Pin
Lemaire Guillaume27-Jan-05 6:09
Lemaire Guillaume27-Jan-05 6:09 
GeneralRe: Not supporting _UNICODE [modified] Pin
Peter Weyzen29-Sep-06 11:53
Peter Weyzen29-Sep-06 11:53 
Questioncan u help me ? Pin
thekillerm18-Dec-04 6:21
thekillerm18-Dec-04 6:21 
AnswerRe: can u help me ? Pin
Fess66219-Dec-04 22:54
Fess66219-Dec-04 22:54 
GeneralfolderCopy not working Pin
zhuangm9-Sep-04 19:27
zhuangm9-Sep-04 19:27 
GeneralfolderCopy are working Pin
Fess66219-Dec-04 23:20
Fess66219-Dec-04 23:20 
GeneralPoor Copy() functionality ! Pin
ChristophF31-Aug-04 22:11
ChristophF31-Aug-04 22:11 

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.