Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
28 Feb 2003 3  
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