FTP batch File

ThomDroz
ThomDroz used Ask the Experts™
on
Hi
I have a windows 2003 Server
I want to create a .bat file that i will schedule to run nightly.
The file should FTP all files in "photo" directory that were created today.
they should go to my my web site in a folder there called "photosremote"

If there is an error on the process I should receive an email at "photoerror@aol.com"

I do NOT want the files to be synced.  If the file was created in the current day I want it FTP'd regardless of what else is in either directory.

Is this something that can be scripted in a .bat file for windows 2003 server?

Thanks for any help!




Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:

Author

Commented:
billprew: & HainKurt:

Thanks, I have done many searchs, that is why I am asking here, none of your links provides the answer to my question as I am looking for the actual script that will perform as I described.

Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

HainKurtSr. System Analyst

Commented:
there wont be any "actual" script unless you spend some time to customize the script you find on internet ;)
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
The basic script to do the FTP will look something like this:

@echo off
(echo open ftp.server.com
echo user username
echo password
echo cd photosremote
echo lcd photos
echo bin
echo prompt
echo mput *
echo quit
) | ftp -n -i > ftp.log

error trapping using the Windows native ftp utility is very difficult.  Do you have to use that, or can you use a third party FTP tool like NCFTPPUT (free).

Once you decide if you have an error or not (likely by seearching the error log from the FTP command with FIND or FINDSTR, then you can use something like BLAT or SENDMAIL or BMAIL to send out an email, perhaps attaching the ftp log file, maybe something like:

blat ftp.log -server smtp.server.com -to photoerror@aol.com -f photoerror@aol.com -subject "FTP Errors"

~bp
Commented:
billprew:

Thanks for the help.

The main item i am looking for is how to only send the files that are dated today....

I have seen many scripts that upload files by name, by type, by folder etc.

I am looking for script steps that will only send files in a folder saved today.

Maybe that is not possible with the ftp that comes with windows server 2003.  
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
Well, it is possible.  You could look for the files in the photos folder that were created today and add a PUT line to the FTP commands for each file added today.

Would it be okay to use the last modified date rather than the created date, that is a bit easier to get access to in a FOR loop in a BAT file?  It's not impossible to look at the created date, just more work in the script.

~bp

Author

Commented:
Billprew

Last modified date is actually what I want, so any script step that would do that would be perfect!
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
Okay, if you do the following command on the server involved, what does it display (date formats can vary from one windows system to another).

ECHO %DATE%

~bp
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
Well, here's a starting place, this will likely need some work, but it is a start.  It assumes it is run from the parent of the photos directory currently.

Take a look and play around with it.  I'd probably start by adding an EXIT /B right before the FTP command, so that you can see what the CTL file looks like at that point before even trying the FTP part.

I also couldn't easily test this here, so there may be some syntax issues to be worked out.  Hopefully most of it makes sense though.

~bp
@echo off
setlocal EnableDelayedExpansion
 
REM Grab the current date and save it for later
Today=%DATE:~-10%
 
REM Delete any FTP control file from a prior run
if exist ftp.ctl del ftp.ctl
 
REM change to the photos folder
pushd photos
 
REM Build the first part of the FTP control file with connect info, etc
echo open ftp.server.com>>ftp.ctl
echo user username>>ftp.ctl
echo password>>ftp.ctl
echo cd photosremote>>ftp.ctl
echo bin>>ftp.ctl
echo prompt>>ftp.ctl
 
REM Look at all files in the current directory, if they were dated today the add them to the FTP transfer list
for %%A in (*.*) do (
  for /F %%B in ("%%~tA") do (
    if "%%B" EQU "%Today%" (
      echo mput %%A>>ftp.ctl
    )
  )
)
 
REM Add wrapup commands to FTP control file
echo quit>>ftp.ctl
 
REM Execute the FTP transfer, commands from the control file built above
ftp -n -i -s:ftp.ctl >ftp.log
 
REM Check the FTP log file for any error conditions
find /i "error" ftp.log>NUL
if %ERRORLEVEL% EQU 0 (
  REM If errors found then email the log file
  blat ftp.log -server smtp.server.com -to photoerror@aol.com -f photoerror@aol.com -subject "FTP Errors"
)
 
REM Restore the current directory
popd

Open in new window

Author

Commented:
Bill
thanks
I will give it a work out tomorrow and let you know!

Author

Commented:
Bill
I am new at script writing, I can not get you script to work at all as a bat file.  I appreciate the help none the less

Author

Commented:
Bill
I am new at scripting and could not get your file to work as a .bat file.
I am starting from scratch on my own on this
I appreciate your help

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial