Click here to Skip to main content
Licence 
First Posted 28 Jul 2002
Views 136,920
Bookmarked 43 times

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

By | 28 Feb 2003 | Article
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

About the Author

Abin



China China

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
GeneralDeprecation warning with Visual 2005 PinmemberBerny2U23:54 21 Dec '06  
GeneralDisc is ready PinmemberGregor Schiller5:58 27 Jul '06  
GeneralBig problem , need help Pinmemberyousefk21:53 8 May '06  
QuestionCopy all files into a folder no tree copy PinsussBenito Loyola9:12 6 Sep '05  
GeneralMkDir is incomplete. PinmemberAnthony_Yio18:31 3 Dec '04  
GeneralPossible Problems Pinmembermehdi_cit22:35 2 Sep '04  
GeneralWhy use SHFILEOPT Pinsussrelipse9:30 19 Jul '04  
General8.3 PinmemberMB319:30 23 May '04  
GeneralI'm lost. (C++, filemanip.h) PinmemberApollAthe1:11 17 Feb '04  
GeneralRe: I'm lost. (C++, filemanip.h) Pinmember=[ Abin ]=2:00 17 Feb '04  
GeneralRe: I'm lost. (C++, filemanip.h) PinmemberApollAthe2:30 17 Feb '04  
GeneralThanks PinmemberYVOTHI4:20 1 Dec '03  
GeneralXcopy. PinmemberCanopenR9:24 9 Oct '03  
GeneralPlease show me .... Pinmemberchatter19:31 8 Oct '03  
GeneralThanks Pinmemberskallestad15:32 2 Oct '03  
GeneralANSI C++ Problem Pinmembernon03216:05 25 Sep '03  
GeneralHelp with accessing folder Pinmembervgandhi8:42 13 Jun '03  
GeneralFile name from file handle Pinmemberbfadi7:41 24 Feb '03  
GeneralRe: File name from file handle PinmemberEric Lapouge21:23 2 Mar '03  
Generalhello Pinmemberarcotkalyan6:41 20 Feb '03  
GeneralSome comments... PinmemberVictor Boctor12:00 13 Feb '03  
GeneralRe: Some comments... Pinmember=[ Abin ]=14:50 13 Feb '03  
GeneralSpeed PinmemberG. Graeber23:51 14 Jan '03  
GeneralRe: Speed Pinmember=[ Abin ]=14:37 13 Feb '03  
Generallicensing question PinmemberEugene Polonsky5:26 22 Nov '02  

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
Web04 | 2.5.120517.1 | Last Updated 1 Mar 2003
Article Copyright 2002 by Abin
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid