掲示板システム
ホーム
アクセス解析
カテゴリ
ログアウト
空フォルダの検出削除をする (ID:35243)
名前
ホームページ(ブログ、Twitterなど)のURL (省略可)
本文
tttさんの言われるとおり再帰で深い順から削除できると思いますよ。 ↓はUnicode対応のAPIを使っていますが、やってることはDelphiのFindFirstなどと同じです。 function TForm1.DeleteEmptyFolder(sPath: WideString): Boolean; //sPathが空のフォルダなら削除してTrueを返す。 var lh_File: THandle; lr_Info: TWin32FindDataW; begin Result := True; if (sPath[Length(sPath)] <> '\') then begin sPath := sPath + '\'; end; lh_File := FindFirstFileW(PWideChar(sPath + '*.*'), lr_Info); if (lh_File <> INVALID_HANDLE_VALUE) then begin try repeat if (BOOL(lr_Info.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY)) then begin //フォルダ。 if (lr_Info.cFileName <> WideString('.')) //カレントフォルダではなく and (lr_Info.cFileName <> WideString('..')) //親フォルダでもない then begin //サブフォルダ。 if not(DeleteEmptyFolder(sPath + lr_Info.cFileName)) then begin //サブフォルダは空ではない。 Result := False; end; end; end else begin //ファイルが存在するのでフォルダは空ではない。 Result := False; end; Application.ProcessMessages; until not(FindNextFileW(lh_File, lr_Info)); finally Windows.FindClose(lh_File); end; end; if (Result) then begin //リストに追加&削除 MyListBox1.Items.Add(sPath); RemoveDirectoryW(PWideChar(sPath)); end; end;
←解決時は質問者本人がここをチェックしてください。
更新する
戻る
掲示板システム
Copyright 2021 Takeshi Okamoto All Rights Reserved.