Problem: when the first traversal folder after successful upload, want to delete folder traverse cannot delete folder again, can only delete files, excuse me each greatly, if not release documents after I first traversal or something, how can I do to delete? The code is as follows:
{-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Description: the upload directory button
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -}
Procedure TMainForm. Btn_UploadDirectoryClick (Sender: TObject);
The function DoUploadDir (idftp: TIdFTP; SDirName: String; SToDirName: String; SDirBackup: String) : Boolean;
Var
HFindFile: Cardinal;
Tfile: String;
SCurDir: String [255];
FindFileData: WIN32_FIND_DATA;
The begin
//save the current directory
SCurDir:=GetCurrentDir;
The ChDir (sDirName);
//idFTP. ChangeDir (AnsiToUtf8 (sToDirName));
IdFTP. ChangeDir (sToDirName);
HFindFile:=FindFirstFile (' *. * 'FindFileData);
Application. ProcessMessages;
If hFindFile<> INVALID_HANDLE_VALUE then
The begin
Repeat
Tfile:=FindFileData. CFileName;
If (tfile=') or (tfile='.. ') or (uppercase (tfile)='BACKUP') then
The Continue;
If FindFileData. DwFileAttributes=FILE_ATTRIBUTE_DIRECTORY then
The begin
Try
+ tfile ForceDirectories (sDirBackup + '\');
//IdFTP. MakeDir (AnsiToUtf8 (tfile));
IdFTP. MakeDir (tfile);
Mmo_Log. Lines. The Add (DateTimeToStr (Now) + ' '+' new folder: '+ tfile);
Except,
end;
DoUploadDir (idftp, sDirName + '\' + tfile, tfile, + tfile sDirBackup + '\');
Idftp. ChangeDir ('.. ');
Application. ProcessMessages;
End
The else
The begin
//IdFTP. Put (tfile AnsiToUtf8 (tfile));
IdFTP. Put (tfile tfile);
Mmo_Log. Lines. The Add (DateTimeToStr (Now) + ' '+' upload file: '+ tfile);
Used by CopyFile (PChar (sDirName + '\' + tfile), PChar (sDirBackup + '\' + tfile), False);
Application. ProcessMessages;
end;
Until FindNextFile (hFindFile, FindFileData)=false;
End
The else
The begin
The ChDir (sCurDir);
Result:=false;
exit;
end;
//back to the original directory
The ChDir (sCurDir);
Result:=true;
end;
Var
StrPath, strToPath, temp: string;
The begin
If idftp_Client. Connected=false then
The begin
//not connected
With idftp_Client do
Try
Passive:=True;//passive mode
Username:=Trim (edt_UserName. Text);
Password:=Trim (edt_UserPassword. Text);
Host:=Trim (edt_ServerAddress. Text);
The Connect ().
Self. ChageDir (edt_CurrentDirectory. Text);
The finally
Btn_Connect. Enabled:=True;
If Connected then
Btn_Connect. Caption:='disconnected';
Mmo_Log. Lines. The Add (DateTimeToStr (Now) + ' '+' to connect server ');
end;
end;
If idftp_Client. Connected then
The begin
If chk_AutoUpload. Checked then
The begin
If the Trim (edt_SourceDirectory. Text)='then
The begin
MessageDlg (' source file directory can't be empty, 'mterror, [mbYes], 0).
exit;
End the else
StrPath=Trim (edt_SourceDirectory. Text);
Self. ChageDir ('/');
End the else
If SelectDirectory (' select upload directory ', ' ', strPath)=False then the exit;
//temp:=Utf8ToAnsi (idftp_Client RetrieveCurrentDir);
Temp:=idftp_Client. RetrieveCurrentDir;
StrToPath:=temp;
If Length (strToPath)=1 then
StrToPath:=strToPath + ExtractFileName (strPath)
The else
StrToPath: strToPath +='/' + ExtractFileName (strPath);
Try
//idftp_Client. MakeDir (AnsiToUtf8 (ExtractFileName (strPath)));
Idftp_Client. MakeDir (ExtractFileName (strPath));
Except,
end;
ForceDirectories (strPath + '\ BACKUP');//create the backup directory
DoUploadDir (idftp_Client strPath, strToPath, strPath + '\ BACKUP');
Self. ChageDir (temp);
DeleteDir (edt_sourceDirectory. Text);
end;
end;
Procedure TMainForm. DeleteDir (sDirectory: String);
Var
Sr: TSearchRec;
SPath sFile: String;
The begin
//check whether there is behind the directory name '\'
If the Copy (sDirectory, Length (sDirectory), 1) & lt;> '\' then
SPath:=sDirectory + '\'
The else
SPath:=sDirectory;
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
If FindFirst (sPath + '*. *, faAnyFile, sr)=0 then
The begin
Repeat
SFile:=Trim (sr. Name);
If (sFile=') or (sFile='.. ') or (uppercase (sFile)='BACKUP') then
The Continue;
SFile:=sPath + sr. Name;
If (sr. Attr and faDirectory) & lt;> 0 then
DeleteDir (sFile)
Else if (sr. Attr and faAnyFile)=sr. Attr then
The DeleteFile (sFile);//delete files
Until FindNext (sr) & lt;> 0;
FindClose (sr);
end;
RemoveDir (sPath);
end;
CodePudding user response:
File deletion is asynchronous operations, asynchronous operations still unfinished to delete the folder will fail, may be the reason? Try program after each start to delete the last legacy of the empty folders,CodePudding user response:
Delete empty directory at a lower level, and then delete the current directoryCodePudding user response:
If you are using XE, use the lineTDirectory. Delete (sDirectory, True);
If use D7, such as when I didn't say
CodePudding user response: