-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautofileaway.py
64 lines (50 loc) · 1.53 KB
/
autofileaway.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
import os
import re
import csv
import shutil
import datetime
excludeFormat = ['.+\.part$','.+\.crdownload$']
rootDir = os.path.dirname(os.path.abspath(__file__))
def logDate():
return '['+str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))+'] '
log = open(rootDir+'/autofileaway.log', 'a')
with open(rootDir+'/autofileaway.rules.csv', 'rb') as csvfile:
spamreader = csv.reader(csvfile, delimiter=',', quotechar='"')
for rows in spamreader:
if len(rows) < 3:
continue
srcFolder = rows[0]
srcFormat = rows[1]
dstFolder = rows[2]
# go thru files
for f in os.listdir(srcFolder):
exclude = False
for ef in excludeFormat:
if re.search(ef, f):
print f, 'match exclude rule', ef
exclude = True
continue
if not exclude and re.search(srcFormat, f):
if len(rows) > 3:
dstFormat = rows[3]
print f, 'match', srcFormat, '->', dstFormat
dstFileName = re.sub(srcFormat, dstFormat, f)
else:
print f, 'match', srcFormat
dstFileName = f
srcFull = srcFolder+'/'+f
dstFull = dstFolder+'/'+dstFileName
if os.path.isdir(srcFull):
print srcFull, 'is a folder, skipping'
continue
print srcFull, 'renamed', dstFull
if os.path.exists(dstFull):
log.write(logDate()+'Already exists\n')
log.write(logDate()+dstFull+'\n\n')
continue
if not os.path.exists(dstFolder):
os.mkdir(dstFolder)
shutil.move(srcFull, dstFull)
log.write(logDate()+srcFull+'\n')
log.write(logDate()+dstFull+'\n\n')
log.close()