Sample scripts are provided as-is with no warranty of fitness for a particular purpose. These scripts are solely intended to demonstrate techniques for accomplishing common tasks. Additional script logic and error-handling may need to be added to achieve the desired results in your specific environment.
download_new_files.s
This script uses FTPSNAPSHOT, FTPDIFF, and FTPGETDIFF to check a remote site for files which have changed since the last time you checked.
1 * Log onto the FTP site.
2 FTPLOGON 'ftp.robo-ftp.com' /user=anonymous
3
4 * Change the current directory on the server
5 FTPCD 'outgoing'
6
7 * Check to see if there is already a pre-existing SNAPSHOT database
8 * If so, we can skip the "do_snapshot" routine, which creates that database
9 IFFILE "ftpsnapshot_test.sql" GOTO do_diff
10
11 :do_snapshot
12 * Take a snapshot of the current directory
13 * This isn't necessary with every run
14 FTPSNAPSHOT "" "ftpsnapshot_test.sql"
15
16 * Check to see if there was an error in creating the snapshot.
17 IFERROR GOTO error_in_snapshot
18
19 :do_diff
20 * Check the site for changes.
21 FTPDIFF "*" "ftpsnapshot_test.sql"
22
23 * Check for an error in the DIFF command
24 IFERROR GOTO error_in_diff
25
26 * If there are differences, then download files
27 IFNUM> %ftpdifffiles 00 GOTO do_getdiff
28
29 * Otherwise, finish the script
30 GOTO done
31
32 :do_getdiff
33 * First, do a rewind to make sure you are looking at the first difference
34 FTPDIFFREWIND
35
36 :download_files
37 * Check the next difference
38 FTPGETDIFF "ftpsnapshot_test.sql"
39
40 * An EOF error indicates that all files have been downloaded; exit the loop
41 IFERROR= $ERROR_READ_EOF GOTO download_complete
42
43 * Any other error indicates that something went wrong when checking the
44 * database, so we need to check for that error
45 IFERROR GOTO error_in_getdiff
46
47 * if the difference is $DIFF_FILE_NOT_FOUND, do not delete the file, skip this
48 * file and go back to the top of the loop
49 IFNUM= %ftpdifffileid 5001 GOTO download_files
50
51 * the other difference types indicate that the site file is new or has changed,
52 * so let's download it
53 RCVFILE %ftpdifffilename
54
55 * check for an error when downloading
56 IFERROR GOTO error_in_download
57
58 * move on to the next difference
59 GOTO download_files
60
61 :download_complete
62 * do another diff, to see if anything changed while we were downloading files
63 * in most cases, there will be no more differences, and the do_diff procedure
64 * will simply exit to the "done" label.
65 GOTO do_diff
66
67 :error_in_download
68 * You would normally place any error handling in this section. For the sake
69 * of brevity, we have omitted any specific error handling.
70 * You script should respond to download errors. Download errors are typically
71 * caused by networking issues, or file read/write permissions
72 * Common error-handling includes:
73 * logging off the server and logging back on, then trying to download again
74 * writing a test file to the database location, to check for file read/write errors
75 * reporting the error to an administrator, usually via email
76 * logging off the server and ending script execution
77
78 :error_in_snapshot
79 * You would normally place any error handling in this section. For the sake
80 * of brevity, we have omitted any specific error handling.
81 * You script should respond to FTPSNAPSHOT errors. Since FTPSNAPSHOT is creating
82 * a database of information from the server, errors can come either
83 * from an error in getting server information or in writing that information to disk.
84
85 :error_in_diff
86 * You would normally place any error handling in this section. For the sake
87 * of brevity, we have omitted any specific error handling.
88 * You script should respond to FTPDIFF errors. Since FTPDIFF is comparing a
89 * database of information to the current conditions in the server, the most
90 * frequent cause for an error hear is either a problem in the database file,
91 * or a problem getting information to the server.
92
93 :error_in_getdiff
94 * You would normally place any error handling in this section. For the sake
95 * of brevity, we have omitted any specific error handling.
96 * You script should respond to FTPGETDIFF errors. FTPGETDIFF errors indicate
97 * a problem retrieving information from the FTPDIFF database
98
99 :done
100 FTPLOGOFF
101 * Bye

