2009年7月9日

ファイルをごみ箱経由で削除

ファイルをごみ箱に送る(ごみ箱経由で削除する)にはSHFileOperationSHFILEOPSTRUCTのwFuncにFO_DELETEを指定します。またファイル名はNUL文字で区切って最後にNUL文字を2つ重ねる("double-null terminated string")必要があります。
uses
  Windows, SysUtils, Classes, ShellAPI;

function SHDeleteFiles(const Filename: String; WindowHandle: HWND;
                       AllowUndo: Boolean; FilesOnly: Boolean;
                       NoErrorUI: Boolean; Silent: Boolean): Boolean; overload;
function SHDeleteFiles(Filenames: TStrings; WindowHandle: HWND;
                       AllowUndo: Boolean; FilesOnly: Boolean;
                       NoErrorUI: Boolean; Silent: Boolean): Boolean; overload;

function InternalDeleteFiles(const Filenames: String;
                             WindowHandle: HWND;
                             AllowUndo: Boolean;
                             FilesOnly: Boolean; 
                             NoErrorUI: Boolean;
                             Silent: Boolean): Boolean; forward;

function SHDeleteFiles(const Filename: String; WindowHandle: HWND;
                       AllowUndo: Boolean; FilesOnly: Boolean;
                       NoErrorUI: Boolean; Silent: Boolean): Boolean;
var
  FilenameArray: String;
begin

  FilenameArray := ExpandFileName(Filename) + #0;
  Result := InternalDeleteFiles(FilenameArray,WindowHandle,
                                AllowUndo,FilesOnly,NoErrorUI,Silent);

end;

function SHDeleteFiles(Filenames: TStrings; WindowHandle: HWND;
                       AllowUndo: Boolean; FilesOnly: Boolean; 
                       NoErrorUI: Boolean; Silent: Boolean): Boolean;
var
  Index: Integer;
  FilenameArray: String;
begin

  FilenameArray := '';
  for Index := 0 to Filenames.Count - 1 do
  begin
    FilenameArray := FilenameArray + ExpandFileName(Filenames.Strings[Index]) + #0;
  end;
  FilenameArray := FilenameArray + #0;

  Result := InternalDeleteFiles(FilenameArray,WindowHandle,
                                AllowUndo,FilesOnly,NoErrorUI,Silent);

end;

function InternalDeleteFiles(const Filenames: String;
                             WindowHandle: HWND;
                             AllowUndo: Boolean;
                             FilesOnly: Boolean;
                             NoErrorUI: Boolean; 
                             Silent: Boolean): Boolean;
var
  SHOP: TSHFileOpStruct;
begin

  with SHOP do
  begin
    Wnd := WindowHandle;
    wFunc := FO_DELETE;
    pFrom := PChar(Filenames);
    pTo := nil;
    fFlags := FOF_NOCONFIRMATION;
    if AllowUndo = True then
    begin
      fFlags := fFlags or FOF_ALLOWUNDO;
    end;
    if FilesOnly = True then
    begin
      fFlags := fFlags or FOF_FILESONLY;
    end;
    if NoErrorUI = True then
    begin
      fFlags := fFlags or FOF_NOERRORUI;
    end;
    if Silent = True then
    begin
      fFlags := fFlags or FOF_SILENT;
    end;
    fAnyOperationsAborted := False;
    hNameMappings := nil;
    lpszProgressTitle := nil;
  end;

  Result := (SHFileOperation(SHOP) = 0) and (SHOP.fAnyOperationsAborted = False);

end;
元ねたはDelete files with the ability to UNDO他多数。

0 件のコメント: