📄 diskobject.cpp
字号:
/* ==========================================================================
Class : CDiskObject
Author : Johan Rosengren, Abstrakt Mekanik AB
Date : 2004-03-12
Purpose : "CDiskObject" encapsulates several high-level file-
and directory operations.
Description : All error handling is managed internally, and the
different API-functions return a "BOOL" to signal success
or failure. In case of failure, "FALSE" returned, the
member function "GetErrorMessage" can be called to get a
"CString" with the error message.
Usage : The class is used by creating a "CDiskObject", and
calling the methods of the class. No other setup is
necessary. If a "CWnd"-pointer is submitted to the "ctor",
"CDiskObject" will give feedback by calling "SetWindowText"
with, for example, filenames during processing.This
means that a "CStatic" (or other appropriate control) can
be set up to display the file currently copied, for
example.
========================================================================
Author : Johan Rosengren, Abstrakt Mekanik AB
Date : 2004-04-14
Purpose : 1. Added const-correctness for inparams.
2. Replacing "/" with "\" when qualifying file names/
directories
3. Skipping directory qualify when the path starts
with \\. Can't add a drive letter then... The path
is assumed to be fully qualified.
4. Added pragma to get rid of C4706 assignment warning.
5. Added "RemoveFile" for reasons of symmetry.
========================================================================
Author : Johan Rosengren, Abstrakt Mekanik AB
Date : 2004-05-14
Purpose : 1. Added default ctor
2. Changed name of "EnumDirectories" to "EnumAllDirectories"
and added a non-recursive "EnumDirectories".
========================================================================
Author : Johan Rosengren, Abstrakt Mekanik AB
Date : 2004-05-19
Purpose 1. Checking FILE_ATTRIBUTE_DIRECTORY as a flag instead
of a value.
========================================================================
Author : Johan Rosengren, Abstrakt Mekanik AB
Date : 2004-08-04
Purpose 1. Changed drive length from _MAX_DRIVE to _MAX_PATH
in UNC parsing. (jkaspzyk)
2. Added UNICODE macro to SetSystemErrorMessage
declaration. (nuhi)
========================================================================*/
#include "stdafx.h"
#include <tchar.h>
#include "DiskObject.h"
#pragma warning( disable : 4706 )
#define Trigger( a) if( m_feedbackWindow ) m_feedbackWindow->SetWindowText( a );
////////////////////////////////////////////////////////////////////
// CDiskObject construction/destruction/initialization
CDiskObject::CDiskObject()
/* ============================================================
Function : CDiskObject::CDiskObject
Description : Constructor
Access : Public
Return : void
Parameters : none
Usage : Should normally be created on the stack.
============================================================*/
{
m_feedbackWindow = NULL;
}
CDiskObject::CDiskObject( CWnd* feedbackWindow )
/* ============================================================
Function : CDiskObject::CDiskObject
Description : Constructor
Access : Public
Return : void
Parameters : CWnd* hwndFeedback - "CWnd" to feedback
window
Usage : Should normally be created on the stack.
============================================================*/
{
m_feedbackWindow = feedbackWindow;
}
CDiskObject::~CDiskObject( )
/* ============================================================
Function : CDiskObject::~CDiskObject
Description : Destructor
Access : Public
Return : void
Parameters : none
Usage : Should normally be created on the stack.
============================================================*/
{
}
////////////////////////////////////////////////////////////////////
// CDiskObject operations
//
// File operations
BOOL CDiskObject::CopyFiles( const CString& sourceDirectory,const CString& destDirectory )
/* ============================================================
Function : CDiskObject::CopyFiles
Description : The member copies all files from
"sourceDirectory" to "destDirectory".
Subdirectories will not be copied.
Access : Public
Return : BOOL - "TRUE" if OK.
"GetErrorMessage"
will return
errors.
Parameters : CString sourceDirectory - Copy from. If
empty, current.
CString destDirectory - Copy to. If
empty, current.
Usage : Call to copy the files from one directory to
another.
============================================================*/
{
ClearError( );
CString source( sourceDirectory );
CString dest( destDirectory );
QualifyPath( source );
QualifyPath( dest );
// First, we enumerate all files
CStringArray files;
BOOL result = EnumFilesInDirectory( source, files );
if( result )
{
// Create the destination directory, if necessary
if( ( result = CreateDirectory( dest ) ) )
{
int max = files.GetSize( );
for( int t = 0 ; t < max ; t++ )
{
// Copy the files
CString file;
file = files[ t ];
Trigger( file );
if( !( result = ::CopyFile( source + file, dest + file, FALSE ) ) )
{
// Set error message
SetSystemErrorMessage( ::GetLastError( ),
source +
file +
_T( " -> " ) +
dest +
file );
t = max;
}
}
}
}
return result;
}
BOOL CDiskObject::CopyFiles( CStringArray& files,const CString& destDirectory )
/* ============================================================
Function : CDiskObject::CopyFiles
Description : The function copies the files in the
"CStringArray" "files" to the directory
"destDirectory". Existing files will be
overwritten. The destination will be
created if it doesn't already exist.
Access : Public
Return : BOOL - "TRUE" if OK.
"GetErrorMessage"
will return
errors.
Parameters : CStringArray& files - a string array
with the files
to copy
CString destDirectory - destination
Usage : Copy a list of files to a directory.
============================================================*/
{
ClearError( );
CString dest( destDirectory );
BOOL result = TRUE;
if( files.GetSize( ) )
{
QualifyPath( dest );
// Create destination, if necessary
if( ( result = CreateDirectory( dest ) ) )
{
int max = files.GetSize( );
for( int t = 0 ; t < max ; t++ )
{
// Loop and copy the files
CString file;
file = files[ t ];
if( file.GetLength( ) )
{
Trigger( file );
QualifyFile( file );
// Create destination filename
CString to = dest + GetFileName( file );
if( !( result = ::CopyFile( file, to, FALSE ) ) )
{
// Set error message
SetSystemErrorMessage( ::GetLastError( ),
file +
_T( " -> " ) +
dest +
file );
t = max;
}
}
}
}
}
else
{
SetInternalErrorMessage( );
result = FALSE;
}
return result;
}
BOOL CDiskObject::CopyFile( const CString& sourceFile,const CString& destDirectory )
/* ============================================================
Function : CDiskObject::CopyFile
Description : Will copy "sourceFile" to "destDirectory".
An existing file will be overwritten. The
directory will be created if it doesn't exist.
Access : Public
Return : BOOL - "TRUE" if OK.
"GetErrorMessage"
will contain
errors
Parameters : CString sourceFile - file to copy
CString destDirectory - destination
Usage : Call to copy a file to a directory.
============================================================*/
{
ClearError( );
CString source( sourceFile );
CString dest( destDirectory );
BOOL result = TRUE;
if( sourceFile.GetLength( ) )
{
QualifyFile( source );
QualifyPath( dest );
// Creating destDirectory if necessary.
if( ( result = CreateDirectory( dest ) ) )
{
CString filePart = GetFileName( source );
// Copy the file
Trigger( filePart );
if( !( result = ::CopyFile( source, dest + filePart, FALSE ) ) )
SetSystemErrorMessage( ::GetLastError( ), source );
}
}
else
{
SetInternalErrorMessage( );
result = FALSE;
}
return result;
}
BOOL CDiskObject::RemoveFile( const CString& sourceFile )
/* ============================================================
Function : CDiskObject::RemoveFile
Description : Will remove "sourceFile".
Access : Public
Return : BOOL - "TRUE" if
removed ok
Parameters : const CString& sourceFile - File to
remove
Usage : Call to delete a file. Added for reasons
of symmetry.
============================================================*/
{
ClearError( );
CString source( sourceFile );
BOOL result = TRUE;
if( sourceFile.GetLength( ) )
{
QualifyFile( source );
if( !( result = ::DeleteFile( source ) ) )
SetSystemErrorMessage( ::GetLastError( ), source );
}
else
{
SetInternalErrorMessage( );
result = FALSE;
}
return result;
}
////////////////////////////////////////
// Directory operations
//
BOOL CDiskObject::CreateDirectory( const CString& directory )
/* ============================================================
Function : CDiskObject::CreateDirectory
Description : Will recursively create the directory
"directory".
Access : Public
Return : BOOL - "TRUE" if OK.
"GetErrorMessage"
will get an
error string if
"FALSE"
Parameters : CString directory - directory to
create
Usage : Call to create a directory chain.
============================================================*/
{
ClearError( );
BOOL result = TRUE;
CString indir( directory );
if( indir.GetLength( ) )
{
QualifyPath( indir );
_TCHAR drive[ _MAX_PATH ];
_TCHAR dir[ _MAX_DIR ];
_TCHAR fname[ _MAX_FNAME ];
_TCHAR ext[ _MAX_EXT ];
// Split directory into parts
_tsplitpath( indir, drive, dir, fname, ext );
TCHAR currentDirectory[ _MAX_PATH ];
::GetCurrentDirectory( _MAX_PATH, currentDirectory );
CStringArray directories;
CString parts = dir;
if( parts.GetLength( ) > 2 )
{
if( parts.Left( 2 ) == _T( "\\\\" ) )
{
// We have an UNC name
CString strComputer;
parts = parts.Right( parts.GetLength( ) - 2 );
int findDir = parts.Find( _TCHAR( '\\' ) );
if( findDir!=-1)
{
strComputer = _T( "\\\\" ) + parts.Left( findDir );
parts = parts.Right( parts.GetLength( ) - ( findDir + 1 ) );
}
_tcscpy( drive, strComputer );
}
}
CString strRoot( drive );
// Strip leading \'s
while( parts.GetLength( ) && parts[0] == _TCHAR( '\\' ) )
parts = parts.Right( parts.GetLength( ) - 1 );
// Cut into separate directories
int find = parts.Find( _TCHAR( '\\' ) );
while( find != -1 )
{
directories.Add( parts.Left( find ) );
parts = parts.Right( parts.GetLength( ) - ( find + 1 ) );
find = parts.Find( _TCHAR( '\\' ) );
}
if( parts.GetLength( ) )
directories.Add( parts );
if( fname )
directories.Add( fname );
// Loop directories one-by-one, creating as necessary
int max = directories.GetSize( );
CString strCurrentDirectory( strRoot );
for( int t = 0 ; t < max ; t++ )
{
strCurrentDirectory += _TCHAR( '\\' ) + directories[ t ];
Trigger( strCurrentDirectory );
if( !( result = ::SetCurrentDirectory( strCurrentDirectory ) ) )
{
if( !( result = ::CreateDirectory( strCurrentDirectory, NULL ) ) )
{
SetSystemErrorMessage( ::GetLastError( ), strCurrentDirectory );
t = max;
}
}
}
::SetCurrentDirectory( currentDirectory );
}
else
{
SetInternalErrorMessage( );
result = FALSE;
}
return result;
}
BOOL CDiskObject::EmptyDirectory( const CString& directory )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -