Script

FTP

The professional tool to automate FTP, SFTP, FTPS and Batch FTP

The FTP protocol is not capable of moving files on the FTP server. It was merely designed with file transfer in mind, but there is a simple trick that most FTP servers accept for moving a file: renaming. Let us look ats an example:

OPENHOST("ftp.myserver.com","carl","123456")
RENAMEFILE("a.txt","destination_folder/a.txt")
CLOSEHOST

Note that this trick does not work for every FTP server. It usually works for Unix/Linux FTP servers only. If your server does not support it you can also do it downloading the file, removing it from its original remote directory and upload it again to its destination directory. It’s a very slow way to move a remote file but at least works in every FTP server:

# Connect to FTP server
OPENHOST("ftp.myserver.com","carl","123456")
 
# Change current local directory to c:\mytempdir
LOCALCHDIR("c:\mytempdir")
 
# Download the file filetobemoved.zip from
# the remote folder /origin_remote_dir
GETFILE("/origin_remote_dir/filetobemoved.zip")
 
# Change the current remote directory to
# /destination_remote_dir/
CHDIR("/destination_remote_dir/")
 
# Upload the file filetobemoved.zip
# to its destination
PUTFILE("filetobemoved.zip")
 
# Delete the original remote file
DELETEFILE("/origin_remote_dir/filetobemoved.zip")
 
# Close the connection
CLOSEHOST

In the above script it is advisable to add some error handling. We don’t want to delete the file from the original location if the upload or any other step failed:

# Connect to FTP server
$result=OPENHOST("ftp.myserver.com","carl","123456")
 
# Stop the script execution if not connected
IF($result!="OK")
   STOP
END IF
 
# Change current local directory to c:\mytempdir
$result=LOCALCHDIR("c:\mytempdir")
 
# Stop the script execution if could not
# change the current local directory
IF($result!="OK")
   STOP
END IF
 
# Download the file filetobemoved.zip from
# the remote folder /origin_remote_dir
$result=GETFILE("/origin_remote_dir/filetobemoved.zip")
 
# Stop the script execution if could not
# download the file
IF($result!="OK")
   STOP
END IF
 
# Change the current remote directory to
# /destination_remote_dir/
$result=CHDIR("/destination_remote_dir/")
 
# Stop the script execution if could not
# change the current remote directory
IF($result!="OK")
   STOP
END IF
 
# Upload the file filetobemoved.zip
# to its destination directory
$result=PUTFILE("filetobemoved.zip")
 
# Stop the script execution if could not
# upload the file to the destination directory
IF($result!="OK")
   STOP
END IF
 
# Delete the remote file
$result=DELETEFILE("/origin_remote_dir/filetobemoved.zip")
 
# Stop the script execution if could not
# delete the remote file
IF($result!="OK")
   STOP
END IF
 
# Close the connection
CLOSEHOST