Home arrow Scripting arrow Batch Scripts arrow FTP a file with logging and notification
FTP a file with logging and notification Print E-mail
Written by Jason   
Dec 05, 2008 at 08:25 AM

I recently needed a script that would FTP a file from a Windows server on a set schedule, log the results of the transfer, retry a few times in the event of a failure, and report any failures immediately. Here it is.

The name of this script if FTPER.BAT. This script makes use of the following executables (not counting standard shell commands in Windows): sleep.exe bmail.exe

It also requires a file that contains the actual FTP commands, named ftper.ftp in this file. An example of this would be:

OPEN myftpserver.org
myusername
mypassword
binary
MPUT "C:\Program Files\Some Directory\file.xml"
BYE

Any valid FTP commands will work in this file, such as lcd, ls, etc.

And now for the script:

@echo off
cls
 
echo.   
echo .......................................................................
echo .......................................................................
echo 
echo ''  ftper.bat - ftp a file and email the results
echo ''  File: ftper.bat
echo ''  Usage: run as either a scheduled task or from the command line
echo ''  Requirements: ftper.ftp
echo ''  Version 0.4 12/5/08
echo ''  By Jason Crum ( )
echo.    
echo .......................................................................
echo .......................................................................
echo. 
echo.  
echo.  
echo.  
 
set COUNT=0
 
FOR /f "tokens=2 delims=/.: " %%D IN ("%DATE%") DO SET MTH=%%D
FOR /f "tokens=3 delims=/.: " %%D IN ("%DATE%") DO SET DAY=%%D
FOR /f "tokens=4 delims=/.: " %%D IN ("%DATE%") DO SET YR=%%D
 
echo ........................................................... >> %CD%\logs\%MTH%_%DAY%_%YR%.log
echo.  >> %CD%\logs\%MTH%_%DAY%_%YR%.log
echo.  >> %CD%\logs\%MTH%_%DAY%_%YR%.log
 
echo Current date:
date /T
echo %TIME%
echo Current date:  >> %CD%\logs\%MTH%_%DAY%_%YR%.log
date /T >> %CD%\logs\%MTH%_%DAY%_%YR%.log
echo %TIME% >> %CD%\logs\%MTH%_%DAY%_%YR%.log
echo.  >> %CD%\logs\%MTH%_%DAY%_%YR%.log
echo. 
 
:GOFTP
 
set /a COUNT=COUNT+1
 
ftp -i -s:ftper.ftp > %CD%\logs\lastlog.log
type %CD%\logs\lastlog.log
type %CD%\logs\lastlog.log >> %CD%\logs\%MTH%_%DAY%_%YR%.log
echo.  >> %CD%\logs\%MTH%_%DAY%_%YR%.log
 
type %CD%\logs\lastlog.log | findstr "Not connected." >NUL
 
echo DEBUG: The current ERRORLEVEL is: %ERRORLEVEL%
echo DEBUG: COUNT is: %COUNT%
 
IF %ERRORLEVEL%==0 GOTO :FAILURE
IF %ERRORLEVEL%==1 GOTO :SUCCESS
 
:SUCCESS
 
echo.  
 
echo %TIME% %DATE% - FTP copy successful. 
echo %TIME% %DATE% - FTP copy successful. >> %CD%\logs\%MTH%_%DAY%_%YR%.log
 
bmail -s [SMTP_SERVER] -t [RECEIVING_EMAIL_ADDRESS] -f [SENDING_EMAIL_ADDRESS] -a "FTP Copy: Succeeded, %COUNT% tries" -m %CD%\logs\lastlog.log
 
SET MAILER=1
IF %MAILER%==1 GOTO :DONEMAIL
 
:FAILURE
 
sleep 5
 
IF %COUNT% LSS 10 GOTO :GOFTP
 
echo.  
 
echo %TIME% %DATE% - FTP copy failed.
echo %TIME% %DATE% - FTP copy failed. >> %CD%\logs\%MTH%_%DAY%_%YR%.log
echo This log is found at: %CD%\logs\%MTH%_%DAY%_%YR%.log
echo This log is found at: %CD%\logs\%MTH%_%DAY%_%YR%.log >> %CD%\logs\%MTH%_%DAY%_%YR%.log
 
bmail -s [SMTP_SERVER] -t [RECEIVING_EMAIL_ADDRESS] -f [SENDING_EMAIL_ADDRESS] -a "FTP Copy: Failed, %COUNT% tries" -m %CD%\logs\lastlog.log

 
SET MAILER=2
IF %MAILER%==2 GOTO :DONEMAIL
 
:DONEMAIL
 
Last Updated ( Dec 05, 2008 at 10:32 AM )
� http://www.roboguys.com, Mambo and Designed by Siteground