Last active
August 4, 2023 13:32
-
-
Save wotori/a16ad7d89c1610cae31cd1d5cf9f7fd4 to your computer and use it in GitHub Desktop.
copy media by date creation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import shutil | |
from datetime import datetime | |
from tqdm import tqdm | |
source = "/from" | |
dest = "/to" | |
dist_set = set() | |
file_count = 0 | |
# Count the total number of files to copy | |
for root, subdir, files in os.walk(source): | |
files = list(filter(lambda x: x[-4:] != ".xmp", files)) | |
file_count += len(files) | |
dist_set = set() | |
file_count = 0 | |
# Count the total number of files to copy | |
for root, subdir, files in os.walk(source): | |
files = list(filter(lambda x: x[-4:] != ".xmp", files)) | |
file_count += len(files) | |
# Create progress bar | |
with tqdm(total=file_count, desc="Copying files") as pbar: | |
for root, subdir, files in os.walk(source): | |
files = list(filter(lambda x: x[-4:] != ".xmp", files)) | |
for file in files: | |
file_path = os.path.join(root, file) | |
timestamp = int(os.path.getmtime(file_path)) | |
date = datetime.utcfromtimestamp(timestamp).strftime('%Y-%m-%d') # %H:%M:%S | |
year, month, day = date.split("-") | |
cp_file_path = os.path.join(dest, year, month, day) | |
if cp_file_path not in dist_set: | |
dist_set.add(cp_file_path) | |
os.makedirs(cp_file_path, exist_ok=True) | |
file_check = os.path.join(cp_file_path, file) | |
if not os.path.exists(file_check): | |
shutil.copy2(file_path, cp_file_path) | |
sidecar = f"{file_path}.xmp" | |
if os.path.exists(sidecar): | |
shutil.copy2(sidecar, cp_file_path) | |
# Update progress bar and display the current file being copied and the destination | |
pbar.set_postfix(file=file, destination=cp_file_path) | |
pbar.update(1) | |
print("done!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment