Click here to Skip to main content
12,696,920 members (28,464 online)
Click here to Skip to main content
Add your own
alternative version

Stats

188.1K views
12.8K downloads
143 bookmarked
Posted

CDiskObject: Simplifying common disk operations

, 11 Oct 2006 Public Domain
Rate this:
Please Sign up or sign in to vote.
A class for common directory and file list operations.

Sample Image - diskobject.gif

Introduction

Some common disk operations are inexplicably tedious to accomplish in code: getting a list of all CPP-files from a directory, getting a list of files in a directory subtree, copying a directory subtree, or getting all EXE-files from a subtree. CDiskObject simplifies some of those common operations.

Using the code

The class is very simple to use. Instantiate a CDiskObject, and call away! If the ctor is called with a CWnd-pointer, the window pointed to will be used for feedback. The feedback is normally files or directories processed, and error messages.

TRUE is returned if functions are successful. GetErrorMessage can be called to get an error message otherwise, except for FileExists, which returns FALSE if the file doesn't exist - a quite normal case.

The public functions are:

Files

  • BOOL FileExists( CString file )

    Returns TRUE if the file 'file' exists.

  • BOOL CreateFile( CString file )

    Creates the empty file 'file'. If 'file' contains a non-existing subdirectory, it will be created.

  • BOOL CopyFile( CString sourceFile, CString destDirectory )

    Copies 'sourceFile' to 'destDirectory'. 'destDirectory' will be created if it doesn't exist.

  • BOOL CopyFiles( CString sourceDirectory, CString destDirectory)

    Copies all files from 'sourceDirectory' to 'destDirectory'. Subdirectories are not copied (use CopyDirectory, described below, for this). 'destDirectory' will be created if it doesn't exist.

  • BOOL CopyFiles( CStringArray& files, CString destDirectory)

    Copies the files in 'files' to 'destDirectory'. Either the filenames in 'files' must be fully qualified or will be copied from the current directory. 'destDirectory' will be created if it doesn't exist.

Directories

  • BOOL CreateDirectory( CString directory )

    Creates 'directory'. Subdirectories will also be created.

  • BOOL CopyDirectory( CString sourceDirectory, CString destDirectory)

    Copies the contents from 'sourceDirectory' to 'destDirectory'. Subdirectories will not be copied.

  • BOOL EmptyDirectory( CString directory )

    Deletes all files from 'directory'. Subdirectories will not be emptied.

  • BOOL RemoveDirectory( CString directory )

    Remove 'directory' even if it is not empty. Will not remove subdirectories.

  • BOOL CopyDirectories( CString sourceDirectory, CString destDirectory)

    Copies the contents from 'sourceDirectory' to 'destDirectory'. Subdirectories will also be copied.

  • BOOL EmptyDirectories( CString directory )

    Deletes all files from 'directory'. Subdirectories will also be emptied.

  • BOOL RemoveDirectories( CString directory )

    Removes 'directory' even if it is not empty. Will also remove subdirectories.

Enumerations

  • BOOL EnumDirectories( CString sourceDirectory, CStringArray& directories )

    Returns a list of all subdirectories in 'sourceDirectory' in 'directories'. Subdirectories will not be enumerated.

  • BOOL EnumFilesInDirectoryWithFilter( CString filter, CString sourceDirectory, CStringArray& files, int mode = EF_ONLY_FILENAMES)

    Returns a list of all files in 'sourceDirectory' matching the filter in 'filter' in 'files'. If mode is EF_ONLY_FILENAMES (default), only the filenames will be returned. If mode is EF_FULLY_QUALIFIED, the filenames will contain the complete path. Subdirectories will not be searched.

  • BOOL EnumFilesInDirectory( CString sourceDirectory, CStringArray& files, int mode = EF_ONLY_FILENAMES )

    Returns a list of all files in 'sourceDirectory' in 'files'. If mode is EF_ONLY_FILENAMES (default), only the filenames will be returned. If mode is EF_FULLY_QUALIFIED, the filenames will contain the complete path.

  • BOOL EnumAllFiles( CString sourceDirectory, CStringArray& files )

    Returns a list of all files in 'sourceDirectory' in 'files'. The filenames will contain the complete path. Subdirectories will also be searched.

  • BOOL EnumAllFilesWithFilter( CString filter, CString sourceDirectory, CStringArray& files )

    Returns a list of all files in 'sourceDirectory' in 'files' matching 'filter'. The filenames will contain the complete path. Subdirectories will also be searched.

Error handling

  • CString GetErrorMessage()

    Returns an error string in case of an error. If a feedback window is submitted to the ctor, the error message will be displayed there as well.

See the downloadable documentation for additions.

Points of Interest

First of all, a warning. Wiping out a directory subtree is not exactly funny; if it so happens, it was not the expected subtree that got removed. Beware!

Second, the same warning again. EmtpyDirectories or RemoveDirectories will make you very, very unhappy if it happens to point to a directory you did not expect.

This class should really be a collection of static functions, but for different reasons, I wanted to keep a separate error string for each object. I toyed with a version where I added more data members, source directory, destination directory, source file, accessors to those, and reduced the number of in-parameters to the operations instead. It looked better on paper. In practical use, however, the class got harder to use, not easier. I had to call a few functions to set up the class and then apply the operation. It was far too easy to get one of, say, four calls wrong than one single one...

History

  • The Dark Ages

    Initial version.

  • 14/4 2004

    Time for an update. The following has been added:

    • Added const-correctness for in params. Even though CStrings will be copied and the multi-meg systems of today will hardly notice a few more strings, the class will be cumbersome to use in a const-correct environment. And that defeats the purpose...
    • Replacing "/" with "\" when qualifying file names/directories. Shameful oversight on the part of an old DOS-grunt.
    • Added pragma to get rid of the C4706 assignment warning. De-pragma warnings are not exactly a good practice, but neither is the extra code necessary to do tests. I don't assume that my computer will run out of letters anytime soon, but nonetheless, extra code lines add up, and wading through a ton of assignments can easily take the concentration away from more important parts of the code. This is a tricky question, however, readability, maintainability, and such stuff. Finally, it's a matter of personal preference. The good thing with a free source is that you are allowed to rewrite it if you don't like it!
    • Added RemoveFile for reasons of symmetry. The API already has a ::DeleteFile, but as it is simple to add a wrapper, why not?
  • 15/5 2004

    Added a demo-project. The demo project is just that, a demo (see the picture), and no attempt to write an Explorer replacement :-) Note that the application will move and delete directories, and there is no way to undo a removal of a directory branch. So please be careful, I already have lots of doubts doing a demo project - can I live with someone inadvertently wiping out important files? I guess I have to, but you have been warned. Warn, warn. Twice.

  • 24/6 2004

    Long overdue, here comes another update. Based on feedback from Amit Pitaru, I've added a default ctor due to CodeWarrior complaining. A CodeWarrior version of the demo project was also created by Amit (included in the downloads). This is the community spirit - thanks a lot for the help! It is far too easy to forget that MFC can be used in other environments.

    Finally, I've included an HTML-documentation for both the class and the demo project.

  • 6/8 2004
    • Added a _T() macro to the default parameter of SetSystemErrorMessage (nuhi).
    • Changed the size of the drive buffer from _MAX_DRIVE to _MAX_PATH (jkaspzyk).
  • 25/3 2005

    Browse-buttons added to the listboxes in the small demo-application (WREY). This allows running against other computers, for example.

  • 17/4 2005

    Some good additions from Allen Rossouw added to the class:

    • EnumFilesInDirectoryWithFilter returns the file list sorted alphabetically.
    • DirectoryExists added, returning TRUE if a given directory exists.
    • A version of CopyFile allowing copying to a file, with a new name added.
    • FileInformation added, returning a BY_HANDLE_FILE_INFORMATION for the given file added.

    A big thanks to Allen for the help!

  • 15/5 2005

    A bug-correction, and two more members this time:

    • Corrected bug using the wrong variable in CopyFile (Lorenzo H. Martín).
    • Added RenameFile (Lorenzo H. Martín).
    • Added MoveFile.
  • 19/6 2005
    • A non-MFC version of the class added, CStdDiskObject. This class uses std::string/std::wstring and std::vector instead of Cstring and CStringArray, and the C-runtime file-handling functions instead of the Windows API-versions. All the thanks for this go to the Webdude, David Mackay, both for getting me started on this and for the testing assistance. I've included his test file, which will show the usage of many of the functions in a console application.

      Note: only the source file Zip is updated in this batch, not the demo application, documentation, or other stuff.

  • 16/7 2006
    • Numerous small corrections, and a big thanks to all who have contributed reports below, as well as to the incomparable Webdude. Webdude lies behind the delightful little test application for the non-MFC version of the project.
  • 1/10 2006
    • Some bug corrections (most notably in the Create directory functionality).

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication

Share

About the Author

Johan Rosengren
Software Developer (Senior) Abstrakt Mekanik AB
Sweden Sweden
45 years old, married, three kids.

Started with computers more than 20 years ago on a CBM-64.

Read Theoretical Philosophy at the University of Lund.

Working as a C++ consultant developer.

Science-fiction freak. Enjoy vintage punkrock.

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralBug in RenameDirectory Pin
ol-lo14-Apr-09 1:13
memberol-lo14-Apr-09 1:13 
GeneralBug in copyfile [Non MFC Code] Pin
gugy119-Mar-09 2:59
membergugy119-Mar-09 2:59 
GeneralRe: Bug in copyfile [Non MFC Code] Pin
Johan Rosengren21-Mar-09 23:20
memberJohan Rosengren21-Mar-09 23:20 
GeneralRe: Bug in copyfile [Non MFC Code] [modified] Pin
VEMS1-May-09 6:20
memberVEMS1-May-09 6:20 
GeneralRe: Bug in copyfile [Non MFC Code] Pin
gugy16-May-09 1:54
membergugy16-May-09 1:54 
GeneralRe: Bug in copyfile [Non MFC Code] Pin
VEMS6-May-09 2:42
memberVEMS6-May-09 2:42 
GeneralExpression uses dangling pointer Pin
kezhu18-Jun-08 23:12
memberkezhu18-Jun-08 23:12 
GeneralRe: Expression uses dangling pointer Pin
Johan Rosengren22-Jun-08 6:27
memberJohan Rosengren22-Jun-08 6:27 
GeneralRe: Expression uses dangling pointer Pin
kezhu23-Jun-08 0:53
memberkezhu23-Jun-08 0:53 
QuestionHorizontal scroolbars Pin
Niltonc23-Nov-07 0:44
memberNiltonc23-Nov-07 0:44 
AnswerRe: Horizontal scroolbars Pin
Johan Rosengren25-Nov-07 3:57
memberJohan Rosengren25-Nov-07 3:57 
GeneralPotential problem in EnumAllDirs Pin
mykel14-Jun-07 10:50
membermykel14-Jun-07 10:50 
GeneralRe: Potential problem in EnumAllDirs Pin
Johan Rosengren15-Jun-07 21:20
memberJohan Rosengren15-Jun-07 21:20 
GeneralIs it a small bug of the updated version of DiskObject, or....? [modified] Pin
SJPan25-Jul-06 23:28
memberSJPan25-Jul-06 23:28 
GeneralRe: Is it a small bug of the updated version of DiskObject, or....? Pin
Johan Rosengren15-Sep-06 9:06
memberJohan Rosengren15-Sep-06 9:06 
QuestionWhy by VALUE ? Pin
Blake Miller21-Jul-06 6:13
memberBlake Miller21-Jul-06 6:13 
AnswerRe: Why by VALUE ? Pin
Johan Rosengren21-Jul-06 8:02
memberJohan Rosengren21-Jul-06 8:02 
AnswerRe: Why by VALUE ? Pin
John M. Drescher22-Jul-06 3:47
memberJohn M. Drescher22-Jul-06 3:47 
GeneralRe: Why by VALUE ? Pin
Johan Rosengren13-Oct-06 8:16
memberJohan Rosengren13-Oct-06 8:16 
GeneralCopying unc pathed files Pin
mrstu3-Jul-06 18:32
membermrstu3-Jul-06 18:32 
GeneralRe: Copying unc pathed files Pin
Johan Rosengren4-Jul-06 7:12
memberJohan Rosengren4-Jul-06 7:12 
Generalbug in copyfile Pin
Jake04072512-May-06 6:56
memberJake04072512-May-06 6:56 
GeneralSmall bug in CreateDirectory Pin
Ralph24-Apr-06 5:29
memberRalph24-Apr-06 5:29 
GeneralRe: Small bug in CreateDirectory Pin
Johan Rosengren24-Apr-06 7:05
memberJohan Rosengren24-Apr-06 7:05 
GeneralA small bug in EnumAllFilesWithFilter Pin
Jagadeesh VN23-Jan-06 21:22
memberJagadeesh VN23-Jan-06 21:22 
GeneralRe: A small bug in EnumAllFilesWithFilter Pin
Johan Rosengren24-Jan-06 5:57
memberJohan Rosengren24-Jan-06 5:57 
GeneralRename Pin
muratmaman22-Jan-06 5:52
membermuratmaman22-Jan-06 5:52 
GeneralRe: Rename Pin
Johan Rosengren22-Jan-06 9:47
memberJohan Rosengren22-Jan-06 9:47 
GeneralRe: Rename Pin
PhilippeFrance781-Jun-06 4:15
memberPhilippeFrance781-Jun-06 4:15 
GeneralQuestion about enumdirectories Pin
Tom Wright20-Dec-05 11:05
memberTom Wright20-Dec-05 11:05 
GeneralRe: Question about enumdirectories Pin
Johan Rosengren21-Dec-05 8:47
memberJohan Rosengren21-Dec-05 8:47 
GeneralFeedback to other objects Pin
Tom Wright29-Aug-05 5:14
memberTom Wright29-Aug-05 5:14 
GeneralRe: Feedback to other objects Pin
Johan Rosengren30-Aug-05 9:18
memberJohan Rosengren30-Aug-05 9:18 
GeneralSmall bug in EnumAllFilesWithFilter Pin
Peter Prescher30-Jul-05 5:40
memberPeter Prescher30-Jul-05 5:40 
GeneralRe: Small bug in EnumAllFilesWithFilter Pin
Johan Rosengren30-Jul-05 22:03
memberJohan Rosengren30-Jul-05 22:03 
GeneralRe: Small bug in EnumAllFilesWithFilter Pin
Peter Prescher31-Jul-05 0:45
memberPeter Prescher31-Jul-05 0:45 
GeneralVery Useful but Pin
Dong Hoon Lee13-Jul-05 4:06
memberDong Hoon Lee13-Jul-05 4:06 
GeneralRe: Very Useful but Pin
Johan Rosengren13-Jul-05 4:26
memberJohan Rosengren13-Jul-05 4:26 
QuestionCheck if network paths exist? Pin
Jesper Knudsen14-Jun-05 23:07
memberJesper Knudsen14-Jun-05 23:07 
AnswerRe: Check if network paths exist? Pin
Johan Rosengren15-Jun-05 7:03
memberJohan Rosengren15-Jun-05 7:03 
AnswerRe: Check if network paths exist? Pin
Johan Rosengren18-Jun-05 23:38
memberJohan Rosengren18-Jun-05 23:38 
GeneralRe: Check if network paths exist? Pin
Jesper Knudsen20-Jun-05 20:45
memberJesper Knudsen20-Jun-05 20:45 
GeneralRe: Check if network paths exist? Pin
Johan Rosengren21-Jun-05 7:50
memberJohan Rosengren21-Jun-05 7:50 
GeneralRe: Check if network paths exist? Pin
Blake Miller24-Jun-05 5:04
memberBlake Miller24-Jun-05 5:04 
GeneralRe: Check if network paths exist? Pin
DavidCrow29-Jun-05 3:57
memberDavidCrow29-Jun-05 3:57 
GeneralRe: Check if network paths exist? Pin
Jesper Knudsen29-Jun-05 4:14
memberJesper Knudsen29-Jun-05 4:14 
GeneralRe: Check if network paths exist? Pin
DavidCrow29-Jun-05 18:04
memberDavidCrow29-Jun-05 18:04 
AnswerRe: Check if network paths exist? Pin
DavidCrow29-Jun-05 3:53
memberDavidCrow29-Jun-05 3:53 
GeneralThanks Pin
Lorenzo H. Martín12-May-05 0:28
memberLorenzo H. Martín12-May-05 0:28 
GeneralRe: Thanks Pin
Johan Rosengren12-May-05 6:04
memberJohan Rosengren12-May-05 6:04 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 11 Oct 2006
Article Copyright 2004 by Johan Rosengren
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid