怎樣洗掉一個里面有檔案的檔案夾 連檔案一起刪掉 求具體代碼 謝謝
uj5u.com熱心網友回復:
RemoveDiruj5u.com熱心網友回復:
用Shell洗掉的函式,供你參考:
function DeleteDirectory(const DirName: string; const UI: Boolean = False): Boolean;
{
洗掉目錄
}
var
fo: TSHFILEOPSTRUCT;
begin
FillChar(fo, SizeOf(fo), 0);
with fo do
begin
Wnd := GetActiveWindow;
wFunc := FO_DELETE;
pFrom := PChar(DirName + #0);
pTo := #0#0;
fFlags := FOF_NOCONFIRMATION + FOF_SILENT;
end;
Result := (SHFileOperation(fo) = 0);
end;
function ClearDirectory(const DirName: string; const IncludeSub, ToRecyle: Boolean): Boolean;
{
清除目錄
}
var
fo: TSHFILEOPSTRUCT;
begin
FillChar(fo, SizeOf(fo), 0);
with fo do
begin
Wnd := GetActiveWindow;
wFunc := FO_DELETE;
pFrom := PChar(DirName + '\*.*' + #0);
pTo := #0#0;
fFlags := FOF_SILENT or FOF_NOCONFIRMATION or FOF_NOERRORUI
or (Ord(not IncludeSub) * FOF_FILESONLY)
or (ORd(ToRecyle) or FOF_ALLOWUNDO);
end;
Result := (SHFileOperation(fo) = 0);
end;
uj5u.com熱心網友回復:
function DelDirectory(const Source:string): Boolean;
var
fo: TSHFILEOPSTRUCT;
begin
FillChar(fo, SizeOf(fo), 0);
with fo do
begin
Wnd := 0;
wFunc := FO_DELETE;
pFrom := PChar(source+#0);
pTo := #0#0;
fFlags := FOF_NOCONFIRMATION+FOF_SILENT;
end;
Result := (SHFileOperation(fo) = 0);
end;
uj5u.com熱心網友回復:
2010中的用法 更簡單uses
IOUtils;
procedure TForm1.Button1Click(Sender: TObject);
begin
TDirectory.Delete('D:\TDDownload\fa', true);
end;
true時可以洗掉非空檔案夾
uj5u.com熱心網友回復:
同意三樓!uj5u.com熱心網友回復:
學習學習
uj5u.com熱心網友回復:
圍觀大神們的回答
uj5u.com熱心網友回復:
一個我用的函式,洗掉一個檔案夾,引數豐富,功能強大,function DeleteTree(DeleteTreePath: string; IncludeCurrDir: boolean = true; DelFaileFileLst: TStrings = nil; DelSuccessFileLst: TStrings = nil): boolean; /// 洗掉一個目錄及其下所有檔案
function DeleteFileIntr(_STPath: string): boolean;
var
f: TSearchRec;
// R: boolean;
begin
result := false;
if Length(_stPath) = 0 then exit;
if _StPath[length(_StPath)] <> '\' then
_StPath := _stPath + '\';
Result := true;
if FindFirst(_StPath + '*.*', faDirectory + faReadOnly + faHidden + faSysFile, f) = 0 then
begin
repeat
if f.Attr and faDirectory > 0 then
if (f.Name = '.') or (f.name = '..') then
else
begin
if DeleteFileIntr(_StPath + f.name + '\') then
if not RemoveDir(_StPath + f.name) then
Result := false;
end;
until FindNext(f) <> 0;
FindClose(f);
end;
if FindFirst(_StPath + '*.*', faArchive + faReadOnly + faHidden + faSysFile, f) = 0 then
begin
repeat
if f.Attr and faDirectory = 0 then
if (f.Name = '.') or (f.name = '..') then
else
begin
if not DeleteFile(_StPath + f.name) then
begin
Result := false;
if DelFaileFileLst <> nil then DelFaileFileLst.Add(_StPath + f.name + ' ...' + SysErrorMessage(GetlastError));
end else
begin
if DelSuccessFileLst <> nil then DelSuccessFileLst.Add(_StPath + f.name);
end;
end;
until FindNext(f) <> 0;
FindClose(f);
end;
end;
begin
// if DelFaileFileLst <> nil then DelFaileFileLst.Clear;
// if DelSuccessFileLst <> nil then DelSuccessFileLst.Clear;
Result := DeleteFileIntr(DeleteTreePath);
if Length(DeleteTreePath) = 0 then exit;
if IncludeCurrDir then
begin
if DeleteTreePath[length(DeleteTreePath)] = '\' then
DeleteTreePath := Copy(DeleteTreePath, 1, Length(DeleteTreePath) - 1);
if not RemoveDir(DeleteTreePath) then
Result := false;
end;
end;
uj5u.com熱心網友回復:
回圈一下,看看是單個目錄,還是多個級目錄uj5u.com熱心網友回復:
uj5u.com熱心網友回復:
---- 2、洗掉目錄---- 洗掉目錄與拷貝目錄很類似,但為了能洗掉位于根目錄下的一個空目錄,需要在輔助函式中設定一個標志變數,即:如果洗掉的是空目錄,則置bEmptyDir為True,這一句已經用深色框表示了。
---- 2.1洗掉目錄的遞回輔助函式:DoRemoveDir
function DoRemoveDir(sDirName:String):Boolean;
var
hFindFile:Cardinal;
tfile:String;
sCurDir:String;
bEmptyDir:Boolean;
FindFileData:WIN32_FIND_DATA;
begin
//如果洗掉的是空目錄,則置bEmptyDir為True
//初始時,bEmptyDir為True
bEmptyDir:=True;
//先保存當前目錄
sCurDir:=GetCurrentDir;
SetLength(sCurDir,Length(sCurDir));
ChDir(sDirName);
hFindFile:=FindFirstFile( '*.* ',FindFileData);
if hFindFile < > INVALID_HANDLE_VALUE then
begin
repeat
tfile:=FindFileData.cFileName;
if (tfile= '. ') or (tfile= '.. ') then
begin
bEmptyDir:=bEmptyDir and True;
Continue;
end;
//不是空目錄,置bEmptyDir為False
bEmptyDir:=False;
if FindFileData.dwFileAttributes=
FILE_ATTRIBUTE_DIRECTORY then
begin
if sDirName[Length(sDirName)] < > '\ ' then
DoRemoveDir(sDirName+ '\ '+tfile)
else
DoRemoveDir(sDirName+tfile);
if not RemoveDirectory(PChar(tfile)) then
result:=false
else
result:=true;
end
else
begin
if not DeleteFile(PChar(tfile)) then
result:=false
else
result:=true;
end;
until FindNextFile(hFindFile,FindFileData)=false;
FindClose(hFindFile);
end
else
begin
ChDir(sCurDir);
result:=false;
exit;
end;
//如果是空目錄,則洗掉該空目錄
if bEmptyDir then
begin
//回傳上一級目錄
ChDir( '.. ');
//洗掉空目錄
RemoveDirectory(PChar(sDirName));
end;
//回到原來的目錄下
ChDir(sCurDir);
result:=true;
end;
---- 2.2洗掉目錄的函式:DeleteDir
function DeleteDir(sDirName:String):Boolean;
begin
if Length(sDirName) < =0 then
exit;
//洗掉...
Result:=DoRemoveDir(sDirName) and RemoveDir(sDirName);
end;
uj5u.com熱心網友回復:
感謝各位分享上面的方法,我測驗了,如果 當前有檔案在使用的話,不沒法 洗掉的
必須先 退出 所有程式后,才能清理所有檔案及檔案夾的
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/113192.html
標籤:數據庫相關
