From 7852b060d7ed8236ccb23b9a9f5c0b36b291e5c5 Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Wed, 20 Aug 2025 21:14:01 +0300 Subject: [PATCH] Feat(mv): Move/rename a directory of images --- mv.py | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/mv.py b/mv.py index 9adf21f..19e69b7 100755 --- a/mv.py +++ b/mv.py @@ -1,5 +1,5 @@ #! /usr/bin/env python3 -"""Move/rename an image +"""Move/rename an image or a directory of images This file is a part of XSetBg. @@ -40,12 +40,30 @@ connection = xsetbg_db._connection txn = connection.transaction() xsetbg_db._connection = txn -rows = list(xsetbg_db.selectBy(full_name=from_filename)) -if not rows: - sys.exit("No rows by filename %s" % from_filename) +if os.path.isdir(from_filename): # Rename a directory + rows = list(xsetbg_db.select( + LIKE(xsetbg_db.q.full_name, from_filename + '/%') + )) + if not rows: + sys.exit("No rows in directory %s" % from_filename) -row = rows[0] -row.full_name = to_filename + for row in rows: + row.full_name = os.path.join( + to_filename, + # Remove separator to make the path relative + row.full_name[len(from_filename) + 1:]) + + dest_dir = os.path.dirname(to_filename) + if not os.path.exists(dest_dir): + os.makedirs(dest_dir) + +else: # Rename just 1 file + rows = list(xsetbg_db.selectBy(full_name=from_filename)) + if not rows: + sys.exit("No rows by filename %s" % from_filename) + + row = rows[0] + row.full_name = to_filename os.rename(from_filename, to_filename) -txn.commit() +txn.commit(close=True) -- 2.39.5