0

Structure: 20170410.1207.te <- Date (2017 04 10 , 12:07)

There is a company folder that contains several folders. All folders with the above structure which are older than 30 days should be moved to the folder _History (basically archiving them), but at least 5 should be left no matter which timestamp.

As a time value, the string must be taken from the folder name to be converted as a date and compared to today's date minus 30 days.

I also want to create a log file that logs when which folders were moved at what location.

The Code below just shows me the filename, can somebody help me please?

import os
import shutil

for subdir, dirs, files in os.walk("C:\Python-Script\Spielwiese"):
    for file in files:
        print(os.path.join(file))

shutil.move("C:\Python-Script\Spielwiese\", "C:\Python-Script\Spielwiese2")
YungCarti
  • 1
  • 1
  • So, first thing: How do you want to determine the age of the files? Windows' creation date? An answer on how to do this can be found here: https://stackoverflow.com/questions/237079/how-to-get-file-creation-modification-date-times-in-python – Dschoni Mar 27 '19 at 08:35
  • Yes with the Windows Creation Date. – YungCarti Mar 27 '19 at 08:36
  • @YungCarti Please comment in English only as this comment will only provide additional information/clarification for individuals able to read/understand German language.. – iLuvLogix Mar 27 '19 at 08:47
  • @iLuvLogix **20170410.1207.te** There is a company folder that contains several folders. All folders with the above structure should be moved to the folder _History, which are older than 30 days but at least 5 should be left no matter which timestamp. As a time value, the string must be taken from the folder name to be converted as a date and compared to today's date - 30 days. Also create a log file that writes what and where has been delete – YungCarti Mar 27 '19 at 09:04

1 Answers1

0

The following code will return a list of all files in a given timeframe, sorted by create time on windows. Depending on how you want to filter, I can give you more information. You can than work on the resulting list. One more thing is, that you should use pathlib for windows filepaths, to not run into problems with german paths and unicode escapes in your pathname.

import os
import shutil

found_files = []

for subdir, dirs, files in os.walk("C:\Python-Script\Spielwiese"):
    for file in files:
        name = os.path.join(file)
        create_date = os.path.getctime(file)
        if create_date > some_time: # Put the timeframe here
            found_files.append((name, create_date))
found_files.sort(key=lambda tup: tup[1]) # Sort the files according to creation time
Dschoni
  • 3,714
  • 6
  • 45
  • 80