谁给一个删除文件夹里文件的代码或组件(以前的不行)??(100分)

  • 主题发起人 kernel32
  • 开始时间
K

kernel32

Unregistered / Unconfirmed
GUEST, unregistred user!
1。 谁给一个删除文件夹里所有文件和文件夹的代码或组件(要能够执行的!不会有错误!)
如果要删除的文件正在运行而不能删除,该然后处理??
2。 我在安装一个组件的时候,提示找不到dsgnintf.dcu文件。我在 delphi/source/toolsapi/
找到了 dagnintf.pas 文件,怎么样把他编译成为dsgnintf.dcu文件??
 
2.把那个文件的路径加入到tools-environment options-library-library paths里
1.要加入shellapi单元
procedure TForm1.Button1Click(Sender: TObject);

Var

T:TSHFileOpStruct;

P:String;

begin

P:='d:/address';//这里改成你要删除的任意目录名

With T do

Begin

Wnd:=0;

wFunc:=FO_DELETE;

pFrom:=Pchar(P);

pTo:=nil;

fFlags:=FOF_ALLOWUNDO+FOF_NOCONFIRMATION+FOF_NOERRORUI;//标志表明允许恢复,无须确认并不显示出错信息

hNameMappings:=nil;

lpszProgressTitle:='正在删除文件夹';

fAnyOperationsAborted:=False;

End;

SHFileOperation(T);

end;

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;
如果findclose那句话出错,就注释掉,不影响的

 
以上的代码在使用的时候,有一个问题,就是当删除的文件夹里有正在使用的文件(打开的)
的时候,以上的代码不会删除文件,就好象没有执行该代码似的!!!
有何方法象实现deltree c:/windowstemp/*.* /y 这样的效果。
 
你可以参考一下下面的代码,我没有测试过!


//删除子目录及其下文件

//This doesn't check for attributes being set, which might preclude deletion of a file. Put a {$I-} {$I+} pair around the functions that cause the problem.

procedure removeTree (DirName: string);
var
FileSearch: tSearchRec;
begin
{ first, go through and delete all the directories }
chDir (DirName);
FindFirst ('*.*', faDirectory, FileSearch);
while (getlastError = 0) do
begin
if (FileSearch.name <> '.')
AND (FileSearch.name <> '..')
AND ((FileSearch.attr AND faDirectory) <> 0)
then begin
if DirName[length(DirName)] = '/' then
removeTree (DirName+FileSearch.Name)
else
removeTree (DirName+'/'+FileSearch.Name);
ChDir (DirName);
end;
FindNext (FileSearch)
end;
{then, go through and delete all the files }
FindFirst ('*.*', faAnyFile, FileSearch);
while (getlastError = 0) do
begin
if (FileSearch.name <> '.')
AND (FileSearch.name <> '..') then
deletefile(FileSearch.name); //Remove和WorkDir是何意,删除文件?
FindNext (FileSearch)
end;
rmDir (DirName) //应进入上层目录
end;
 
procedure ToRecycle(AHandle: THandle; const ADirName: String);
var
SHFileOpStruct: TSHFileOpStruct;
DirName: PChar;
BufferSize: Cardinal;
begin
BufferSize := Length(ADirName) +1 +1;
GetMem(DirName, BufferSize);
try
FillChar(DirName^, BufferSize, 0);
StrCopy(DirName, PChar(ADirName));

with SHFileOpStruct do
begin
Wnd := AHandle;
wFunc := FO_DELETE;
pFrom := DirName;
pTo := nil;
fFlags := FOF_ALLOWUNDO;

fAnyOperationsAborted := False;
hNameMappings := nil;
lpszProgressTitle := nil;
end;

if SHFileOperation(SHFileOpStruct) <> 0 then
RaiseLastWin32Error;
finally
FreeMem(DirName, BufferSize);
end;
end;
ToRecycle(0, edtRecycleDir.Text);
 
我回去试试!!谢谢各位!
 
顶部