Click here to Skip to main content
15,867,895 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.2K   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

 
GeneralDeprecation warning with Visual 2005 Pin
Berny2U21-Dec-06 23:54
Berny2U21-Dec-06 23:54 
GeneralDisc is ready Pin
Gregor Schiller27-Jul-06 5:58
Gregor Schiller27-Jul-06 5:58 
GeneralBig problem , need help Pin
yousefk8-May-06 21:53
yousefk8-May-06 21:53 
QuestionCopy all files into a folder no tree copy Pin
Benito Loyola6-Sep-05 9:12
sussBenito Loyola6-Sep-05 9:12 
GeneralMkDir is incomplete. Pin
Anthony_Yio3-Dec-04 18:31
Anthony_Yio3-Dec-04 18:31 
GeneralPossible Problems Pin
mehdi_cit2-Sep-04 22:35
mehdi_cit2-Sep-04 22:35 
GeneralWhy use SHFILEOPT Pin
pyrokins19-Jul-04 9:30
pyrokins19-Jul-04 9:30 
General8.3 Pin
MB323-May-04 19:30
MB323-May-04 19:30 
GeneralI'm lost. (C++, filemanip.h) Pin
ApollAthe17-Feb-04 1:11
ApollAthe17-Feb-04 1:11 
GeneralRe: I'm lost. (C++, filemanip.h) Pin
Abin17-Feb-04 2:00
Abin17-Feb-04 2:00 
GeneralRe: I'm lost. (C++, filemanip.h) Pin
ApollAthe17-Feb-04 2:30
ApollAthe17-Feb-04 2:30 
GeneralThanks Pin
YVOTHI1-Dec-03 4:20
YVOTHI1-Dec-03 4:20 
GeneralXcopy. Pin
CanopenR9-Oct-03 9:24
CanopenR9-Oct-03 9:24 
GeneralPlease show me .... Pin
chatter8-Oct-03 19:31
chatter8-Oct-03 19:31 
GeneralThanks Pin
skallestad2-Oct-03 15:32
skallestad2-Oct-03 15:32 
GeneralANSI C++ Problem Pin
non03225-Sep-03 16:05
non03225-Sep-03 16:05 
GeneralHelp with accessing folder Pin
vgandhi13-Jun-03 8:42
vgandhi13-Jun-03 8:42 
GeneralFile name from file handle Pin
Fad B24-Feb-03 7:41
Fad B24-Feb-03 7:41 
GeneralRe: File name from file handle Pin
Eric Lapouge2-Mar-03 21:23
Eric Lapouge2-Mar-03 21:23 
Generalhello Pin
arcotkalyan20-Feb-03 6:41
arcotkalyan20-Feb-03 6:41 
GeneralSome comments... Pin
Victor Boctor13-Feb-03 12:00
Victor Boctor13-Feb-03 12:00 
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 

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.