////////////////////////////////////////////////////////////////////////
///
/// A part of "filecontrol.cpp" , for use in the project 'FoxMemo 2.0'.
/// Author: Jisang Yoo
///
////////////////////////////////////////////////////////////////////////
#include <FileCtrl.hpp> //for 'DirectoryExists()'
#include "filecontrol.h"
//@return: returns true if the function succeeds.
BOOL ClearAndRemoveDirectory(String directory)
{
BOOL ret = true;
ret=ret&& ClearDirectory(directory);
ret=ret&& RemoveDirectory(directory.c_str());
return ret;
}
//@func: Clears all the contents in the specified directory
//@return: returns true if the function succeeds.
BOOL ClearDirectory(String directory)
{
BOOL ret = true;
String dirC = directory + "\\";
String Lfilename;
WIN32_FIND_DATA filedata;
HANDLE filehandle = FindFirstFile((dirC + "*.*").c_str(),&filedata);
if(filehandle != INVALID_HANDLE_VALUE) {
do {
Lfilename = dirC+filedata.cFileName;
if( filedata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
{
if( String(filedata.cFileName) != "." && String(filedata.cFileName) != "..")
{
ret=ret&& ClearDirectory(Lfilename);
ret=ret&& RemoveDirectory(Lfilename.c_str());
}
}
else
ret=ret&& DeleteFile(Lfilename);
}while(FindNextFile(filehandle,&filedata));
FindClose(filehandle);
}
return ret;
}
/* @func: Copy 'ExistingDirectory' to 'NewDirectory'.
'ExistingDirectory' must exists. 'NewDirectory' may or may not exists.(if not exists, 'NewDirectory' is created first.)
'bFailExists' indicates whether the function fails (that is, returns false) when some copying file is already exists.
@return: returns true if the function succeeds. */
BOOL CopyDirectory(String ExistingDirectory,String NewDirectory, BOOL bFailIfExists)
{
BOOL ret = true;
String LfileFrom; String LfileNew;
WIN32_FIND_DATA filedata;
CreateDirectoryIfNotExists(NewDirectory.c_str());
HANDLE filehandle = FindFirstFile((ExistingDirectory + "\\*.*").c_str(),&filedata);
if(filehandle != INVALID_HANDLE_VALUE) {
do {
LfileFrom = ExistingDirectory + "\\" + filedata.cFileName;
LfileNew = NewDirectory + "\\" + filedata.cFileName;
if( filedata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
{
if( String(filedata.cFileName) != "." && String(filedata.cFileName) != "..")
ret=ret&& CopyDirectory(LfileFrom.c_str(),LfileNew.c_str() ,bFailIfExists);
}
else
ret=ret&& CopyFile(LfileFrom.c_str(),LfileNew.c_str() ,bFailIfExists);
}while(FindNextFile(filehandle,&filedata));
FindClose(filehandle);
}
return ret;
}
-----------------------------------------------------------------------------------------
참고할 API함수들.
/* BOOL CopyFile( //<winbase.h>
LPCTSTR lpExistingFileName, // pointer to name of an existing file
LPCTSTR lpNewFileName, // pointer to filename to copy to
BOOL bFailIfExists // flag for operation if file exists
);
@ Return: If the function succeeds, the return value is nonzero.
*/
/* BOOL MoveFile( //<winbase.h>
LPCTSTR lpExistingFileName, //Points to a null-terminated string that names an existing file or directory.
LPCTSTR lpNewFileName //Points to a null-terminated string that specifies the new name of a file or directory. The new name must not already exist. A new file may be on a different file system or drive. A new directory must be on the same drive.
);
@ Return : If the function succeeds, the return value is nonzero.
@ Remark: The MoveFile function will move (rename) either a file or a directory (including all its children) either in the same directory or across directories. The one caveat is that the MoveFile function will fail on directory moves when the destination is on a different volume.
*/
/* BOOL DeleteFile( //<winbase.h>
LPCTSTR lpFileName
);
@ Return: If the function succeeds, the return value is nonzero.
@ Remark: If an application attempts to delete a file that does not exist, the DeleteFile function fails. Windows 95: The DeleteFile function deletes a file even if it is open for normal I/O or as a memory-mapped file. To prevent loss of data, close files before attempting to delete them.
*/
/* BOOL RemoveDirectory( //<winbase.h>
LPCTSTR lpPathName // The path must specify an empty directory, and the calling process must have delete access to the directory.
);
The RemoveDirectory function deletes an existing empty directory.
@ Return: If the function succeeds, the return value is nonzero.
*/
/* BOOL CreateDirectory( //<winbase.h>
LPCTSTR lpPathName, // pointer to a directory path string
LPSECURITY_ATTRIBUTES lpSecurityAttributes // pointer to a security descriptor
);
The CreateDirectory function creates a new directory. There is a default string size limit for paths of MAX_PATH characters.
@ Return: If the function succeeds, the return value is nonzero.
*/
|