aboutsummaryrefslogtreecommitdiffstats
path: root/min4.py
blob: f16218ca8c3cadb6e48ed1d29ad6028ce8b2093e (plain)
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
# min4.py
#
# put files older than 4 years in a separate folder
# erik liodden

import os
from subprocess import call
import glob
from datetime import datetime
from sys import argv

_OLD = datetime.now().year - 4

def organise_dir(directory):
    d = next(os.walk(directory))[1]
    candidates = []

    for sd in d:
        if sd != 'older' and int(sd[:-1]) < _OLD:
            candidates.append(sd)

    if candidates:
        call(['mkdir', '-p', directory + '/older'])
        for c in candidates:
            src = f"./{directory}/{c}/"
            dst = f"./{directory}/older/"
            call(['rsync', '-a', '--ignore-existing', src, dst])
            call(['rm', '-rf', src])

def main():
    try:
        path = argv[1]
    except:
        path = '.'

    if path[-1] != '/':
        path += '/'

    d_temp = next(os.walk(path))[1]
    d = []
    for item in d_temp:
        d.append(path + item)

    for sd in d:
        organise_dir(sd)
    return 0

if __name__ == '__main__':
    main()