Click here to Skip to main content
Licence 
First Posted 30 Mar 2004
Views 192,938
Bookmarked 53 times

Copy, Move and Delete files and directories without using SHFileOperation

By | 30 Mar 2004 | Article
Class that allows file operations without using SHFileOperation

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

About the Author

Fess

Web Developer

Russian Federation Russian Federation

Member



Sign Up to vote   Poor Excellent
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. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionCatching Exception in Copy() PinmemberAnandi.VC22:27 5 Mar '12  
QuestionCan't copy subFolder? PinmemberRobillhood15:51 13 Feb '11  
Generalsaved my drive! thank you Pinmemberbahram8:57 28 Aug '08  
GeneralRe: saved my drive! thank you Pinmemberjoyjjjz20:16 18 Sep '08  
GeneralWarning !! Pinmembersebmahe6:05 9 Mar '07  
Questionwithout MFC ? Pinmemberspacedoudou12:02 25 Jan '07  
QuestionCan you copy to a remote server PinmemberAYcoder6:14 28 Jul '06  
GeneralBoost's FileSystem library is probably a better choice PinsussJoe Montgomery17:56 15 Jun '05  
GeneralRe: Boost's FileSystem library is probably a better choice PinmemberSevenCat16:16 27 Mar '06  
QuestionBug? PinmemberBob Palin18:27 7 Jun '05  
AnswerRe: Bug? PinmemberFess23:03 7 Jun '05  
GeneralRe: Bug? PinmemberBagpuss3:37 30 Dec '05  
Hello,
 
When do you expect to have the new modified code available?
 
John
GeneralNot supporting _UNICODE PinmemberLemaire Guillaume6:09 27 Jan '05  
GeneralRe: Not supporting _UNICODE [modified] PinmemberPeter Weyzen11:53 29 Sep '06  
Questioncan u help me ? Pinmemberthekillerm6:21 18 Dec '04  
AnswerRe: can u help me ? PinmemberFess66222:54 19 Dec '04  
GeneralfolderCopy not working Pinsussctudd19:27 9 Sep '04  
GeneralfolderCopy are working PinmemberFess66223:20 19 Dec '04  
GeneralPoor Copy() functionality ! PinmemberChristophF22:11 31 Aug '04  
GeneralRe: Poor Copy() functionality ! PinmemberFess66222:51 31 Aug '04  
GeneralRe: Poor Copy() functionality ! PinmemberChristophF23:04 31 Aug '04  
GeneralCopyFileEx error Pinmemberrzabek4:08 10 Jul '04  
GeneralRe: CopyFileEx error PinmemberFess6624:03 19 Jul '04  
GeneralWild Cards *.*, *.dat, MyFile.* not supported PinmemberDavid299813:03 4 Apr '04  
GeneralRe: Wild Cards *.*, *.dat, MyFile.* not supported PinmemberFess6624:47 20 Jul '04  

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120529.1 | Last Updated 31 Mar 2004
Article Copyright 2004 by Fess
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid