How to issue raw commands to an FTP server

Introduction

When dealing with FTP servers, It is often a requirement to need to perform extra tasks like creating directories and changing file permissions.

This lesson shows how you can do this by sending raw FTP commands to a server.

Lay out the Stack

Create a new stack then drag a button and a field onto it.

Set the name of this field to "Output".

Save this stack.

Create the Script

Edit the script of the button that you placed on the stack by right clicking on the button and selecting "edit script".

Add the following code.

on mouseUp
   -- Use a publicly available FTP server for testing
   put "ftp://speedtest.tele2.net" into tFtpServer
   
   -- Create the list of commands we want to send to the FTP server (one per line) and
   -- store it in a variable for use in the tsNetSendCmdSync command
   -- NOTE:  These must be RAW FTP commands
   put "CWD /upload" & cr into tFtpCmd
   put "PWD" & cr after tFtpCmd
   
   -- The tSettings array can be used to provide additional configuration details, in this case
   -- the username and password to use when connecting to the FTP server
   put "anonymous" into tSettings["username"]
   put "test" into tSettings["password"]
   
   -- Issue the commands
   put tsNetSendCmdSync(tFtpServer, tFtpCmd, tResult, tBytes, tSettings) into tData
   
   -- tResult will start with "tsneterr:" if the tsNetSendCmdSync call was unsuccessful
   if the first word of tResult is "tsneterr:" then
      answer "Error sending FTP commands" && tResult
   else 
      put tData into field "Output"
   end if 
   
end mouseUp

Test

Switch to Run mode and click the button.

More information

Please note that each command must be on a separate line when sending FTP commands to a server using the tsNetSendCmd* functions,

You can use tsNetSendCmd* functions with SFTP as well.  However, the commands sent to the server would be different to those used with FTP.

0 Comments

Add your comment

E-Mail me when someone replies to this comment