top of page
Search
worldiweststinlicp

Batch file for FTP: Best practices and common errors to avoid



Windows FTP scripts enable you to put together a chain of commands in a file that you can call into play when needed. Scripts can come in handy when you want to automate file transfer processes. In this introductory post, we explain what FTP scripts are, why you'll want to use them, and how to create simple Windows-based scripts to upload or download files from a FTP server.


In a previous article, we taught you how to execute FTP commands in the terminal. The examples we included in that tutorial were all done in interactive mode. Meaning, they all required you to enter commands into the command line each time you wanted to do something during an FTP session, e.g. login to a server, display a directory listing, upload files, download files, change a directory, and so on.




Batch file for FTP



Interactive mode is sufficient for ad hoc purposes. But if you need to transfer files on a regular basis, e.g. as part of a B2B transaction or data exchange, then interactive mode is no longer practical. You'll be much more efficient if you can automate some parts of the process, and the way to that would be to write FTP scripts.


Notice how we've simply entered the same commands you'd normally use in interactive mode. You can use your favorite text editor to create the script and save it in a text file, e.g. ftpscript.txt. To run the script, you just execute the FTP command with the -s option. For example,


So how different is this from interactive mode? Well, the BIG difference is that, you no longer have to input the same values and FTP upload commands over and over every time you need to upload the same file to the same FTP server (there are certainly sophisticated scripts that can do more than that).


Most B2B file transfers are quite repetitive. Practically the same file transfers are carried out periodically. Why assign someone (who can probably do more productive tasks) to do that repetitive task when you can just call a script?


If the batch file (e.g. fileupload.bat) is located in a directory whose path is included in your PATH environment variable, then you'll be able to run that batch file as a command prompt executable file from any directory.


Notice that this batch file accepts an argument (%1) and passes the value to the script. Here's how it looked like when I executed the batch file to "download" the file named samplefile.txt. samplefile.txt is the argument we passed to the batch file, which in turn passed it on to the FTP script.


If you want to run this batch file (and consequently, the FTP script) on a pre-defined schedule, you'll have to add the batch file in your Windows Task Scheduler. That's a separate configuration altogether and is beyond the scope of this blog post.


Using the Windows FTP client you would want to use the -s:filename option to specify a script for the FTP client to run. The documentation specifically points out that you should not try to pipe input into the FTP client with a


Each line of a batch file will get executed; but only after the previous line has completed. In your case, as soon as it hits the ftp line the ftp program will start and take over user input. When it is closed then the remaining lines will execute. Meaning the username/password are never sent to the FTP program and instead will be fed to the command prompt itself once the ftp program is closed.


I used basic PowerShell commands. I did not need to download any additional components. I first checked if the requisite number of files existed. If they I invoked the FTP the second time with an Mget.I run this from a Windows Server 2008 connecting to a Windows XP remote server.


This is example is a batch script running as administrator. It creates a zip file using some date & time variables. Then it creates a ftp text file on the fly with some variables. Then it deletes the zip, folder and ftp text file.


You have to put the ftp commands to a separate file. You cannot put lines you otherwise type on terminal to .bat file and expect it to behave identically. The .bat file can include only Windows commands. When you run the ftp command from the batch file, it waits for its commands. It does not know about the .bat file, so it cannot read the commands from there.


The -s switch is the most valuable switch for batch files that take care of unattended downloads and uploads:FTP -s:ftpscript.txtOn some operating systems redirection may do the same:FTP


Sometimes it may be necessary to make the script completely unattended, without the user having to know the password, or even the user ID, but with the possibility to check for errors during transfer.There are several ways to do this.One is to redirect FTP's output to a log file and either display it to the user or use FIND to search the log file for any error messages.Another way to do this, on the fly, is by displaying FTP's output on screen, in the mean time using FIND /V to hide the output you do not want the user to see (like the password and maybe even the USER command):


To create a semi interactive FTP script, you may need to split it into several smaller parts, like an unattended FTP script to read a list of remote files, the output of which is redirected to a temporary file, which in turn is used by a batch file to create a new unattended FTP script on the fly to download and/or delete some of these files.


ScriptFTP is a tool to, you may have guessed, automate FTP file transfers.It supports plain FTP, FTPS and SFTP protocols.Commands to e-mail and/or log results are available.All commands can be run on the command line or from a script.


Use the /script command line option to pass the script to the WinSCP executable. Generally, you should also use /ini=nul switch to isolate the script execution from GUI configuration. You can embed the complete command line into a Windows batch file (.bat), like as follows:


Alternatively, you can generate new script file each time. To automate that, make a wrapper script file. For simple tasks you can use built-in Windows scripting functionality from batch file (.bat). For complex tasks, you will need to use some scripting language, such JScript or VBScript from Windows script host or PHP or Perl.


The "FTP -s:ftpscript.txt" option executes a FTP script wheres "%f0" resolved to the name of the running batch file."GOTO:EOF" ends the batch script and makes sure the FTP script doesn`t run as part of the batch.Good: You end up with only one file that contains the batch script and the FTP script combined.Minor flaw: The batch command in the first line causes an "Invalid command." error when executed in FTP context, however the FTP execution will continue.


If you frequently find yourself calling FTP from the command line, each time having to login and change directory and change FTP modes, until you finally get where you want be in order to do some real work then you may wish to get there with a singe click.This little batch can connect to your FTP server and logs you in before it gives you the prompt. You can easily add more FTP commands to it, like changing directories or switching to binary mode or whatever you like to be done before taking over control on the FTP prompt.The FTP connection information is embedded within the batch itself. The batch connects to an FTP server by executing itself in FTP context using the FTP -s option. Once executing in FTP context it executes all FTP commands listed in the file. By omitting the final FTP "bye" command it will stopat the FTP prompt and wait for user input.Optionally a FTP script can be provided as input stream, that way multiple FTP scripts can share the same login information. Example:FtpLogin.bat @ftp -i -s:"%f0"&GOTO:EOFInvalid command.ftp> open example.comConnected to example.com.220-220 FTP Server readyUser (example.com:(none)):331 Password required230 User logged inftp> pwd257 "/" is the current directoryftp> TOP2010-02-05 FTP - Download Only New Files - Ftp script to download only files that don`t exist in local folder, i.e. to avoid overwrite Description: This batch connects twice to the FTP server. First time it retrieves a list of fileson the FTP server. This list is being trimmed to contain only files that don`t already exist locally. The files in the trimmed list are then downloaded during a second connection.Note: Since all files are passed into the FTP`s MGET command there might be a limit to the number of files that can be processed at once. Script: Download: BatchFtpDownloadOnlyNewFiles.bat 1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.32.33.34.35.36.37.38.39.40.41.42.43.44.45.46.47.48.49.50.51.52.53.54.55.56.57.58.59.60.61.62.63.64.65.66.67.68.69.70. @Echo OffREM -- Define File Filter, i.e. files with extension .txtSet FindStrArgs=/E /C:".txt"REM -- Extract Ftp Script to create List of FilesSet "FtpCommand=ls"Call:extractFileSection "[Ftp Script 1]" "-">"%temp%\%n0.ftp"Rem Notepad "%temp%\%n0.ftp"REM -- Execute Ftp Script, collect File NamesSet "FileList="For /F "Delims=" %%A In ('"Ftp -v -i -s:"%temp%\%n0.ftp"Findstr %FindStrArgs%"') Do ( Call Set "FileList=%%FileList%% "%%A"")REM -- Extract Ftp Script to download files that don't exist in local folderSet "FtpCommand=mget"For %%A In (%FileList%) Do If Not Exist "%%A" Call Set "FtpCommand=%%FtpCommand%% "%%A""Call:extractFileSection "[Ftp Script 1]" "-">"%temp%\%n0.ftp"Rem Notepad "%temp%\%n0.ftp"For %%A In (%FtpCommand%) Do Echo.%%AREM -- Execute Ftp Script, download filesftp -i -s:"%temp%\%n0.ftp"Del "%temp%\%n0.ftp"GOTO:EOF:extractFileSection StartMark EndMark FileName -- extract a section of file that is defined by a start and end mark:: -- [IN] StartMark - start mark, use '...:S' mark to allow variable substitution:: -- [IN,OPT] EndMark - optional end mark, default is first empty line:: -- [IN,OPT] FileName - optional source file, default is THIS file:$created 20080219 :$changed 20100205 :$categories ReadFile:$source SETLOCAL Disabledelayedexpansionset "bmk=%1"set "emk=%2"set "src=%3"set "bExtr="set "bSubs="if "%src%"=="" set src=%f0& rem if no source file then assume THIS filefor /f "tokens=1,* delims=]" %%A in ('find /n /v "" "%src%"') do ( if /i "%%B"=="%emk%" set "bExtr="&set "bSubs=" if defined bExtr if defined bSubs (call echo.%%B) ELSE (echo.%%B) if /i "%%B"=="%bmk%" set "bExtr=Y" if /i "%%B"=="%bmk%:S" set "bExtr=Y"&set "bSubs=Y")EXIT /b[Ftp Script 1]:S!Title Connecting...open example.comusernamepassword!Title Preparing...cd public_html/MyRemoteDirectorylcd c:\MyLocalDirectorybinaryhash!Title Processing... %FtpCommand%%FtpCommand%!Title Disconnecting...disconnectbye TOP2009-12-06 FTP Scripts Sharing Login Info - Manage the FTP login separately from your FTP scripts Description: If you have multiple FTP scripts that all use the same login information to your FTP site then you may wish to manage the login information separately from your FTP scripts in a single place. That way if the username, password or hostname for the FTP connection changes you only need to edit a single place instead of having to edit all FTP scripts one by one.This automatic login script (also described in detail earlier) can be used to execute different FTP scriptsthat share the same login information stored within the batch file. Example: FtpLogin.bat script1.ftpNote: The FTP scripts passed into the batch must have the login sequence removed.Note: The FTP script executes even if the connection sequence fails potentially causing `Not connected` and other errors. This is no different from regularly executing FTP with -s option. Script: Download: FtpLoginSharing.bat 1.2.3.4. @type %1ftp -i -s:"%f0"&GOTO:EOFopen example.comusernamepassword TOP2010-02-05 FTP - Upload Only New Files - Ftp script to upload only files that don`t exist in remote folder, i.e. incremental upload Description: This batch connects twice to the FTP server. First time it retrieves a list of fileson the FTP server. Local files that are are not in this list will then be uploaded during a second connection.Note: Since all files are passed into the FTP`s MPUT command there might be a limit to the number of files that can be processed at once. Script: Download: BatchFtpUploadOnlyNewFiles.bat 1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.32.33.34.35.36.37.38.39.40.41.42.43.44.45.46.47.48.49.50.51.52.53.54.55.56.57.58.59.60.61.62.63.64.65.66.67.68.69.70.71.72.73.74. @Echo OffSetlocal EnabledelayedexpansionREM -- Define File Filter, i.e. files with extension .txtSet FindStrArgs=/E /C:".txt"REM -- Extract Ftp Script to create List of FilesSet "FtpCommand=ls"Call:extractFileSection "[Ftp Script 1]" "-">"%temp%\%n0.ftp"Rem Notepad "%temp%\%n0.ftp"REM -- Execute Ftp Script, collect File NamesSet "FileList="For /F "Delims=" %%A In ('"Ftp -v -i -s:"%temp%\%n0.ftp"Findstr %FindStrArgs%"') Do ( Call Set "FileList=%%FileList%% "%%A"")REM -- Extract Ftp Script to upload files that don't exist in remote folderSet "FtpCommand=mput"For %%A In (%FileList%) Do set "Exist["%%A"]=Y"For /F "Delims=" %%A In ('"dir /b "%localdir%"Findstr %FindStrArgs%"') Do ( If Not defined Exist["%%A"] Call Set "FtpCommand=%%FtpCommand%% "%%A"")Call:extractFileSection "[Ftp Script 1]" "-">"%temp%\%n0.ftp"rem Notepad "%temp%\%n0.ftp"For %%A In (%FtpCommand%) Do Echo.%%AREM -- Execute Ftp Script, download filesftp -i -s:"%temp%\%n0.ftp"Del "%temp%\%n0.ftp"GOTO:EOF:extractFileSection StartMark EndMark FileName -- extract a section of file that is defined by a start and end mark:: -- [IN] StartMark - start mark, use '...:S' mark to allow variable substitution:: -- [IN,OPT] EndMark - optional end mark, default is first empty line:: -- [IN,OPT] FileName - optional source file, default is THIS file:$created 20080219 :$changed 20100205 :$categories ReadFile:$source SETLOCAL Disabledelayedexpansionset "bmk=%1"set "emk=%2"set "src=%3"set "bExtr="set "bSubs="if "%src%"=="" set src=%f0& rem if no source file then assume THIS filefor /f "tokens=1,* delims=]" %%A in ('find /n /v "" "%src%"') do ( if /i "%%B"=="%emk%" set "bExtr="&set "bSubs=" if defined bExtr if defined bSubs (call echo.%%B) ELSE (echo.%%B) if /i "%%B"=="%bmk%" set "bExtr=Y" if /i "%%B"=="%bmk%:S" set "bExtr=Y"&set "bSubs=Y")EXIT /b[Ftp Script 1]:S!Title Connecting...open example.comusernamepassword!Title Preparing...cd public_html/MyRemoteDirectorylcd c:\MyLocalDirectorybinaryhash!Title Processing... %FtpCommand%%FtpCommand%!Title Disconnecting...disconnectbye TOP2008-10-17 FTP - Resolving Environment Variables - Creating FTP script on the fly at runtime and using variables within the FTP script Description: This batch executed the FTP script embedded within the batch. All variables in the FTP script willbe resolved.The FOR loop extracts the FTP script into a temporary file. It the ECHO command is being CALLed for each line in order to resolve the variables.Variables can be used within the FTP script the same way as in a batch script, including any string manipulation and command line arguments like %1 %2 %n0 %* and so on.All batch lines start with semicolon so that they will be ignored by the FOR loop. Semicolonis the default end-of-line (EOL) character used by the FOR command. Script: 1.2.3.4.5.6.7.8.9.10.11.12.13.14.15. ;@echo off;(for /f "usebackq delims=" %%A in ("%f0") do call echo.%%A)>"%temp%\%n0.ftp";ftp -i -s:"%temp%\%n0.ftp";GOTO:EOFopen example.comusernamepassword!:--- FTP commands below here ---cd public_html/%COMPUTERNAME%binaryhash on%*disconnectbye TOP2008-01-01 Classic FTP - Executing a FTP script Description: The FTP command support the "-s:ftpscript.txt" option. The FTP commands listed in ftpscript.txt will automatically run after FTP starts. The FTP command can be started from a batch file. 2ff7e9595c


0 views0 comments

Recent Posts

See All

Comments


bottom of page