How to traverse the entire directory with IDFTP - download, delete
I haven't been published online for a long time, mainly because the level is too stinky, I am afraid I have delayed the future of my brothers, haha!
Less nonsense, cut into the topic.
Do a project in these two days, where you need to download the entire directory on the server on the server, and after downloading, delete the entire directory. Since the FTP cannot be worn, you can only do it in the current directory, so use the general method you can't achieve the expected results. May I want to be lazy! So I want to search from online, I see if there is a ready-made Dongdong to use :)
As a result, I am very disappointed, not that I can't do it, I can't reach my expectation. In fact, some people have asked such problems, and they can have not satisfactorily. Hey! I have to rely on myself! Small Japan can not be so obsessive, don't know if you have heard that Diaoyu Islands, go to the parade.
The following program is implemented with Delphi7.0 IDFTP. There may be BUG, but I hope to bring a little help and prompt to people who need him! The program only downloads and deleted code. As for the uploaded code, you can think it is a little though.
Unit unit1;
Interface
Uses
Windows, Messages, Sysutils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Stdctrls, IDBaseComponent, IDcomponent, IDtcpConnection, IDFTPLIST,
IDTCPCLIENT, IDFTP;
Type
TFORM1 = Class (TFORM)
BTT_DOWNLOADDIR: TBUTTON
IDFTP1: TIDFTP;
BTT_DELETEDIR: TBUTTON;
Label1: TLABEL;
LB_NUM: TLABEL; // Processing the number of items.
Procedure btt_downloaddirclick (sender: TOBJECT);
Procedure btt_deletedirclick (sender: TOBJECT);
Private
{Private Declarations}
public
{Public declarations}
END;
VAR
FORM1: TFORM1;
IMPLEMENTATION
{$ R * .dfm}
{Download the entire directory and traverse all subdireuses
First ChangeDir (root) to the root directory
Then create a local directory RemoteDir
Then get all directory names with List
Circular judge, enter the remotedir directory inside
If it is the directory continues to return. Otherwise get this file to the local directory, return to the previous directory when Get is completed
Use List to get information, continue to circulate
}
Procedure ftp_downloaddir (var idftp: tidftp; remotedir, localdir: string);
Label files;
VAR
I, Dircount: Integer;
Begin
IF not Directoryexists (Localdir RemoteDir) THEN
Forcedirectories (LocalDir Remotedir);
Idftp.changedir (RemoteDir);
Idftp.list (nil);
Dircount: = idftp.directoryListing.count;
if Dircount = 0 THEN
Begin
Idftp.changedirup;
Idftp.list (nil);
END;
For i: = 0 to Dircount - 1 dobegin
IF Dircount <> idftp.directoryListing.count dam
Begin
Repeat
Idftp.changedirup;
Idftp.list (nil);
Until Dircount = idftp.directoryListing.count;
END;
If idftp.directoryListing [i] .ItemType = DITDIRECTORY THEN
FTP_DOWNLOADDIR (idftp, idftp.directoryLISTING [I] .FileName, LocalDir RemoteDir '/')
Else Begin
IDftp.get (idftp.directoryLISTING [I] .FileName, Localdir RemoteDir '/'
Idftp.directoryListing [i] .filename, true);
Form1.lb_num.caption: = INTOSTR (strt (form1.lb_num.caption) 1);
Form1.lb_num.Update;
IF i = Dircount - 1 THEN
Begin
Idftp.changedirup;
Idftp.list (nil);
END;
END;
END;
END;
{Delete the entire FTP directory, including the following files,
Rootdir = The root directory to be deleted, in general, REMOTEDIR and Rootdir equal}
Procedure ftp_deleteallfiles (var idftp: tidftp; remotedir, rootdir: string);
Label files;
VAR
I, Dircount: Integer;
Temp: String;
Begin
Idftp.changedir (RemoteDir);
IF POS (rootdir, idftp.retrievecurrentdir) = 0.
FILES:
Idftp.list (nil);
Dircount: = idftp.directoryListing.count;
While Dircount = 0 DO
Begin
Temp: = idftp.RetrieVecurrentdir;
Idftp.changedirup;
Idftp.removedir (TEMP);
Idftp.list (nil);
Dircount: = idftp.directoryListing.count;
For i: = 0 to Dircount - 1 DO
if idftp.directoryLISTING [I] .filename = rootdir thr;
END;
For i: = 0 to Dircount - 1 DO
Begin
IF POS (rootdir, idftp.retrievecurrentdir) = 0 the Break;
If idftp.directoryListing [i] .ItemType = DITDIRECTORY THEN
Begin
FTP_DELETEALLES (IDFTP, IDFTP.DIRECTORYLISTING [I] .fileName, Rootdir;
ELSE BEGIN
Idftp.delete (idftp.directoryLISTING [I] .filename);
Form1.lb_num.caption: = INTOSTR (STRTOINT (Form1.LB_Num.caption) 1); Form1.lb_num.Update;
Goto files;
END;
END;
END;
Procedure TFORM1.BTT_DOWNLOADDIRCLICK (Sender: TOBJECT);
Begin
Idftp1.connect (true, -1);
if idftp1.connected
Begin
Idftp1.changedir ('Bigimage');
FTP_DOWNLOADDIR (IDFTP1, '1002.1002.1002', 'g: / ftpdir /');
END;
Idftp1.disconnect;
END;
Procedure TFORM1.BTT_DELETEDIRCLICK (Sender: TOBJECT);
Begin
Idftp1.connect (true, -1);
if idftp1.connected
Begin
Idftp1.changedir ('Bigimage');
FTP_DELETEALLES (IDFTP1, '1002.1002.1002', '1002.1002.1002');
END;
Idftp1.disconnect;
END;
End.
Operating environment WIN2000 Advanced Server Delphi7.0 IIS6.0