-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmvbydate
More file actions
executable file
·49 lines (40 loc) · 1.69 KB
/
mvbydate
File metadata and controls
executable file
·49 lines (40 loc) · 1.69 KB
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
#!/usr/bin/env python
# move by date -- a way of reorganizing files by their date stamp
# Author: David McClosky
# Homepage: http://zorglish.org
# Code: http://github.com/dmcc
# vi: syntax=python
import os
import time
from optparse import OptionParser
from pathlib import Path
from waterworks.Files import mkdirparents
parser = OptionParser()
parser.add_option("-k", "--dry-run", action='store_true',
help="Don't actually move any files, just print the "
"commands to do so.")
parser.add_option("-f", "--format", default='%Y.%m.%d', metavar="FMT",
help="A format string appropriate for time.strftime "
"(default '%Y.%m.%d' results in YYYY.MM.DD)")
parser.add_option("-t", "--timestamp", default='mtime',
choices=['ctime', 'mtime', 'atime'],
help="Which timestamp to use (ctime, mtime, or atime)")
(options, args) = parser.parse_args()
for filename in args:
filename = Path(filename)
if filename.is_dir():
print("Skipping directory:", filename)
continue
timestamp = getattr(os.path, 'get' + options.timestamp)(filename)
new_dirname = time.strftime(options.format, time.localtime(timestamp))
full_new_path = filename.parent/new_dirname/filename.name
if not options.dry_run:
mkdirparents(full_new_path.parent)
# in case full_new_dirname already existed and had a file with the
# same name in it
if full_new_path.exists():
raise ValueError("{} already has a {} in it".format(full_new_path.parent,
filename))
print('mv', filename, full_new_path)
if not options.dry_run:
filename.rename(full_new_path)