Question : get just todays date files

i have got this python script : see below: not designed by me.

our system is changing and now  i need it to get just today's files (date created)...
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
import os
from ftplib import FTP
import subprocess
from datetime import datetime
 
# config
ftp_uname = '?????????????????'
ftp_pwd = '?????????????'
ftp_server = '111.111.111.111'
# end of config
 
fileroot = 'DBBackup_'
os.chdir('c:/')
files = []	
 
logfile = open('e:/dbbackup/getmdbackups.log','a+')
 
def makelist(line):
    if (line.startswith(fileroot)):
        fs = [line,0]	
        files.append(fs)
 
def log(line):
    ll = str(datetime.now()) + ' : ' + str(line)
    print ll
    logfile.write(ll + '\n')
 
def fileexists(ff, size):
    if (os.path.exists(ff)):
        stat = os.stat(ff)
        if (stat.st_size == size):
            return True
    return False
 
 
try:
    # first connect using ftp to get a list of valid backup failes available
    log('Connecting to ftp server')
    ftp = FTP(ftp_server)
    ftp.set_pasv(False)
    #ftp.set_debuglevel(2)
    resp = ftp.login(ftp_uname,ftp_pwd)
    log(resp)
    ftp.retrlines('NLST',makelist)
    log(str(files))
    ftp.quit()
    
    # fetch files in a loop using wget.
    for ff in files:
        ftp = FTP(ftp_server)
        ftp.set_pasv(False)
        resp = ftp.login(ftp_uname,ftp_pwd)
        log(resp)
        size = ftp.size(ff[0])
        log('Size of server file = ' + str(size))
        ftp.quit()
        try:    
            if (not fileexists(ff[0],size)):
                log('Transferring: ' + ff[0])
                # make parameters to wget the backup file
                params = ' http://' + ftp_server + '/?????????/' + ff[0]
                rcode = subprocess.call('e:/wget/wget.exe ' + params)
                log('Return code from wget = ' + str(rcode))
                if (rcode == 0):
                    ff[1] = 1
            else:
                log('File ' + ff[0] + ' already exists locally, not transferring')
                ff[1] = 1
 
        except Exception, e:
          log(str(e))
	    
    log('Transfer complete')
    # delete the server files that have been transferred or are already here with the right filesize.
    for ff in files:
        if (ff[1] == 1):
            log('delete ' + ff[0])
except Exception,e:
    log(str(e))
# clean up temp files
log('Finished.')
logfile.close()
Open in New Window Select All

Answer : get just todays date files

here you go, this script will check the ending of the file and if the end date equals todays date, then it gets download else not

also still check to make sure that the file starts with DBBackup_
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
import os
from ftplib import FTP
import subprocess
from datetime import datetime
from datetime import time
import string
 
# config
ftp_uname = ''
ftp_pwd = ''
ftp_server = ''
# end of config
 
fileroot = 'DBBackup_'
os.chdir('c:/')
files = []
curr_date = datetime.now().strftime('%Y%m%d')
file_end = '_' + curr_date + '00.7z'
print file_end
 
def makelist(line):
    if (line.startswith(fileroot)):
        fs = [line,0]	
        files.append(fs)
 
def fileexists(ff, size):
    if (os.path.exists(ff)):
        stat = os.stat(ff)
        if (stat.st_size == size):
            return True
    return False
 
 
try:
    # first connect using ftp to get a list of valid backup failes available
    ftp = FTP(ftp_server)
    ftp.set_pasv(False)
    #ftp.set_debuglevel(2)
    resp = ftp.login(ftp_uname,ftp_pwd)
    ftp.retrlines('NLST',makelist)
    ftp.quit()
    
    # fetch files in a loop using wget.
    for ff in files:
        ftp = FTP(ftp_server)
        ftp.set_pasv(False)
        resp = ftp.login(ftp_uname,ftp_pwd)
        size = ftp.size(ff[0])
        ftp.quit()
        try:    
            if (not fileexists(ff[0],size)):
                if ff[0].endswith( file_end ):
                    # make parameters to wget the backup file
                    params = ' ftp://' + ftp_server + '/' + ff[0]
                    rcode = subprocess.call('e:/wget/wget.exe ' + params)
                    if (rcode == 0):
                        ff[1] = 1
                else:
                    print "File was not created today:" + ff[0]
            else:
                ff[1] = 1
 
        except Exception, e:
           print (str(e))
except Exception,e:
     print (str(e))
Open in New Window Select All
Random Solutions  
 
programming4us programming4us