C++Builder Programming Forum
C++Builder  |  Delphi  |  FireMonkey  |  C/C++  |  Free Pascal  |  Firebird
볼랜드포럼 BorlandForum
 경고! 게시물 작성자의 사전 허락없는 메일주소 추출행위 절대 금지
C++빌더 포럼
Q & A
FAQ
팁&트릭
강좌/문서
자료실
컴포넌트/라이브러리
메신저 프로젝트
볼랜드포럼 홈
헤드라인 뉴스
IT 뉴스
공지사항
자유게시판
해피 브레이크
공동 프로젝트
구인/구직
회원 장터
건의사항
운영진 게시판
회원 메뉴
북마크
볼랜드포럼 광고 모집

C++빌더 팁&트릭
C++Builder Programming Tip&Tricks
[346] 디렉토리 전체를 삭제 및 복사하는 루틴의 구현.
유지상 [newjisang] 8880 읽음    2002-06-15 19:30
////////////////////////////////////////////////////////////////////////
///
///    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.
*/
김윤동.제라툴 [zeratul]   2002-07-04 09:20 X
예전에 유닉스관련 책자를 한권 본적이 있는데 거기에 이와 비슷한거 본기억 이 나는군요.. 정말 좋은 구조내요..

+ -

관련 글 리스트
346 디렉토리 전체를 삭제 및 복사하는 루틴의 구현. 유지상 8880 2002/06/15
Google
Copyright © 1999-2015, borlandforum.com. All right reserved.