Skip to content

Commit f2aed1e

Browse files
committed
Add -p/--preserve-dirs option
1 parent 53e32c9 commit f2aed1e

File tree

2 files changed

+26
-17
lines changed

2 files changed

+26
-17
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ Or you can sort by year without century, then week number, then an abbreviated d
3434

3535
The possibilities go on and on.
3636

37+
## original source directory structure
38+
The default behavior is to omit the original directory structure in the source directory and to move or copy the files to the formatted date directories under the destination directory. By using the -p/--preserve-dir option, the original source directory structure will be preserved and created under the destination directory as well as the formatted date directories.
39+
40+
For Example, with a directory structure of src/dir1/dir2/file, the default behavior will lead to:
41+
42+
dst/2014/08-Aug/file
43+
44+
With -p/--preserve-dir the behavior is:
45+
46+
dst/dir1/dir2/2014/08-Aug/file
47+
3748
## duplicate removal
3849
SortPhotos will *always* check to make sure something with the same file name doesn't already exist where it's trying to write, so that you don't unintentionally overwrite a file. It this occurs it will append a number on the end of the file. So for example if photo.jpg was taken on June 1, 2010 but 2010 > June > photo.jpg already exists then the new file will be copied as photo_1.jpg and so on. SortPhotos will go one step further and if it finds a file of the same name, it will then run a file compare to see if the files are actually the same. If they are *exactly* the same, it will just skip the copy (or move) operation. This will prevent you from having duplicate files. However you have the option of turning this off (not the name comparison, that will always happen, just the weeding out of duplicates). This option would be useful, for example, if you are copying over a bunch of new photos that you are sure don't already exist in your organized collection of photos. It's a little faster to skip duplicate detection. Invoke the option ``--keep-duplicates`` in order to skip duplicate detection.
3950

src/sortphotos.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def get_creation_time(path):
116116
# --------- main script -----------------
117117

118118
def sortPhotos(src_dir, dest_dir, extensions, sort_format, move_files, removeDuplicates,
119-
ignore_exif, day_begins):
119+
ignore_exif, day_begins, preserve_dirs):
120120

121121

122122
# some error checking
@@ -147,15 +147,16 @@ def sortPhotos(src_dir, dest_dir, extensions, sort_format, move_files, removeDup
147147

148148
# add file root and save the matched file in list
149149
for match in matches:
150-
matched_files.append(os.path.join(root, match))
150+
matched_files.append((root, match))
151151

152152

153153
# setup a progress bar
154154
num_files = len(matched_files)
155155
idx = 0
156156

157157

158-
for src_file in matched_files:
158+
for file_root, file_name in matched_files:
159+
src_file = os.path.join(file_root, file_name) # Full original file name
159160

160161
# update progress bar
161162
numdots = int(20.0*(idx+1)/num_files)
@@ -194,16 +195,16 @@ def sortPhotos(src_dir, dest_dir, extensions, sort_format, move_files, removeDup
194195
date = check_for_early_morning_photos(date, day_begins)
195196

196197
# create folder structure
197-
dir_structure = date.strftime(sort_format)
198-
dirs = dir_structure.split('/')
199-
dest_file = dest_dir
200-
for thedir in dirs:
201-
dest_file = os.path.join(dest_file, thedir)
202-
if not os.path.exists(dest_file):
203-
os.makedirs(dest_file)
198+
dest_file_dirs = [dest_dir]
199+
if preserve_dirs:
200+
dest_file_dirs.append(file_root)
201+
dest_file_dirs += date.strftime(sort_format).split('/')
202+
dest_file = os.path.join(*dest_file_dirs)
203+
if not os.path.exists(dest_file):
204+
os.makedirs(dest_file)
204205

205206
# setup destination file
206-
dest_file = os.path.join(dest_file, os.path.basename(src_file))
207+
dest_file = os.path.join(dest_file, file_name)
207208
root, ext = os.path.splitext(dest_file)
208209

209210
# check for collisions
@@ -234,11 +235,6 @@ def sortPhotos(src_dir, dest_dir, extensions, sort_format, move_files, removeDup
234235
else:
235236
shutil.copy2(src_file, dest_file)
236237

237-
238-
print
239-
240-
241-
242238
if __name__ == '__main__':
243239

244240
import argparse
@@ -249,6 +245,7 @@ def sortPhotos(src_dir, dest_dir, extensions, sort_format, move_files, removeDup
249245
parser.add_argument('src_dir', type=str, help='source directory (searched recursively)')
250246
parser.add_argument('dest_dir', type=str, help='destination directory')
251247
parser.add_argument('-m', '--move', action='store_true', help='move files instead of copy')
248+
parser.add_argument('-p', '--preserve-dirs', action='store_true', help='preserve original src_dir folder structure while moving or copying to dest_dir')
252249
parser.add_argument('-s', '--sort', type=str, default='%Y/%m-%b',
253250
help="choose destination folder structure using datetime format \n\
254251
https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior. \n\
@@ -270,7 +267,8 @@ def sortPhotos(src_dir, dest_dir, extensions, sort_format, move_files, removeDup
270267
args = parser.parse_args()
271268

272269
sortPhotos(args.src_dir, args.dest_dir, args.extensions, args.sort,
273-
args.move, not args.keep_duplicates, args.ignore_exif, args.day_begins)
270+
args.move, not args.keep_duplicates, args.ignore_exif,
271+
args.day_begins, args.preserve_dirs)
274272

275273

276274

0 commit comments

Comments
 (0)