EnterpriseDT Blogs

Advanced FTP Scripting

FtpScript is a command-line tool that allows users to execute FTP scripts that employ VB or C# syntax. A secure FTP (FTPS/SFTP) version is available upon request.

The executable may be downloaded fromĀ here. It requires .NET framework 2.0 or better. A list of commands may be found here.

Scripts are invoked in the same way as any other console command:

ftpscript getfile.vb

The following simple VBScript-based script connects to a website and lists the files in the root directory:

Connect "ftp.gnu.org", "anonymous", "test@test.com"
Print GetFiles

or to change into a particular directory and download a file:

Connect "ftp.gnu.org", "anonymous", "test@test.com"
ChangeWorkingDirectory "/gnu/bison"
DownloadFile "bison-2.0.tar.gz"

Since the tool supports real programming languages it’s possible to use advanced constructs such as conditional statements and loops. For example, the following example connects to the same website but then downloads the first file (as opposed to directory) that it finds:

Connect "ftp.gnu.org", "anonymous", "test@test.com"
For Each file in GetFilesInfos
  If Not file.Dir Then
    DownloadFile file.Name
    Exit For
  End If
Next

If the user prefers C# syntax then the above script would look like this:

Connect("ftp.gnu.org", "anonymous", "test@test.com");
foreach (FTPFile file in GetFilesInfos())
  if (!file.Dir)
  {
    DownloadFile(file.Name);
    break;
  }

FtpScript also supports command-line arguments so that variables may be set directly from the command-line as follows:

ftpscript getfile.vb ftp.gnu.org anonymous test@test.com

They are used within the scripts as follows:

Connect $1 $2 $3
For Each file in GetFilesInfos
  If file.Dir Then
    DownloadFile file.Name
    Exit For
  End If
Next

$1 gets the first value after the script-file name, $2 the second, and so on.

Comments are closed.