Subscribe

Get the Network Administrators tool pack

Subscribe to our newsletter and get 11 free network administrator tools, plus a 30 page user guide so you can get the most out of them.

Click Here to get your free tools

Recent Posts

Search

Archives

Receive an email notification for your FTP Script

by Steve Wiseman on May 7, 2010 · 1 comment

in BAT Files,Tips,Tools,Utility,Windows


.

Last week I wrote how to FTP the contents of a folder by just using a batch file. We had a few good suggestions on how to improve that script.

Listening to those suggestions, I have come up with this updated version:

@echo off
rem ===============================
rem = FTP Script Made by =
rem = http://www.intelliadmin.com =
rem ===============================

REM = Set the name of our temp script =

set SCRIPT_NAME=FTP_SCRIPT.FTP

REM = Generate our FTP Script
echo open [host]> %SCRIPT_NAME%
echo [username]>> %SCRIPT_NAME%
echo [password]>> %SCRIPT_NAME%
echo bin>> %SCRIPT_NAME%
echo hash>> %SCRIPT_NAME%

REM = Loop through each file that matches our wildcard

for %%f in (*.log) do @echo put %%f>> %SCRIPT_NAME%

echo quit>> %SCRIPT_NAME%

REM = Now call FTP with our generated script
ftp -s:%SCRIPT_NAME%

REM = Delete our temp script file and we are done
del %SCRIPT_NAME% /q
@echo on

An additional note: If your password, or username contain special characters (%, &, etc) you can escape them by putting a ^ before them. If you forget this, your username or password will not be properly echoed and you will get a login failure

If your password is: &1994ABCD

You would write: ^&1994ABCD

(Thanks Mike for emailing me this tip!)

Now, on to the email notifications. My favorite tool of choice for this is blat. It is free, easy to use, and will even cook you dinner. You can find it here:

http://www.blat.net/

To see how to use it, visit an older article of mine:

Send Email from the command line

One disappointment I had was that the FTP.exe program does not set %ERRORLEVEL% on failure. I was hoping that I could get it to send email only if it failed. The compromise is an email each time it runs. To see if it failed, you look at the email message and see the FTP output.

First, we will modify our script to redirect the ftp output to a text file. This can be done by changing the FTP.exe call in the batch file to this:

ftp -s:%SCRIPT_NAME% >> status.log

Then, at the end of the script we can call blat.exe, and send out our text file. Here is an example of how I would send it to support@intelliadmin.com:

blat.exe -f support@intelliadmin.com -to support@intelliadmin.com -server my.smtp.server -subject "FTP Script Results" -bodyF status.log

Each time the script runs, I will get the FTP output sent to me.

Here is the finished script:


@echo off
rem ===============================
rem = FTP Script Made by =
rem = http://www.intelliadmin.com =
rem ===============================

REM = Set the name of our temp script =

set SCRIPT_NAME=FTP_SCRIPT.FTP

REM = Generate our FTP Script
echo open [host]> %SCRIPT_NAME%
echo [username]>> %SCRIPT_NAME%
echo [password]>> %SCRIPT_NAME%
echo bin>> %SCRIPT_NAME%
echo hash>> %SCRIPT_NAME%

REM = Loop through each file that matches our wildcard

for %%f in (*.log) do @echo put %%f>> %SCRIPT_NAME%

echo quit>> %SCRIPT_NAME%

REM = Now call FTP with our generated script
ftp -s:%SCRIPT_NAME% >> status.log

REM = Delete our temp script file and we are done
del %SCRIPT_NAME% /q
blat.exe -f [from_email] -to [to_email] -server [your smtp server] -subject "FTP Script Results" -bodyF status.log
del status.log
@echo on

One last note – if anyone knows of a way to detect that FTP.exe encountered an error, please let me know. It would be great if you could get it to email only if it failed, or create different subject lines to get your attention when it fails.

One more thing…Subscribe to my newsletter and get 11 free network administrator tools, plus a 30 page user guide so you can get the most out of them. Click Here to get your free tools

Related Articles:

{ 1 comment… read it below or add one }

1 Scott Leseman June 17, 2010 at 2:29 pm

I worked on this some time ago so I can’t remember exacly how the 2nd batch file works. We call the first batch file from the 2nd. The 2nd captures the output of the first and looks for 226 Transfer complete. If that is found then we say there was no error. Now I’m trying to figure out how to do this with powershell.

–DATA.BAT–
http://ftp.exe -i -n -s:”DATA.FTP”

–FTP_DATA.BAT–
DATA.BAT | find /i “226 Transfer complete” /c > FTP_226.TXT
set /p COUNT_226=< FTP_226.TXT
IF %COUNT_226%==1 EXIT 0
IF %COUNT_226% NEQ 1 EXIT 1

Leave a Comment

Category Links - Windows Forum - Exchange Forum