Scripting lesson #3: downloading only files modified in the last 24 hours

FtpScripter makes it very easy to perform actions that would be extremely complicated with a standard DOS FTP batch script. One of such actions is – for example – downloading only the files that have been modified during the last day (or whatever timeframe is needed).

Here’s a commented script to achieve such result:

Delphi/Pascal

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

// First of all we need to declare variables... we will need a

// TSFTPClient object, a TDirList object to list files on the

// FTP server, and an integer variable to loop over the

// directory list...

var

Cli:TSFTPClient;

Dir:TDirList;

i:integer;

begin

// As usual we create our objects...

Cli:=TSFTPClient.Create;

Dir:=TDirList.Create;

try

// We set the initial connection parameters...

Cli.ServerAddr:='localhost';

Cli.Username:='test';

Cli.Password:='test';

// We call the "Open" method to connect and authenticate.

ifCli.Open then

begin

// Now these are the 4 lines of code that perform the

// magic, we get the list of the remote directory into

// our "Dir" object, and then we loop over it: for every

// element we check if the date is greater than yesterdat

// (now-1), and only if it is, then we download such element.

Cli.ListDir('/documents',Dir);

fori:=0toDir.Count-1do

if(Dir[i].Time&gt;(now-1))then

Cli.Download(Dir[i].FullName,'d:\',false,feaOverwrite);

// We are done. We can disconnect.

Cli.Close;

end;

finally

// To avoid wasting memory, we free the objects we created.

Dir.Free;

Cli.Free;

end;

end.

Without all the comment lines, you can appreciate how short and easy-to-understand this script is: