The Problem: I need to FTP files to a remote location, check for success and log the connection. I was a little lazy and didn't care to learn a new scripting language, much less to install any additional software on my system. Well the only thing additional is blat but it was already installed and it just emails you on failures or successes so it is not necessary.
My Solution: Using the built in FTP, DOS batch, and blat the script looks like this...
The Code:
@rem -----------------------------------------------------
@rem -- Created to send FTP files that end in .im
@rem -- to remote site via FTP
@rem -- created 4.8.2006 www.francoconnection.com
@rem -----------------------------------------------------
@rem -----------------------------------------------------
@rem -- Set your variables
@rem -- WorkDir = location of script and where log files will be
@rem -- FTPF = the input file for FTP.exe
@rem -- FTPL = log file for troubleshooting
@rem -- PickupDir = directory where the Files are FTPed from
@rem -- FTPSite = URL of the remote server
@rem -- USR = User name or Anonymous
@rem -- PWD = user password or email address if anonymous
@rem -----------------------------------------------------
setlocal
set WorkDir="c:\Scripts"
set FTPF=ftp.in
set FTPL=ftp.log
set PickupDir="c:\Scripts\outgoing\"
set RemoteDir=directory123set FTPSite="ftp.domain.com"
set USR=myusername
set PWD=mypassword
@rem -----------------------------------------------------
@rem -- move to the working directory
@rem -- where the script and log files are located
@rem -----------------------------------------------------
pushd %WorkDir%
@rem -----------------------------------------------------
@rem -- create the FTP input file
@rem -- while overwriting the previous one.
@rem -- Notice the *.* it is the file filter
@rem -- change if necessary (example... *.txt or *.jpg)
@rem -----------------------------------------------------
echo user %USR% %PWD% > %FTPF%
echo type binary >> %FTPF%
for /R %PickupDir% %%i in (*.*) do (echo put "%%~pnxi" "%RemoteDir%%%~nxi" >> %FTPF%)
echo bye >> %FTPF%
@rem -----------------------------------------------------
@rem -- FTP the files
@rem -- and send output to a log file
@rem -----------------------------------------------------
%systemroot%\system32\ftp -n -s:%FTPF% %FTPSite% > %FTPL%
@rem ------------------------------------------------------
@rem -- check log for success "226 Transfer complete."
@rem -- if ANY 226 is found in the log file it will be
@rem -- considered a success otherwise it is considered failed.
@rem ------------------------------------------------------
for /F %%i in (%FTPL%) do if %%i==226 (goto success)
@rem ------------------------------------------------------
@rem -- use blat to send email w/ attached log file on failure
@rem ------------------------------------------------------
:fail
blat .\%FTPL% -t youremail@domain.com -s "FTP FAILURE - File Transfer Failed"
goto endfile
@rem ------------------------------------------------------
@rem -- use blat to send email w/ attached log file on success
@rem ------------------------------------------------------
:success
@rem blat .\%FTPL% -t youremail@domain.com -s "FTP SUCCESS - File Transfer Successful"
@rem ------------------------------------------------------
@rem -- end
@rem ------------------------------------------------------
:endfile
popd
endlocal
Downloads: Blat is a free Windows program and is used to command line SMTP.
Further Reading: Rob van der Woude's Scripting Pages
Copyright: FrancoFTP open source software distributed under the terms of the GPL.