FTPShell Script: automate FTP, SFTP and FTPS transfers
FTPShell Script is the scripting language built into FTPShell Client Enterprise. It is for transfers that have to run without anyone watching: poll a server and fetch what is new, push an export to a list of partners, mirror a folder every night, verify by checksum, encrypt with PGP, and tell someone how it went. Scripts run from the FTPShell editor, as scheduled tasks, from the command line, or through the ftpshellc console client inside batch files and other schedulers.
What a script looks like
Statements end with a semicolon, blocks use begin ... end, # starts a comment, strings are double-quoted and Windows paths work as written. Variables start with a tilde (~text), lists with @, and $item names the current element inside a foreach, with .name, .pathname, .size, .modtime and .isfolder available on it. Tools > FTP scripts creates a .fscr file with a commented header and opens it in the editor; Compile checks the syntax and reports the line of any error; Run executes it in the current session. A sample checkftp.fscr, which polls a server, downloads *.txt, deletes on success and repeats every five minutes, is installed as a template.
Real logic, not just a command list
if/else with text, numeric and wildcard comparisons; and, or, not; loop N; while; foreach; exitloop; endscript; waitsecs. retry count, delay repeats any command with each attempt logged; onerror begin ... end runs once for any failure with the failing command and message available; ftpresult, fileresult, mailresult, syncresult, backupresult, compareresult and pgpresult report the last outcome of each group, so a script can branch on what actually happened.
Command groups
Connect. ftpconnect, ftpconnectssh, ftpconnectssl, ftpconnectssli and ftpconnectsslc (encrypted login, clear data channel), with an optional ACCT; pkeyload for SSH private keys with passphrase; certload for FTPS client certificates; proxyhttp; enablepasv and disablepasv; setnlst; settransfertype; settimeskew; seterrorlimit.
Transfer. ftpupload and ftpdownload for files, folders (recursive) or wildcards, with optional rename; ftpuploadv and ftpdownloadv verify by CRC, MD5 or SHA-1; setduperules for skip, overwrite, resume or rename; ftpdelete, ftpmakefolder, ftprename, ftpmodtime, ftpsize, ftpcustomcmd; ftpcopylocal and ftpmovelocal; ftpcompress and ftpuncompress.
Sync and backup. setsource and setdestination with include/exclude and preserve/remove patterns; syncrun and backuprun compare by size, date, newer date, larger size, CRC, MD5 or SHA-1, with verified variants; comparerun fills source-only, destination-only, different and identical lists without transferring anything.
Lists, strings and timestamps. ftpgetlist with a depth, listsort, listappend with filters, listcompare, ftpsavelist, and foreach over any list; stringjoin, strupper, strlower, strslice, regexreplace, ftpregexmatch, ftpglobmatch and ftpwildcardmatch; gettimestamp with increments by second through year; formattimestamp and formatfilename with %YYYY% %MM% %DD% %hh% %mm% %ss% %NAME% %BASE% %EXT%.
Environment and programs. getenvvar, getcompname, getusername and readwinregistry; ftprunprogram runs any program, optionally as another user with a timeout and expected exit code, and captures its output; fileopen, filereadline and filewriteline for control files and reports; print and messagebox.
Email reports. mailcreate, mailaddline, mailaddfile, mailsend, mailsendssl and mailsendtls send status reports with logs attached over plain, SSL or STARTTLS SMTP.
Credentials and PGP. setprotectedvar stores a value protected with ftpshellc -protectstring and decrypts it only when used as a login, password, key path or passphrase; pgpencrypt, pgpdecrypt, pgpsign and pgpsignsrc with ASCII armor and key import and export.
Running scripts
From the GUI: Run in the editor executes the script in the session it was opened from; Schedule script runs it at a date and time, once or repeating. From the command line: ftpshell.exe -script job.fscr runs minimized and exits when done; -logfile, -tslogfile and -append keep a rolling log; -set NAME=VALUE passes parameters that scripts read with getenvvar; exit code 1 signals a bad script or log path. From batch files: ftpshellc.exe -script job.fscr lists parse errors before anything runs and returns 0 on success, 1 for license, file or parse errors, otherwise the script's own exit code.
Examples and the manual
- FTPShell Script manual: every statement and command, with examples.
- Poll a server for new files: check a remote folder every five minutes, download new files and delete them on the server after a successful transfer.
- Upload one file to many servers: connect to a list of servers in turn and upload the same file to each.
- Timestamped compressed backup: zip files and folders, name the archive by date and time, and upload it to a backup server.
We also write complex scripts as a service; contact
with what the job has to do.
Questions people ask
Do I need the Enterprise edition to run FTPShell scripts?
Yes. Writing, compiling, running and scheduling scripts, command-line script execution and the ftpshellc console are Enterprise features; the 30-day trial includes them.
Can a script log in to SFTP with a private key?
Yes. pkeyload loads a passphrase-protected PEM key (RSA, DSA or ECDSA) before ftpconnectssh; certload does the same with a PEM, DER or PFX client certificate for FTPS; proxyhttp tunnels SFTP through an authenticated HTTP CONNECT proxy.
Can a script send an email when a transfer fails?
Yes. Put the transfer inside retry and onerror blocks, and in onerror use mailcreate, mailaddline and mailaddfile to build a report with the log attached, then mailsend, mailsendssl or mailsendtls.
Can I pass parameters to a script?
Yes. Start it with ftpshell.exe -script job.fscr -set NAME=VALUE and read the value with getenvvar inside the script, so one script serves several servers or folders.
Is there a script that replaces ftp.exe -s:file?
The ftpshellc console runs plain command lists with -s:file exactly like ftp -s, in FTP, SFTP or FTPS mode, so existing batch files move over unchanged and gain encryption.
