Python Forum
How to create def for sorted() from list of versioning files (filename+datetime)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to create def for sorted() from list of versioning files (filename+datetime)
#1
Hi everyone.

I am running FreeFileSync app on PC clients to sync/backup Users folders to network file server, with versioning turned on.

System is Windows 7 64-bit with Python 3.5.

Versioning does following: If synced file is new, then old file is moved into special folder, and then new file is synced/copied. Problem arises because FreeFileSync does not have option to limit the number of versions kept, so with large files (2GB+) that are changed daily like Outlook.pst, Thunderbird msf files, etc, HDD is filled in just few days/weeks.

So I decided to create a Python script that will go through Versioning locations/folders, separate/filter versions of each file, and delete any but just 2 last files. Just deleting anything older then (current date - x days) is not viable because if some file has not changed in that time, all versions (but current backup) will be deleted.

Now we come to the point where I am stuck. I have following format of file names in folders I need to parse:

Quote:archive.pst 2016-10-14 080101.pst
archive.pst 2016-10-15 080101.pst
archive.pst 2016-10-17 080101.pst
archive.pst 2016-10-18 080101.pst
archive.pst 2016-10-19 080101.pst
archive.pst 2016-10-20 080101.pst

Outlook.pst 2016-10-14 080101.pst
Outlook.pst 2016-10-15 080101.pst
Outlook.pst 2016-10-17 080101.pst
Outlook.pst 2016-10-18 080101.pst
Outlook.pst 2016-10-19 080101.pst
Outlook.pst 2016-10-20 080101.pst

Outlook.sharing.xml.obi 2016-10-14 080101.obi
Outlook.sharing.xml.obi 2016-10-15 080101.obi
Outlook.sharing.xml.obi 2016-10-17 080101.obi
Outlook.sharing.xml.obi 2016-10-18 080101.obi
Outlook.sharing.xml.obi 2016-10-19 080101.obi
Outlook.sharing.xml.obi 2016-10-20 080101.obi

Notice that versioning file name has original filename with ext, then space, then YYYY-MM-DD date, space, then HHmmss and then .ext of original file.

What I need to do is to create a "key" definition for sorted() to find and separate all files from separate original files (like archive.pst, Outlook.pst, Outlook.sharing.xml.obi)

I think best option to separate/recognize them is to recognize ext of the file from end of the file (.pst, .obi) and to locate that substring in the rest of the file. Then, using sublist of files belonging to original file, to sort them by datetime (newest first?) from filename (date of the file might be different then in filename if something goes wrong!), eliminate those two (first?) files from the list (copy the rest into new resulting list?) so I can delete all extra files (on that resulting? list) in next step, leaving only two latest files.

Problem is that I am new with Python and all of this is WAY above my understanding, and HDD is already pretty full so I am deleting huge files manually.

Code to replicate is like this:
def ljfilter(a):
# def code here
   return

MyDir = 'c:/test'
os.chdir(MyDir)

lista = [f for f in listdir(MyDir) if isfile(join(MyDir, f))]
for item in sorted(lista, key=ljfilter):
   print(item)
Reply


Messages In This Thread
How to create def for sorted() from list of versioning files (filename+datetime) - by DrLove73 - Jan-11-2017, 02:10 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Delete strings from a list to create a new only number list Dvdscot 8 1,676 May-01-2023, 09:06 PM
Last Post: deanhystad
  list the files using query in python arjunaram 0 708 Mar-28-2023, 02:39 PM
Last Post: arjunaram
  Create new folders and copy files cocobolli 3 1,578 Mar-22-2023, 10:23 AM
Last Post: Gribouillis
  Failing to print sorted files tester_V 4 1,353 Nov-12-2022, 06:49 PM
Last Post: tester_V
  [split] why can't i create a list of numbers (ints) with random.randrange() astral_travel 7 1,628 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
  How to download a list of files from FTP? schnarkle 0 1,043 Jun-21-2022, 10:35 PM
Last Post: schnarkle
  Rename part of filename in multiple files atomxkai 7 7,509 Feb-18-2022, 10:03 PM
Last Post: atomxkai
  Python code to read second line from CSV files and create a master CSV file sh1704 1 2,463 Feb-13-2022, 07:13 PM
Last Post: menator01
  Append files and add column with last part of each filename NiKirk 0 2,670 Feb-04-2022, 07:35 AM
Last Post: NiKirk
  set and sorted, not working how expected! wtr 2 1,329 Jan-07-2022, 04:53 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020