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
[76] 디렉토리지우기(하위 폴더에 화일이 있건 없거..자식폴더까정...)
이영수 [] 7812 읽음    2001-02-21 20:15
//---------------------------------------------------------------------------
// 부모 폴더안의 모든 자식폴더 이름 얻기 (재귀 호출)
//
//
void __fastcall TForm1::GetDescendantDir(TStringList *strlstDirNames, AnsiString strPath)
{
    TSearchRec sr;
    try
    {
        if(FindFirst(strPath + "*.*", faAnyFile, sr) ==0)
        {
            do
            {
                if(sr.Name != "." && sr.Name != ".." && sr.Attr == faDirectory)
                {
                    strlstDirNames->Add(strPath + sr.Name + "\\");
                    GetDescendantDir(strlstDirNames, strPath + sr.Name + "\\");
                }
            }
            while(FindNext(sr) == 0);
        }
    }
    __finally
    {
        FindClose(sr);
    }
}


//---------------------------------------------------------------------------
//  GetDescendantDir과 GetAllFile를 이용해서 디렉토리를 지운다.
//
//
void __fastcall TForm1::RemoveDirectory(AnsiString strPath)
{
    TStringList *strlstDescPath = new TStringList;
    TStringList *strlstFileName = new TStringList;
    try
    {
        strlstDescPath->Add(strPath);
        GetDescendantDir(strlstDescPath, strPath);
        while(strlstDescPath->Count)
        {
            GetAllFile(strlstFileName, strlstDescPath->Strings[strlstDescPath->Count-1]);
            while(strlstFileName->Count)
            {
                DeleteFile(strlstFileName->Strings[0]);
                strlstFileName->Delete(0);
            }
            RemoveDir(strlstDescPath->Strings[strlstDescPath->Count-1]);
            strlstDescPath->Delete(strlstDescPath->Count-1);
        }
    }
    __finally
    {
        delete strlstDescPath;
        delete strlstFileName;
    }
}

//---------------------------------------------------------------------------
// 폴더안의 모든 화일이름 얻기
//
//
void __fastcall TForm1::GetAllFile(TStringList *strlstFileName, AnsiString strPath)
{
    TSearchRec sr;
    try
    {
        if(FindFirst(strPath + "*.*", faAnyFile, sr) ==0)
        {
            do
            {
                if(sr.Name != "." && sr.Name != ".." && sr.Attr != faDirectory)
                    strlstFileName->Add(strPath + sr.Name);
            }
            while(FindNext(sr) == 0);
        }
    }
    __finally
    {
        FindClose(sr);
    }
}


void __fastcall TForm1::Button1Click(TObject *Sender)
{
    RemoveDirectory("D:\\20010102\\"); // 지우고자 하는 폴더를 적어준다..끝에 꼭 폴더구분기호 \\를 넣어 줘야 됩니다.
}
//---------------------------------------------------------------------------

+ -

관련 글 리스트
76 디렉토리지우기(하위 폴더에 화일이 있건 없거..자식폴더까정...) 이영수 7812 2001/02/21
Google
Copyright © 1999-2015, borlandforum.com. All right reserved.