Copy latest file from ftp site every day

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
kobys
Posts: 2
Joined: 13 Sep 2010 15:06

Copy latest file from ftp site every day

#1 Post by kobys » 13 Sep 2010 15:22

I'm trying to write a batch file that goes to an ftp site, gets the last updated file and copies it to a directory on my network every day. Is there an easy way to do this. The last file is always the last business day if there is no way to specify the last updated file in a generic fashion.

Thanks for any help!

aGerman
Expert
Posts: 4705
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Copy latest file from ftp site every day

#2 Post by aGerman » 13 Sep 2010 15:42

I'm not familar with ftp because I never used it by myself, but here on DosTips you'll find some example scripts.
Wat about http://www.dostips.com/DtTipsFtpBatchScript.php#Batch.FtpBatchDownloadOnlyNewFiles

Regards
aGerman

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Copy latest file from ftp site every day

#3 Post by ghostmachine4 » 13 Sep 2010 19:53

Use a programming language with FTP support to do file transfer. eg in Python

Code: Select all

import ftplib
server="localhost"
user="anonymous"
password="test@email.com"
filelist=[]
def download(ftp, filename):
    ftp.retrbinary("RETR " + filename, open(filename,"wb").write)
try:
   ftp = ftplib.FTP()
   ftp.debug(3)
   ftp.connect(server,21)
   ftp.login(user,password)
except Exception,e:
    print e
else:
    ftp.cwd("directory to change if any")
    ftp.retrlines('LIST',filelist.append)
    for latest in filelist:
        if latest.startswith("-") and "latest file pattern if any" in latest:
            print latest
            download(ftp,latest)
    ftp.quit()
sys.exit()



usage:

Code: Select all

c:\test> python myftp.py

kobys
Posts: 2
Joined: 13 Sep 2010 15:06

Re: Copy latest file from ftp site every day

#4 Post by kobys » 14 Sep 2010 07:41

aGerman,

Thanks so much! This worked perfectly!

Cheers

Post Reply