//删除文件夹及文件部分
procedure TfrmDownFiles.DelDirFiles(ADirFilesCount: integer);
var
iLoop: integer;
strSourceFlNm, strUpdatedFolder, strCurrPathName, strTempDirFiles: string;
label FreshStart;
label Final;
begin
try
strUpdatedFolder := UpdatedFolder;
FreshStart: // Label 开始
if (ADirFilesCount = 3) and(FtpClient.RetrieveCurrentDir = strTempDir) then
// strTempDir :全局变量,记录 Users目录下的文件夹
begin
// ACount 为 3 表示:文件夹中有 1 个文件夹或文件
goto Final;
end
else
begin
if ADirFilesCount <= 2 then
// ACount 为 2 表示:此文件夹中已经没有文件及文件夹
begin
// 此时,转到上级目录,删除之
strCurrPathName := FtpClient.RetrieveCurrentDir;
if strCurrPathName <> strTempDir then
begin
FtpClient.ChangeDirUp;
FtpClient.RemoveDir(strCurrPathName);
end;
end
else
begin
for iLoop := 0 to ADirFilesCount - 1do
begin
strSourceFlNm := FtpClient.DirectoryListing[iLoop].FileName;
if FtpClient.DirectoryListing[iLoop].ItemType = ditFile then
begin
strTempDirFiles := FtpClient.RetrieveCurrentDir + '/' + strSourceFlNm;
FtpClient.Delete(strTempDirFiles);
break;
end
else
if FtpClient.DirectoryListing[iLoop].ItemType = ditDirectory then
begin
if (strSourceFlNm <> '.') and(strSourceFlNm <> '..') then
begin
if strSourceFlNm = strUpdatedFolder then
continue
else
begin
strTempDirFiles := FtpClient.RetrieveCurrentDir + '/' + strSourceFlNm;
FtpClient.ChangeDir(strTempDirFiles);
FtpClient.List(nil, '*.*', true);
ADirFilesCount := FtpClient.DirectoryListing.Count;
goto FreshStart;
// 转到FRESHSTART 重新分析文件夹,执行过程
end;
end;
end;
end;
end;
FtpClient.List(nil, '*.*', true);
ADirFilesCount := FtpClient.DirectoryListing.Count;
goto FreshStart;
// 转到FRESHSTART 重新分析文件夹,执行过程
end;
Final: // 过程结束
except
end;
end;
//引用部分
procedure TfrmDownFiles.btnDelTestClick(Sender: TObject);
var
iLoop,jCount: integer;
slFileLst: TStringList;
begin
slFileLst := TStringList.Create;
if FtpClient.Connected then
begin
FtpClient.ChangeDir('users');
FtpClient.List(nil, '*.*', true);
for iLoop := 0 to FtpClient.DirectoryListing.Count - 1do
slFileLst.Add(FtpClient.DirectoryListing[iLoop].FileName);
for iLoop:=0 to slFileLst.Count - 1do
begin
if (slFileLst[iLoop] <> '.') and(slFileLst[iLoop] <> '..') then
begin
strTempDir := FtpClient.RetrieveCurrentDir + '/' + slFileLst[iLoop];
FtpClient.ChangeDir(slFileLst[iLoop]);
FtpClient.List(nil, '*.*', true);
jCount := FtpClient.DirectoryListing.Count;
DelDirFiles(jCount);
FtpClient.ChangeDirUp;
end;
end;
end;
end;
//谢谢!!