|
같은 결과지만,
if(DirectoryExists(fpath)) DirRecursiveScan(fpath);를
if(sr.Attr & faDirectory) DirRecursiveScan(fpath);로 하시고.
한편, 문자열 비교를 할 때는 대소문자가 구별되므로 한 가지로 통일한 후 비교하세요.
const String file_name_to_delete("description.sdo"); //일단 소문자로 했음
if(sr.Name.LowerCase()==file_name_to_delete) DeleteFile(sr.Name);
TSearchRec::Name에는 디렉터리부분은 없고 파일이름과 확장자로만 이루어져 있습니다.
DeleteFile()이 사용하는 현재 디렉터리와 FindFile()/FindNext()가 사용하는 현재 디렉터리가 상호 일치하면 DeleteFile에 디렉터리를 지정하지 않아도 됩니다. 그 것은 쉽지 않은 확율이므로 DeleteFile()의 매개변수에 지정하는 파일이름은 완전한 경로여야 합니다.
즉, 아래 1 또는 2로 해야 합니다.
1. DirRecursiveScan(String path)의 선두 부분에 curpath = ExtractFilePath(path);를 추가
if(sr.Name.LowerCase()==file_name_to_delete) DeleteFile(curpath+sr.Name);
2. if(sr.Name.LowerCase()==file_name_to_delete) DeleteFile(path+sr.Name);
님의 코드에 다른 문제가 있는지 다 살펴보지는 않았음을 말씀드리며
잘 안 되면 계속 질문하시기 바랍니다.
최진호 님이 쓰신 글 :
: 간단한 프로그램 같은데..
:
: 잘안되네요..
:
: 디렉토리와 서브 폴더에서 description.sdo 파일을 발견하면
:
: 삭제하는 프로그램을 만들려고 하는데..잘안되네요..
:
: 고수님의 조언을 부탁드립니다...
:
:
: void __fastcall TForm1::DirRecursiveScan(String path)
: {
: //TODO: Add your source code here
: try
: {
: if(String(path.AnsiLastChar()) != "\\") path += "\\";
: AnsiString fpath, SaveEn, currpath, Filename;
: TSearchRec sr;
: char *pszBuffer;
: int inFile;
: int iFileLen;
:
: int Found = FindFirst(path + "*.*", faArchive | faDirectory, sr);
:
: while(Found == 0)
: {
: fpath = path + sr.Name;
: if(*sr.Name.c_str() != '.')
: {
: if(DirectoryExists(fpath)) DirRecursiveScan(fpath);
: else
: {
:
: if(sr.Name=="description.sdo")
: {
: DeleteFile(sr.Name);
: }
: }
: }
: Found = FindNext(sr);
: }
: FindClose(sr);
: }
: catch(Exception *e)
: {
: throw e;
: }
:
:
: }
:
: void __fastcall TForm1::Button1Click(TObject *Sender)
: {
: String path ="c:\\ex1";
: DirRecursiveScan(path);
: ShowMessage(" 작업이 완료됨");
: }
|