ShellApi 의 Shell Operation을 이용.
클래스의 static method로 구현.

에러메세지고 뭐고 닥치고 동작수행하는 버전.
자세한 내용은 아래 링크 참조.

ShFileOperation : http://msdn.microsoft.com/en-us/library/bb762164(VS.85).aspx
ShFileOpStruct : http://msdn.microsoft.com/en-us/library/bb759795(v=VS.85).aspx


주의할 점은,
pFrom, pTo 는 String의 리스트라서 각 문자열은 null char로 구분하며, 리스트의 끝은 2개의 null char가 필요함.
리스트로 안해서 그런지 그냥 PWideChar 컨버팅으로 그냥 됨...-_-;

TDir.Delete('삭제할 디렉토리 경로');  <- 이런식으로 사용.



unit uDir;

interface

uses
  SysUtils, ShellApi;

type
  TDir=class
    public
      class function FileOperation(Op: Integer; PathFrom, PathTo: String): Boolean;
      class function Delete(const Path: String): Boolean;
      class function Copy(const PathFrom, PathTo: String): Boolean;
      class function Move(const PathFrom, PathTo: String): Boolean;
      class function Rename(const PathFrom, PathTo: String): Boolean;
  end;


implementation

{ TDir }

// 삭제
// _____________________________________________________________________________
class function TDir.Delete(const Path: String): Boolean;
begin
  Result:=FileOperation(FO_DELETE, Path, '');
end;

// 복사
// _____________________________________________________________________________

class function TDir.Copy(const PathFrom, PathTo: String): Boolean;
begin
  Result:=FileOperation(FO_COPY, PathFrom, PathTo);
end;

// 이동
// _____________________________________________________________________________

class function TDir.Move(const PathFrom, PathTo: String): Boolean;
begin
  Result:=FileOperation(FO_MOVE, PathFrom, PathTo);
end;

// 이름바꾸기
// _____________________________________________________________________________

class function TDir.Rename(const PathFrom, PathTo: String): Boolean;
begin
  Result:=FileOperation(FO_RENAME, PathFrom, PathTo);
end;

// File Operation
// _____________________________________________________________________________

class function TDir.FileOperation(Op: Integer; PathFrom, PathTo: String): Boolean;
var FileOpStruct: TShFileOpStruct;
begin
  FileOpStruct.Wnd:=0;
  FileOpStruct.wFunc:=Op;
  FileOpStruct.pFrom:=PWideChar(ExcludeTrailingPathDelimiter(PathFrom));
  FileOpStruct.pTo:=PWideChar(ExcludeTrailingPathDelimiter(PathTo));
  FileOpStruct.fFlags:=FOF_NOCONFIRMATION or FOF_SILENT or FOF_NOERRORUI;
  FileOpStruct.lpszProgressTitle:=nil;

  Result:=ShFileOperation(FileOpStruct)=0;
end;

end.





Posted by bloodguy
,