hi there i need to collect the 6 latest backup files from a folder every day and currently working on a script to do this for me so far i have managed to pull the 6 latest files as shown in code below but the problem i have is most of the dates are the same i need to get 6 different dates but the latest ones,
sorry about how i have write this, hope it makes sense to someone.

# sorts files by modified date,
# pulls 6 most recent files,
# and delete all others.
#created by dan holding v0.1 (03 Aug 2010)
import os, glob, time, sets
root = '.'
date_file_list = []
date_file_set = []
for folder in glob.glob(root):
    print "folder =", folder
    for file in glob.glob(folder + '\*.txt*'):
# /\ select the type of file, for instance *.bat or all files *.*
        stats = os.stat(file)
        lastmod_seconds = stats[8]
        # /\ use seconds first to sort the time
        date_file_tuple = lastmod_seconds, file
        date_file_list.append(date_file_tuple)
        # \/ create a set to eliminate duplicate files
        # with matching seconds and path/name
        date_file_set = set(date_file_list)
        # \/ convert set back to a list
        date_file_list = list(date_file_set)
        # \/ latest modified date (in seconds) now first
        date_file_list.sort(reverse = True)
#\/ test
print (len(date_file_list))
print '/\ number of files in list '*1,'-'*50
print (len(date_file_set))
print '/\ number of files in set'*1,'-'*52
# show six most recent files
for lastmod_seconds, path in date_file_list[:6]:
    folder, file_name = os.path.split(path)
    # convert seconds to time tuple
    lastmod_tuple = time.localtime(lastmod_seconds)
    # convert time tuple to MM/DD/YYYY HH:MM:SS format
    file_date = time.strftime("%m/%d/%y %H:%M:%S", lastmod_tuple)
    print "%-40s %s" % (file_name, file_date)

cheers and hope to here back from someone soon

Recommended Answers

All 2 Replies

How about:

# sorts files by modified date,
# pulls 6 most recent files,
# and delete all others.
#created by dan holding v0.1 (03 Aug 2010)
import os, glob, time, sets
root = '.'
date_file_list = []
date_file_set = []
for folder in glob.glob(root):
    print "folder =", folder
    for file in glob.glob(folder + '\*.txt*'):
# /\ select the type of file, for instance *.bat or all files *.*
        stats = os.stat(file)
        lastmod_seconds = stats[8]
        # /\ use seconds first to sort the time
        date_file_tuple = lastmod_seconds, file
        date_file_list.append(date_file_tuple)
        # \/ create a set to eliminate duplicate files
        # with matching seconds and path/name
        date_file_set = set(date_file_list)
        # \/ convert set back to a list
        date_file_list = list(date_file_set)
        # \/ latest modified date (in seconds) now first
        date_file_list.sort(reverse = True)
#\/ test
print (len(date_file_list))
print '/\ number of files in list '*1,'-'*50
print (len(date_file_set))
print '/\ number of files in set'*1,'-'*52
# show six most recent files
date, count = '', 0
for lastmod_seconds, path in date_file_list:
    folder, file_name = os.path.split(path)
    # convert seconds to time tuple
    lastmod_tuple = time.localtime(lastmod_seconds)
    # convert time tuple to MM/DD/YYYY HH:MM:SS format
    file_date = time.strftime("%m/%d/%y", lastmod_tuple)
    if file_date != date:
        date=file_date
        count += 1
    else:
        continue
    file_time = time.strftime("%H:%M:%S", lastmod_tuple)
    print "%-40s %10s %10s" % (file_name, file_date, file_time)
    if count >5: break

that was great worked like a treat thanks so much tony

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.