Skip to content

Instantly share code, notes, and snippets.

@wotori
Last active April 15, 2025 11:04
Show Gist options
  • Save wotori/a16ad7d89c1610cae31cd1d5cf9f7fd4 to your computer and use it in GitHub Desktop.
Save wotori/a16ad7d89c1610cae31cd1d5cf9f7fd4 to your computer and use it in GitHub Desktop.
copy media by date creation
import os
import shutil
from datetime import datetime
from tqdm import tqdm
source = "/mnt/raid/iPhoneDima/iphone_dump_25_04_14"
dest = "/mnt/raid/iPhoneDima/"
file_list = []
# Gather list of all files to copy (excluding .xmp)
for root, subdir, files in os.walk(source):
files = [f for f in files if not f.endswith(".xmp")]
for file in files:
full_path = os.path.join(root, file)
file_list.append(full_path)
# Create progress bar for copying
with tqdm(total=len(file_list), desc="Copying files") as pbar:
for file_path in file_list:
timestamp = int(os.path.getmtime(file_path))
date = datetime.utcfromtimestamp(timestamp).strftime('%Y-%m-%d')
year, month, day = date.split("-")
cp_file_path = os.path.join(dest, year, month, day)
os.makedirs(cp_file_path, exist_ok=True)
file = os.path.basename(file_path)
dest_file = os.path.join(cp_file_path, file)
if not os.path.exists(dest_file):
shutil.copy2(file_path, cp_file_path)
sidecar = f"{file_path}.xmp"
if os.path.exists(sidecar):
shutil.copy2(sidecar, cp_file_path)
pbar.set_postfix(file=file, destination=cp_file_path)
pbar.update(1)
# Verification step
missing_files = []
for file_path in file_list:
timestamp = int(os.path.getmtime(file_path))
date = datetime.utcfromtimestamp(timestamp).strftime('%Y-%m-%d')
year, month, day = date.split("-")
cp_file_path = os.path.join(dest, year, month, day)
file = os.path.basename(file_path)
dest_file = os.path.join(cp_file_path, file)
if not os.path.exists(dest_file):
missing_files.append(dest_file)
if not missing_files:
print("✅ All files copied successfully!")
else:
print("❌ Some files were not copied:")
for f in missing_files:
print(" -", f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment