음.. 링크된 임프님의 소스로 컴포넌트 등록하고 실행하니
전체찾기가 안되고 첫번째 디렉토리만 찾아집니다.
아래는 제가 약간 수정한 코드입니다.
두개의 각 파일을 파일명같이 저장하고 컴포넌트 인스톨에서 인스톨하면
컴포넌트가 생깁니다. "imp" 라는 컴포넌트가 생김.
그러면 새로은 프로젝트에서 이 컴포넌트를 폼에 올려두고 사용하시면 됩니다.
님께서 특정 파일 예를 들어 *.abc 라는 파일을 모두 지우고 싶다면
컴포넌트에서 FileMask 에 *.abc 라고 쓰로
이밴트에서 OnFileFound 이밴트에 이파일을 지우는 코드를 입력 하면 됩니다.
DeleteFile(CurrentPath + F.Name);
새로운 이밴트를 등록할 필요까진 없습니다. 그럼 성공하시길~
수정한 부분은 붉은색으로 표기하였습니다.
****************************************************************************
ImpFindFile.cpp
****************************************************************************
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "ImpFindFile.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
static inline void ValidCtrCheck(TImpFindFile *)
{
new TImpFindFile(NULL);
}
//---------------------------------------------------------------------------
namespace Impfindfile
{
void __fastcall PACKAGE Register()
{
TComponentClass classes[1] = {__classid(TImpFindFile)};
RegisterComponents("Imp", classes, 0);
}
}
//---------------------------------------------------------------------------
__fastcall TImpFindFile::TImpFindFile(TComponent* Owner)
: TComponent(Owner)
{
FAborted = false;
FWorking = false;
FFileCount = 0;
FDirCount = 0;
FTargetPath = "c:\\";
FFileMask = "*.*";
FFileType = TFileType() << ftReadOnly << ftHidden << ftSystem << ftVolumeID << ftDirectory << ftArchive << ftNormal;
FRecursive = true;
//FSubDirList = new TStringList;
}
__fastcall TImpFindFile::~TImpFindFile(void)
{
//delete FSubDirList;
}
void __fastcall TImpFindFile::SetTargetPath(AnsiString Value)
{
if(DirectoryExists(Value))
FTargetPath = Value;
}
void __fastcall TImpFindFile::Abort(void)
{
FAborted = true;
}
void __fastcall TImpFindFile::Execute(void)
{
FFileCount = 0;
FDirCount = 0;
AnsiString CurrentPath = GetCurrentDir();
if(SetCurrentDir(TargetPath)==false)
return;
int Attributes[7] = {faReadOnly, faHidden, faSysFile, faVolumeID, faDirectory, faArchive, 0};
int AttrWord = DDL_READWRITE;
for(int i=ftReadOnly; i<=ftArchive; i++)
if(FileType.Contains((TFileAttr)i))
AttrWord = AttrWord | Attributes[i];
FindFiles();
FAborted = false;
SetCurrentDir(CurrentPath);
}
void __fastcall TImpFindFile::FindFiles(void)
{
TStringList *FSubDirList;
TSearchRec Sr;
AnsiString CurrentPath = GetCurrentDir();
if(FindFirst(FFileMask, FAttr, Sr)==0)
{
do
{
FFileCount++;
if(FOnFileFound!=NULL) FOnFileFound(this, CurrentPath, Sr);
Application->ProcessMessages();
}
while(FindNext(Sr)==0 && !FAborted);
FindClose(Sr);
if(FAborted) return;
}
if(FRecursive && FindFirst("*.*", faDirectory, Sr)==0)
{
FSubDirList = new TStringList();
FSubDirList->Clear();
do
if((Sr.Attr & faDirectory) == faDirectory && Sr.Name[1] != '.')
FSubDirList->Add(Sr.Name);
while(FindNext(Sr)==0 && !FAborted);
FindClose(Sr);
if(FAborted) return;
for(int i=0; i<FSubDirList->Count; i++)
{
FDirCount++;
if(FOnDirFound!=NULL) FOnDirFound(this, CurrentPath, FSubDirList->Strings[i]);
SetCurrentDir(FSubDirList->Strings[i]);
FindFiles();
SetCurrentDir(CurrentPath);
if(FOnDirExit!=NULL) FOnDirExit(this, CurrentPath, FSubDirList->Strings[i]);
}
delete FSubDirList;
}
}
*****************************************************************************
ImpFindFile.h
*****************************************************************************
//---------------------------------------------------------------------------
#ifndef ImpFindFileH
#define ImpFindFileH
//---------------------------------------------------------------------------
#include <SysUtils.hpp>
#include <Controls.hpp>
#include <Classes.hpp>
#include <Forms.hpp>
#include <FileCtrl.hpp>
//---------------------------------------------------------------------------
typedef void __fastcall (__closure *TFileFoundEvent)(TObject *Sender, AnsiString &CurrentPath, TSearchRec &F);
typedef void __fastcall (__closure *TDirFoundEvent)(TObject *Sender, AnsiString &CurrentPath, AnsiString &DirName);
class PACKAGE TImpFindFile : public TComponent
{
private:
int FAttr;
int FAborted;
//TStringList *FSubDirList;
bool FWorking;
int FFileCount;
int FDirCount;
AnsiString FTargetPath;
AnsiString FFileMask;
TFileType FFileType;
bool FRecursive;
TFileFoundEvent FOnFileFound;
TDirFoundEvent FOnDirFound;
TDirFoundEvent FOnDirExit;
void __fastcall SetTargetPath(AnsiString Value);
protected:
void __fastcall FindFiles(void);
public:
__fastcall TImpFindFile(TComponent* Owner);
__fastcall virtual ~TImpFindFile(void);
void __fastcall Execute(void);
void __fastcall Abort(void);
__property bool Working = {read=FWorking};
__property int FileCount = {read=FFileCount};
__property int DirCount = {read=FDirCount};
__published:
__property AnsiString TargetPath = {read=FTargetPath, write=SetTargetPath};
__property AnsiString FileMask = {read=FFileMask, write=FFileMask};
__property TFileType FileType = {read=FFileType, write=FFileType};
__property bool Recursive = {read=FRecursive, write=FRecursive};
__property TFileFoundEvent OnFileFound = {read=FOnFileFound, write=FOnFileFound};
__property TDirFoundEvent OnDirFound = {read=FOnDirFound, write=FOnDirFound};
__property TDirFoundEvent OnDirExit = {read=FOnDirExit, write=FOnDirExit};
};
//---------------------------------------------------------------------------
#endif
김주성 님이 쓰신 글 :
: 사용법은 간단합니다. *.tif 를 찾으려면 FileMask 프로퍼티에 *tif라고 넣고, 발견되는 대로 몽땅 지우려면, OnFileFound 이벤트 핸들러를 만들어서 넘어온 인자인 CurrentPath와 F를 조합해서 파일명을 만든 후 DeleteFile() 함수로 지우면 됩니다. 다음과 같이 말이죠.
: void __fastcall TForm1::ImpFindFile1FileFound(TObject *Sender,
: AnsiString &CurrentPath, TSearchRec &F)
: {
: DeleteFile(CurrentPath + F.Name);
: }
:
: 여기서 위의 코딩을 어떻게 집어 넣어야 하나요?? 그냥 넣으니깐
: 에러가 나던데요...
: 부탁드립니다.
:
:
:
:
:
:
:
:
:
:
:
:
: 우리 님이 쓰신 글 :
: : 아래링크를 참조하세요
: :
: :
http://cbuilder.borlandforum.com/impboard/impboard.dll?action=read&db=bcb_qna&no=4671
: :
: :
: : 김주성 님이 쓰신 글 :
: : : 안녕하세요.
: : :
: : : visual c++ 로 다음과 같은 프로그램을 만들수 있을까요??
: : :
: : : c:\ 내에 모든 디렉토리에 있는 특정문구가 들어있는 파일을 모두 찾아서 삭제할수 있는
: : :
: : : 코딩을 짜고 싶습니다. 그런것이 가능할까요?? 연구해 보고 싶은데
: : :
: : : 혹시 소스 코드를 알려주실수 있으신가요??