C CDiskObject: Simplifying common disk operations
http://www.codeproject.com/Articles/6415/CDiskObject-Simplifying-common-disk-operations
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 (useCopyDirectory
, 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 isEF_ONLY_FILENAMES
(default), only the filenames will be returned. If mode isEF_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 isEF_ONLY_FILENAMES
(default), only the filenames will be returned. If mode isEF_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.