<< back to sysax.com Product page

Chapter 6. Recursive File Transfers

A recursive list of files can be obtained using the ftpgetlist command by setting the recurse level to 0. The names of files within a subfolder are stored in the .name property as subfolder/filename.txt for a remote listing or subfolder\filename.txt for a local listing. A foreach statement can then be used to select each file in the list and perform the file transfer using the ftpupload or ftpdownload commands. The following examples will upload or download files recursively by creating subfolders as needed.

Exhibit 6.1. Example for recursively uploading files that match the pattern *.txt

ftpsetpath local, "c:\\ftpout";
ftpgetlist local, @mylocallist, 0;
foreach $ftpitem in @mylocallist begin
  if $ftpitem.name eq "*.txt" begin
    ftpupload file, $ftpitem.name;
  end
end       

Exhibit 6.2. Example for recursively downloading files that match the pattern *.txt

ftpsetpath remote, "/ftpacc";
ftpgetlist remote, @myremotelist, 0;
foreach $ftpitem in @myremotelist begin
  if $ftpitem.name eq "*.txt" begin
    ftpdownload file, $ftpitem.name;
  end
end       

If all the files in a folder tree need to be uploaded or downloaded to a single destination folder, the / or \ characters in the .name property that denote a path can be replaced by an underscore or another character using the regexreplace command. The following example replaces the / character with a - character and causes files from an entire folder tree in the remote folder to be saved the current local folder as subfolder-filename.txt.

Exhibit 6.3. Example for recursively downloading files from a remote folder tree into a single local folder

ftpsetpath remote, "/ftpacc";
ftpgetlist remote, @myremotelist, 0;
foreach $ftpitem in @myremotelist begin
  if $ftpitem.name eq "*.txt" begin
    strprint ~myfile, $ftpitem.name;
    regexreplace ~myfile, "/", "-", "ig";  
    ftpdownload file, ~myfile;
  end
end