Click here to Skip to main content
15,884,472 members
Articles / Desktop Programming / MFC
Article

CFileManip: "DOS-Command-Like" File/Directory Manipulation

Rate me:
Please Sign up or sign in to vote.
4.88/5 (12 votes)
28 Feb 20033 min read 175.5K   3.3K   47   36
A class that allows fast and easy file/directory operation

Sample Image - CFileManip_demo.gif

Introduction

Like many did, I started using computer when DOS 6.x was the main operating system, I was so used of it that now almost ten years passed and I still remember those DOS commands so very clearly, that like I was still typing them yesterday.

I do file/directory manipulation a lot in my programs, every time I wanted to copy or remove a directory including all its files and nested subdirectories to/from customers disk, I thought of those DOS commands, such as "xcopy", "deltree". Of course those can be done by API calls, but wouldn't it be more convenient if there is a class that can do such things by one single line of code, and wouldn't it be even cooler if the class methods look like DOS commands?

CFileManip, an API wrapping class that is developed to make file manipulation simpler and easier. The class itself basically offers "DOS command like" methods which are very similar to related DOS commands in both names and functionalities, I hope they bring you back to the good old time of DOS age.:-) Being able to use functions such as "xcopy" and "deltree" in my C++ code is what I always wanted to.

Progress windows are provided automatically during lengthy tasks. This class does not require MFC, thus may be used in any Win32 applications.

Class Member Methods

// File-only operations
static BOOL Copy(LPCTSTR lpSource, LPCTSTR lpDestination, BOOL bHidePrompt = TRUE);
static BOOL Del(LPCTSTR lpSource, BOOL bHidePrompt = TRUE);
static BOOL Ren(LPCTSTR lpSource, LPCTSTR lpDestination, BOOL bHidePrompt = TRUE);
Parameters
lpSource: Null terminated string that specifies path of source target. Wildcard allowed.
lpDestination: Null terminated string that specifies path of destination target.
bHidePrompt: If set to TRUE, no prompt or confirmation message windows will be displayed, otherwise those message windows may appear upon various situation.

Return value
Returns TRUE if the operation succeeded, FALSE otherwise.

Remarks
As their names explained, they do what DOS commands "copy"(copies files), "del"(deletes files) and "ren"(renames files) do. File-only operations, none of those methods are allowed to modify directories.

// File/directory operations
static BOOL XCopy(LPCTSTR lpSource, LPCTSTR lpDestination, BOOL bHidePrompt = TRUE);
static BOOL DelTree(LPCTSTR lpSource, BOOL bHidePrompt = TRUE);
static BOOL Move(LPCTSTR lpSource, LPCTSTR lpDestination, BOOL bHidePrompt = TRUE);

Parameters
lpSource: Null terminated string that specifies path of source target. Wildcard allowed.
lpDestination: Null terminated string that specifies path of destination target.
bHidePrompt: If set to TRUE, no prompt or confirmation message windows will be displayed, otherwise those message windows may appear upon various situation.

Return value
Returns TRUE if the operation succeeded, FALSE otherwise.

Remarks
As their names explained, they do what DOS commands "xcopy"(copies files or directory including subdirectories to target path, with all attributes and subdirectory structures inherited), "deltree"(deletes files or directory including subdirectories) and "move"(moves files or directory to target path) do. Those methods can modify both files and directories.

// Directory-only operations
static BOOL MkDir(LPCTSTR lpDirectory);
static BOOL RmDir(LPCTSTR lpDirectory);

Parameters
lpDirectory: Null terminated string that specifies directory name.

Return value
Returns TRUE if the operation succeeded, FALSE otherwise.

Remarks
Simple API calls. As their names explained, they do what DOS commands "mkdir"(create an empty directory), "rmdir"(deletes an empty directory. Those operations can not modify files.

// File/directory attributes access
static BOOL SetAttribute(LPCTSTR lpSource, DWORD dwNewAttr);
static DWORD GetAttribute(LPCTSTR lpSource);

Parameters
lpSource: Null terminated string that specifies path of source target.
dwNewAttr: A DWORD value that represents new attributes to be assigned to the source target. A complete list of attributes value can be found in MSDN "Platform SDK: Files and I/O: SetFileAttributes".

Return value
SetAttribute returns TRUE if the operation succeeded, FALSE otherwise.
GetAttribute returns a DWORD value that represents attributes of source target. A complete list of attributes value can be found in MSDN "Platform SDK: Files and I/O: GetFileAttributes".

Remarks
Set and get file or directory attributes.

// File/directory existence check
enum { FM_NOTEXIST = 0, FM_DIRECTORY, FM_FILE};
static int Existence(LPCTSTR lpSource);

Parameters
lpSource: Null terminated string that specifies path of source target.

Return value
Return value can be one of the following:
FM_NOTEXIST: Target does not exist.
FM_DIRECTORY: Target is an existing directory.
FM_FILE: Target is an existing file.

Remarks
Checks existence of a given target.

// Directory travelling
static BOOL CdDotDot(LPTSTR lpCurDirectory = NULL);

Parameters
lpCurDirectory: Null terminated string that will receives full path of current directory after this operation.

Return value
FALSE if current directory is the root directory of a driver, TRUE otherwise.

Remarks
This function does exactly what DOS command "cd.." does, to pull current directory backward(towards the root) one level in the directory tree structure. Calling ::GetCurrentDirectory before and after CdDotDot will show the difference.

History

Aug 3, 2002 - updated downloads.

Feb 12, 2003 - the class has been made UNICODE compliant.

Feb 23, 2003 - removed some unnecessary parts from source 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


Written By
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: Some comments... Pin
Abin13-Feb-03 14:50
Abin13-Feb-03 14:50 
GeneralSpeed Pin
Member 7475914-Jan-03 23:51
Member 7475914-Jan-03 23:51 
GeneralRe: Speed Pin
Abin13-Feb-03 14:37
Abin13-Feb-03 14:37 
Generallicensing question Pin
Eugene Polonsky22-Nov-02 5:26
Eugene Polonsky22-Nov-02 5:26 
GeneralRe: licensing question Pin
Anonymous13-Feb-03 8:03
Anonymous13-Feb-03 8:03 
GeneralDOS Command to copy wildcard folder to another drive Pin
Anonymous20-Nov-02 9:47
Anonymous20-Nov-02 9:47 
GeneralOnly changed files Pin
Brian Delahunty25-Sep-02 9:44
Brian Delahunty25-Sep-02 9:44 
GeneralRe: Only changed files Pin
Dave S being helpful13-Feb-03 2:13
sussDave S being helpful13-Feb-03 2:13 
Assume that X is the original directory and Y is the directory to update.

The simplest way to do that would be to query the list of files in X and Y. If the file in X had a date stamp newer than the file in Y, then move that file to Y. If the file in X didn't exist in Y, then move that file also.

You would have to get a list of files in each directory, loaded into memory, then make a line by line compare. You would have to synchronise the list to handle files in one list that are not in the other.

You might want to think about deleting files that are in Y that aren't in X
GeneralRe: Only changed files Pin
Brian Delahunty13-Feb-03 3:35
Brian Delahunty13-Feb-03 3:35 
GeneralBOOL bHidePrompt = TRUE Pin
mchan20-Aug-02 7:25
mchan20-Aug-02 7:25 
GeneralThe reason why you need to copy the strings Pin
jbarton30-Jul-02 5:00
jbarton30-Jul-02 5:00 
GeneralRe: The reason why you need to copy the strings Pin
Abin30-Jul-02 5:16
Abin30-Jul-02 5:16 
QuestionWhat is the difference between Ren and Move? Pin
Jörgen Sigvardsson29-Jul-02 22:31
Jörgen Sigvardsson29-Jul-02 22:31 
AnswerRe: What is the difference between Ren and Move? Pin
Abin30-Jul-02 4:20
Abin30-Jul-02 4:20 
GeneralRe: What is the difference between Ren and Move? Pin
0xdeadc0de7-Apr-03 0:28
0xdeadc0de7-Apr-03 0:28 

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.