News, Product Information, and Tips
Check out our free utlities in the downloads section

Remote Administration



Remote Administration For Windows. Easy remote access of Windows 7, XP, 2008, 2000, and Vista Computers

Click here to find out more

Create Outlook Profiles



No more setup wizards in Outlook. Setup Outlook Profiles automatically from the command line

Click here to find out more

Network Administrator



Reboot Hundreds of computers, disable flash drives, deploy power managements settings.

Click here to get your free copy of Network Administrator. Over 25 plugins to make your life easier

USB Disabler



Disable Flash Drives for specific users, or only allow specific drives.

Click here for your free trial

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.

Like this article? Then sign up for my newsletter to get free tips and software sent right to your inbox once a week. Like you, I hate spam – I will never spam, or sell your email address.

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