Skip to main content

Umsortieren

Submitted by Erik Wegner on

Die Fotos in einem Verzeichnis werden ausgewertet und in Unterverzeichnisse verschoben.

sort.py
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
83
84
85
86
87
88
#!/usr/bin/env python3
 
 
# Einstellungen
 
# Umgebung ist "linux" oder "windows"
umgebung = "linux"
#umgebung = "windows"
 
# Soll die Jahreszahl im Verzeichnis mit verwendet werden?
erzeuge_jahresverzeichnis = True
 
# Einstellungen je nach Umgebung
 
if umgebung == "linux":
    befehl_mkdir = "mkdir -p"
    befehl_move = "mv"
if umgebung == "window":
    befehl_mkdir = "md"
    befehl_move = "move"
 
# Programm
 
import re
import os
 
unproc = []
directories = []
 
regex1 = re.compile("^\d{4}-\d{2}-\d{2}-.*\.jpg$")
 
 
def procfilename(name):
    "Zerlege den Dateinamen in Datumsbestandteile"
    p = ""
    if name.startswith('IMG_') or name.startswith('VID_'):
        p = name[4:12]
        y = name[4:8]
        m = name[8:10]
        d = name[10:12]
        #print (name + ' => ' + y + '/' + m + '/' + d)
    if name.startswith('Lumia 1020_'):
        p = name[11:19]
        y = name[11:15]
        m = name[15:17]
        d = name[17:19]
        #print (name + ' => ' + y + '/' + m + '/' + d)
    if name.startswith('WP_2') and name.find("HDR") == -1:
        p = name[3:11]
        y = name[3:7]
        m = name[7:9]
        d = name[9:11]
        #print (name + ' => ' + y + '/' + m + '/' + d)
    if name.startswith('Office Lens'):
        p = name[12:20]
        y = name[12:16]
        m = name[16:18]
        d = name[18:20]
        #print (name + ' => ' + y + '/' + m + '/' + d)
    if regex1.match(name):
        p = name[0:10]
        y = name[0:4]
        m = name[5:7]
        d = name[8:10]
    if p == "":
        unproc.append(name)
        return
    if erzeuge_jahresverzeichnis:
        directory = os.path.join(y, m, d)
    else:
        directory = os.path.join(m, d)
    if directory not in directories:
        print (befehl_mkdir + " " + directory)
        directories.append(directory)
    print (befehl_move + ' "' + name + '" ' + directory)
 
def readfile(fname):
    with open(fname) as f:
        for line in f:
            procfilename(line.strip())
    print(repr(unproc))
 
if __name__ == "__main__":
    #readfile('filelist.txt')
    basepath = "."
    for file in os.listdir(basepath):
        if file.endswith(".jpg") or file.endswith(".mp4"):
            procfilename(file)