Skip to content

Commit 13a4ab4

Browse files
authored
(new) merge folders python script (#12)
1 parent fd05d86 commit 13a4ab4

2 files changed

Lines changed: 59 additions & 1 deletion

File tree

README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# StarrScripts
22

3-
A curated collection of scripts to optimize and manage various functions related to Starr applications and associated tools. These scripts are designed to enhance functionality, improve management, and automate routine tasks.
3+
A curated collection of scripts to optimize and manage various functions related to Starr applications and associated tools.
4+
Occasionally holds random useful scripts as well.
5+
These scripts are designed to enhance functionality, improve management, and automate routine tasks.
6+
All scripts are **Created By: [Bakerboy448](https://github.com/bakerboy448/) unless otherwise noted.
47

58
## Scripts Overview
69

@@ -34,6 +37,22 @@ A curated collection of scripts to optimize and manage various functions related
3437
3. Review and adjust script parameters to fit your use case.
3538
- **Output:** Results are saved to a file as specified in the script.
3639

40+
### Merge Folders Utility
41+
42+
- **Script:** `merge_folders.py`
43+
- **Description:** A robust utility designed for merging multiple directories into a single target directory, ensuring that no existing files are overwritten in the process. This script uses a recursive function to efficiently merge content, while providing detailed logging of each step to monitor the creation, movement, and skipping of files and directories.
44+
- **Features:**
45+
- **Recursive Merging:** Seamlessly combines contents of source directories into a target directory.
46+
- **Non-destructive:** Preserves existing files by not overwriting them.
47+
- **Error Handling:** Captures and logs errors during the merging process, ensuring reliable operations.
48+
- **Detailed Logging:** Tracks and logs every file and directory operation, providing clear visibility into the process.
49+
- **Usage Case:** Ideal for consolidating data in scenarios like organizing media libraries, merging data backups, or simplifying file system structures.
50+
- **Setup:**
51+
1. Update `source_dirs` and uncomment the variable
52+
2. Update `target_dir` and uncomment the variable
53+
3. Uncomment `atomic_moves` to engage the movement operation
54+
4. Run the script with `python3 merge_folders.py`
55+
3756
### Notifiarr Branch Builder
3857

3958
- **Script:** `notifiarr-branch-builder.sh`

merge_folders.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
def merge_directories(src, dst):
2+
"""
3+
Recursively merges directories from src to dst without overwriting existing files.
4+
"""
5+
for item in os.listdir(src):
6+
src_path = os.path.join(src, item)
7+
dst_path = os.path.join(dst, item)
8+
9+
if os.path.isdir(src_path):
10+
# If it's a directory, recurse into it
11+
if not os.path.exists(dst_path):
12+
os.makedirs(dst_path)
13+
logging.info(f"Directory created: {dst_path}")
14+
merge_directories(src_path, dst_path)
15+
else:
16+
# It's a file, check if it exists in the destination
17+
if not os.path.exists(dst_path):
18+
# Move the file atomically if on the same filesystem
19+
shutil.move(src_path, dst_path)
20+
logging.info(f"File moved: {src_path} to {dst_path}")
21+
else:
22+
logging.info(f"File skipped (already exists): {dst_path}")
23+
24+
def atomic_moves(source_directories, target_directory):
25+
"""
26+
Handles atomic moving from multiple source directories to a single target directory.
27+
"""
28+
for src in source_directories:
29+
logging.info(f"Processing source directory: {src}")
30+
try:
31+
# Start the merging process for each source directory
32+
merge_directories(src, target_directory)
33+
except Exception as e:
34+
logging.error(f"Error during moving process from {src}: {e}")
35+
36+
# Example use case (commented out for safety):
37+
# source_dirs = ['/mnt/data/media/tv-slade', '/mnt/data/media/tv-tmp']
38+
# target_dir = '/mnt/data/media/tv'
39+
# atomic_moves(source_dirs, target_dir)

0 commit comments

Comments
 (0)