-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopy_files_by_date.py
50 lines (41 loc) · 1.34 KB
/
copy_files_by_date.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
# copy_files_by_date.py
# author: deebugger ([email protected])
#
# copy all image files of the form "yyyy-mm-dd..." to their corresponding
# directories (those being tgtDir/yyyy/mm/dd)
# useful for cleaning the Dropbox camera upload directory
#
# notice: this script doesn't delete any file, it just creates copies
#
# initial setup:
# - srcDir - where all the files are
# - tgtDir - parent directory for creating the yyyy/mm/dd directories
import sys, os, shutil
# fill these two!
srcDir = ""
tgtDir = ""
if srcDir == "" or tgtDir == "":
print "--\n-- Fill in the srcDir and tgtDir correctly, then run again\n--"
sys.exit
if not os.path.exists(tgtDir):
os.makedirs(tgtDir)
allFiles = os.listdir(srcDir)
for file in allFiles:
split = file[:10].split("-")
if len(split) == 3:
# construct the target directory
year = split[0]
month = split[1]
day = split[2]
targetDir = os.path.join(tgtDir, year, month, day)
# make sure the file doesn't already exist in the target,
# or print an error and move on (don't copy)
targetPath = os.path.join(targetDir, file)
if os.path.exists(targetPath):
print "File: ", targetPath, " exists, skipping"
continue
# create the target directory, if it doesn't exist
if not os.path.exists(targetDir):
os.makedirs(targetDir)
# copy the file
shutil.copy2(os.path.join(srcDir, file), targetPath)