Click here to Skip to main content
Click here to Skip to main content
Go to top

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

, 28 Feb 2003
Rate this:
Please Sign up or sign in to vote.
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.Smile | :) 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

Share

About the Author

Abin

China China
No Biography provided

Comments and Discussions

 
GeneralDeprecation warning with Visual 2005 PinmemberBerny2U21-Dec-06 23:54 
GeneralDisc is ready PinmemberGregor Schiller27-Jul-06 5:58 
GeneralBig problem , need help Pinmemberyousefk8-May-06 21:53 
QuestionCopy all files into a folder no tree copy PinsussBenito Loyola6-Sep-05 9:12 
GeneralMkDir is incomplete. PinmemberAnthony_Yio3-Dec-04 18:31 
GeneralPossible Problems Pinmembermehdi_cit2-Sep-04 22:35 
GeneralWhy use SHFILEOPT Pinsussrelipse19-Jul-04 9:30 
General8.3 PinmemberMB323-May-04 19:30 
GeneralI'm lost. (C++, filemanip.h) PinmemberApollAthe17-Feb-04 1:11 
GeneralRe: I'm lost. (C++, filemanip.h) Pinmember=[ Abin ]=17-Feb-04 2:00 

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.

| Advertise | Privacy | Mobile
Web03 | 2.8.140926.1 | Last Updated 1 Mar 2003
Article Copyright 2002 by Abin
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid