FTP scripting with Command FTP

FTP/SFTP scripting with DriveHQ Command FTP


1. Introduction
2. DriveHQ Command FTP Syntax and Parameters
3. Creating FTP Script Files
3.1 An Example of FTP Script File
3.2 FTP Script File with Parameters
4. Automate File Transfer with Windows Task Scheduler
5. Automatic Folder Synchronization using DriveHQ Command FTP
6. FTP Command References
6.1 Standard FTP Commands
6.2 DriveHQ Advanced FTP Commands

1. Introduction

DriveHQ Command FTP is a powerful command-line FTP & SFTP client designed to replace the Windows FTP client. It supports FTP scripting, and is compatible with any standard FTP/SFTP servers.

FTP/SFTP is extremely popular in transferring large files/folders over the Internet. Command-line FTP client is widely used in automated file transferring with FTP scripting. Many people use the built-in command-line FTP client on Windows. However, the built-in FTP client has a few major disadvantages:

  • 1. It only supports active mode FTP, which is often blocked by routers, firewalls or security software.
  • 2. It only supports plain FTP without data encryption.
  • 3. It does not support automatic resuming/retrying.

DriveHQ Command FTP is designed to replace the default Windows FTP client. It has the following advantages:

  • 1. It supports both active and passive mode FTP.
  • 2. It supports FTP over SSL (FTPS & FTPES) and SFTP.
  • 3. It supports automatic resuming/retrying.
  • 4. It supports data compression and encryption.
  • 5. It supports downloading/uploading folders.
  • 6. It supports mirroring/synchronizing folders.
  • 7. It supports event logging for auditing and trouble shooting.
  • 8. It can send email reports after an operation.
  • 9. It supports more powerful FTP scripts.

In many cases, DriveHQ Command FTP can directly replace the Windows FTP client. They share similar syntax and command-line parameters. However, DriveHQ Command FTP offers a lot more features. It can handle complicated file transferring tasks with just one command. With its support for SFTP, You can write an FTP script file, but run it with an SFTP server.

2. DriveHQ Command FTP Syntax and Parameters

Syntax


cmdftp FTP_SERVER [-u:USERNAME] [-p:PASSWORD] [-i] [-A] [-k:"PRIVATE_KEY_FILE"] [-s:"FTP_SCRIPT_FILE"] [-d:"LOG_FILE"] [-m:"SOURCE_FTP_SERVER", "DESTINATION_FTP_SERVER"] [CMDOPTIONS]
cmdftp [-s:"FTP_SCRIPT_FILE"] [-d:"logfile"]

Parameters

Parameter Description
FTP_SERVER The FTP/SFTP server name or IP address such as ftp.drivehq.com, or ftp://ftp.drivehq.com or ftps://ftp.drivehq.com. It is required unless the parameters include -s:FTP_SCRIPT_FILE
-u:USERNAME Specifies the FTP/SFTP account username.
-p:PASSWORD Specifies FTP/SFTP account password.
-k:"PRIVATE_KEY_FILE" Optional. Specifies the SFTP private key file path.
-s:"FTP_SCRIPT_FILE" Optional. Specifies the FTP script file path. If this parameter is provided, then other parameters are ignored, except the "-d:LOG_FILE" option.

Command Options

/xo[:time] Exclude old files
/xn[:time] Exclude newer files
/xf:filenames Exclude files, files are split by comma
/xd:foldernames Exclude folders, folders are split by comma

Logging options

-d:"LogFileName" Specifies the log file path. If using "-d" without a LogFileName, then it will display the log in the console window.

3. Creating FTP Script Files

FTP scripts are very popular among IT administrators and system integrators. They are widely used in automated data backup and file transferring. FTP scripts enable you to create a text file that contains a list of FTP commands and inputs. You can then run these commands sequentially in one execution. Because a script file can contain many FTP commands, it can accomplish a very sophisticated file transfer task.

FTP scripting with the default Windows FTP client is very limited: It only supports plain FTP, which is not secure; it only supports Active Model FTP, which is often blocked by routers/firewalls. DriveHQ Command FTP supports most features of Windows FTP client; in addition, it supports many features that are not available in Windows FTP client. DriveHQ Command FTP is compatible with all standard FTP servers.

To write an FTP script file, you can run the FTP commands in interactive mode first. Open a command window, type in the following commands as shown in the screenshot below:

These commands usually include: Connect to an FTP server, login to the server, list files/folders in a directory, change current directory, upload/download/delete files, and so on. If you only occasionally use FTP, then the above method works just fine. However, if you need to repeatedly transfer/backup/delete files, e.g. as part of a scheduled backup task, or EDI (Electronic Data Interchange) with another company, you can easily write a script file that can be re-used again and again. Coupled with the Task Scheduler supported by the operating system, you can automate the business process.

An Example of FTP Script File
An FTP script file has the same commands/inputs as used in an interactive FTP session. Basically, you just need to create a text file in a text editor (e.g. NotePad), and type in the commands and inputs as you would do in the interactive mode. DriveHQ's Command FTP has very similar interface as the Windows FTP client. The following commands can be run in both the Windows FTP client and DriveHQ's Command FTP clients. To use Command FTP, please type in cmdftp instead of ftp. e.g.:

C:\>ftp
ftp> open ftp.drivehq.com
Connected to ftp.drivehq.com.
220 Welcome to the most popular FTP hosting service! Save on hardware, software,
 hosting and admin. Share files/folders with read-write permission. Visit http:/
/www.drivehq.com/ftp/;
User (ftp.drivehq.com:(none)): dhqdemo
331 User name ok, need password.
Password: xxxxxxxxx
230 User dhqdemo logged on.
ftp> cd test
250 CWD command successful. "/test" is current directory.
ftp> delete orders.xml.bak
550 File not found.
ftp> rename orders.xml orders.xml.bak
350 File exists, ready for destination name.
250 File "/test/orders.xml" renamed successfully.
ftp> put c:\test\orders.xml
200 Port command successful.
150 Opening BINARY mode data connection for file transfer.
226 Transfer complete
ftp: 41 bytes sent in 0.06Seconds 0.65Kbytes/sec.
ftp> quit
221 Bye

C:\>


As you can see, these commands will:
- Log in to an FTP server: ftp.drivehq.com;
- Enter the username and password;
- Change to a local directory "c:\test";
- Change to an FTP directory "/test";
- Delete the file "Orders.xml.bak" (if exists) on the FTP server;
- Rename the file "Orders.xml" (if exists) to "Orders.xml.bak" on the FTP server;
- Upload the local file "Orders.xml" to the FTP server;
- Exit

You can run them in a command window to debug the code before you save them into a script file. The content of the script will be as follows:

open ftp.drivehq.com
USERNAME
xxxxxxxxx
cd test
delete orders.xml.bak
rename orders.xml orders.xml.bak
put c:\test\orders.xml
quit
The above FTP script can be executed daily to automatically transfer new orders info to the FTP server. It will upload the latest "Orders.xml" to the FTP server; before it uploads the file, it will rename (backup) the previous file.
To run the FTP script, you just execute the FTP command with the -s option. With Windows FTP client, you can use:
	ftp -s:ftpscript.txt 

With DriveHQ command FTP client, you can use
	cmdftp -s:ftpscript.txt 

FTP Scripts with Parameters
The above FTP script is static. It can be run repeatedly, but it is not very flexible. For example, if you need to transfer data for multiple clients, you probably want to use different folders. To re-use the same FTP script, it needs to support additional parameters. There are two solutions:
(1) Use a batch script file to dynamically create the FTP script file. Windows batch scripts can take command-line parameters. If you use Windows FTP client, this is the only solution.
(2) DriveHQ Command FTP supports additional parameters.
With the first method, you need to create a batch file, e.g. test.bat. Save the file in the folder C:\test. You can run the batch file by type in:

	c:\test\test.bat PARAM1 PARAM2 ...

The test.bat file will be like:

@echo off
echo open ftp.drivehq.com> ftpscript.txt
echo USERNAME>> ftpscript.txt
echo PASSWORD>> ftpscript.txt
echo cd %1>>  ftpscript.txt
echo delete %2.bak>>  ftpscript.txt
echo rename %2 %2.bak>> ftpscript.txt
echo put c:\%1\%2>> ftpscript.txt
echo quit>> ftpscript.txt

This way, you can pass the arguments to the batch file to the FTP script file. The same batch script can be re-used for different companies, e.g.:
    c:\test\test.bat c:\OrdersOfCompanyA ordersOfCompanyA.xml
    c:\test\test.bat c:\OrdersOfCompanyB ordersOfCompanyB.xml
With DriveHQ Command FTP, you can run an FTP script with additional arguments. This way, you don't need to create a batch script file. You can simply run the FTP script with the following command:
cmdftp -s:ftpscript.txt argument1 argument2 argument3 ...
The FTP script file needs to be modified slightly as follows:
# %1 is like "test", %2 is like "orders.xml"
# This file is saved as c:\temp\ftpscript2.txt
# to run it, use:
# cmdftp -s:c:\temp\ftpscript2.txt -%1:test -%2:orders.xml

open ftp.drivehq.com
USERNAME
PASSWORD
cd /%1
delete /%1/%2.bak
rename /%1/%2 /%1/%2.bak
put c:\%1\%2
quit
In the above script file, the folder name is replaced with %1, and the file name is replaced with %2. To run the FTP script, you must supply these parameters as command-line arguments.
cmdftp -s:c:\temp\ftpscript2.txt -%1:test -%2:orders.xml
When DriveHQ Command FTP executes the script file, it will replace %1 with "test" and %2 with "orders.xml".

4. Automate file transfer with Windows Task Scheduler

Whether you use a batch file, or directly use DriveHQ Command FTP, you can schedule the file transfer in Windows Task Scheduler. You can schedule the task to run daily, weekly or monthly. In the Advanced Settings, you can even set the task to run every few hours or minutes. you can set the time of the day to start the task. To launch Task Scheduler, type "Task scheduler" in the search box, see the screenshot below:

Launch Windows Task Scheduler

Click Task Scheduler, it will launch Windows Task Scheduler.

Click create basic task in Windows Task Scheduler

To create a task, click "Create Basic Task".

Create basic scheduled task

Enter the task name and description, then click Next.

Select task trigger / schedule

Select your task schedule. You can choose daily, weekly and monthly. If you want to run the task hourly, you can do so in the Advanced Settings later. Click Next to set the Action.

Scheduled Task - Action - select a program

In the Action page, select "Start a program", then click Next.

Scheduled Task - Action - Select DriveHQ Command FTP

Browse to select DriveHQ Command FTP (cmdftp.exe) as the program. Add the command-line parameters in the "Add Arguments" text box. The "Start in" is optional. You can set it to the same folder as the EXE file. Click Next to finish creating the task.

To access the Advanced Settings, click "Task Schedule Library", you can see a list of scheduled tasks. Double click on the task "Upload Orders", you can edit the task settings.

Scheduled Task - Advanced Settings - Edit Task Properties

Note by default, the task will run only when the user is logged on. You can change the setting here to run the task even when the user is not logged on.

5. Automatic Folder Synchronization using DriveHQ Command FTP

DriveHQ Command FTP supports folder mirroring. Synchronizing a local folder with an FTP folder is extremely easy. Just use the following command:

	cmdftp FTP_SERVER -u:username -p:password -m:"LOCAL_FOLDER|FTP_FOLDER"

Using Windows Task Scheduler, you can run this command to synchronize the two folders periodically.

6. FTP Command References

After you log on to an FTP server, you can send FTP commands to the FTP server. Windows FTP client supports the following commands. Please note these commands are not the same as FTP commands defined in the FTP Protocol. Each FTP Protocol Command usually only handles one simple issue. An FTP command in Windows FTP client may use multiple FTP Protocol Commands.

DriveHQ Command FTP is similar to the Windows FTP client. It supports similar commands and syntax. In addition, it also supports a few DriveHQ-specific advanced FTP commands, e.g. commands to copy, move, upload, download or delete folders.

6.1 Windows FTP Client Commands

Windows FTP client (ftp.exe) supports the following FTP commands:
FTP Commands Description
! This command toggles between the local operating system and the FTP server. When you log on to an FTP server, it defaults to the FTP server. If you want to run a command locally, you can usually use !CMD_NAME, e.g. !dir, !mkdir, etc.
? Displays the Help information.
append Append to a file.
ascii Set ASCII transfer mode.
bell Turn on "Beep when command completed".
binary Set binary transfer mode.
bye Disconnect the FTP session and exit.
cd Change directory.
close Terminate the FTP session.
delete Deletes a file.
debug Toggle debugging mode on or off.
dir Lists files and sub-folders in a directory.
disconnect Disconnect the FTP session.
get Get file from the remote FTP server.
glob Set globbing on or off. When turned off, wildcard characters in file names (in the put and get commands) are taken literally.
hash Toggle printing "#" for each buffer transferred.
help Display the Help information; usually use help COMMAND_NAME.
lcd Display the current local working directory if typed alone; otherwise, change the local working directory.
literal Send arbitrary command to the connected FTP server with an expected one-line response.
ls List files in the FTP folder.
mdelete Delete multiple files on the FTP server.
mdir Lists files/subfolders of multiple FTP directories.
mget Download multiple files from the FTP server to your local working folder.
mkdir Create a new directory.
mls List contents of multiple FTP directories.
mput Upload multiple local files to the FTP server.
open Connect to an FTP server.
prompt Enables or disables interactive prompting on multiple commands.
put Upload one file to the FTP server.
pwd Print working directory on the FTP server.
quit Disconnect from FTP server and exit.
quote Same as the literal command (Send arbitrary ftp command).
recv Download / receive a file from the FTP server.
remotehelp Get help from remote server.
rename Rename a file on the FTP server.
rmdir Remove a directory on the FTP server.
send Send one file.
status Shows status of current options.
trace Toggle packet tracing.
Type Set file transfer type.
user Send new user information (username).
verbose Set verbose on or off.

6.2 DriveHQ Advanced FTP Commands

DriveHQ Command FTP supports a few more advanced FTP commands:

FTP Commands Description
download Download a folder or file.
Syntax: Download [FTP-FILEorFOLDER-PATH] [LOCAL-FILEorFOLDER-PATH]
upload Upload a folder or file.
Syntax: Upload [LOCAL-FILEorFOLDER-PATH] [FTP-FILEorFOLDER-PATH]
mirror Mirror a local folder and an FTP folder.
Syntax: MIRROR [LOCAL-FOLDER-PATH] [FTP-FOLDER-PATH]
deleteoldfiles In the specified folder, delete files older than x days.
Syntax: RMDIR [FTP-PATH] /xo:[time]