Skip to content

Commit 2025bd9

Browse files
authored
Project: FileMagic_Organizer
1 parent 8aa865c commit 2025bd9

File tree

2 files changed

+165
-0
lines changed

2 files changed

+165
-0
lines changed

FileMagic_Organizer/README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
## FileMagic Organizer - Unleash the Power of Order
2+
3+
Welcome to FileMagic Organizer, where chaos meets its match. This Python script is your ally in the battle against cluttered directories. FileMagic Organizer effortlessly sorts your files into designated categories, bringing harmony to the digital realm.
4+
5+
## Index
6+
- [Introduction](#)
7+
- [Key Features](#key-features)
8+
- [Getting Started](#getting-started)
9+
- [Options provided](#options-provided)
10+
- [Usage](#usage)
11+
- [Customization](#customization)
12+
13+
## Key Features:
14+
15+
- 🔮 **Magical Categorization:** FileMagic Organizer intelligently sorts files by type, creating a structured and easily navigable file system.
16+
17+
- 📅 **Chronological Arrangement:** Dive into the past with chronological organization, unveiling the history of your files in an orderly timeline.
18+
19+
📏 **Size-based Sorting:** Experience the wizardry of size-based categorization, with files neatly grouped into small, medium, and large categories.
20+
21+
## **Getting Started:**
22+
23+
1.**Clone the Repository:** Embrace the magic by cloning the FileMagic Organizer repository.
24+
25+
2. 🚀 **Install Dependencies:** Initiate the enchantment with a quick installation of the required Python libraries.
26+
27+
3.**Run the Magic Spell:** Execute the script, follow the prompts, and witness the transformation as FileMagic Organizer weaves its organizational magic.
28+
29+
## Options provided
30+
31+
- Organize files by type (e.g., images, documents, videos).
32+
- Organize files by creation date into a hierarchical structure of year and month.
33+
- Categorize files by size into small, medium, and large categories.
34+
35+
## Usage
36+
37+
1. **Clone the Repository:**
38+
39+
```bash
40+
git clone https://github.com/malivinayak/file-organizer.git
41+
cd file-organizer
42+
```
43+
44+
2. **Install Dependencies:**
45+
46+
Ensure you have Python installed. Install the required libraries:
47+
48+
```bash
49+
pip install -r requirements.txt
50+
```
51+
52+
3. **Run the Script:**
53+
54+
```bash
55+
python organize_files.py
56+
```
57+
58+
## Customization
59+
60+
- Adjust the file type categories, file extensions, and any other settings in the script based on your needs.
61+
- Modify the size categories and ranges in the script for organizing files by size.

FileMagic_Organizer/main.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import os
2+
import shutil
3+
import datetime
4+
5+
def categorize_by_size(file_size):
6+
# Define size categories and their ranges in bytes
7+
size_categories = {
8+
'small': (0, 1024), # Up to 1 KB
9+
'medium': (1025, 1024 * 1024), # 1 KB to 1 MB
10+
'large': (1024 * 1025, float('inf')) # Larger than 1 MB
11+
}
12+
13+
for category, (min_size, max_size) in size_categories.items():
14+
if min_size <= file_size < max_size:
15+
return category
16+
17+
return 'unknown'
18+
19+
def organize_files(source_dir, destination_dir, organize_by_type=True, organize_by_date=True, organize_by_size=True):
20+
# Create a dictionary to map file extensions to corresponding folders
21+
file_types = {
22+
'images': ['.png', '.jpg', '.jpeg', '.gif'],
23+
'documents': ['.pdf', '.docx', '.txt'],
24+
'videos': ['.mp4', '.avi', '.mkv'],
25+
'other': [] # Add more categories and file extensions as needed
26+
}
27+
28+
# Create destination subdirectories if they don't exist
29+
if organize_by_type:
30+
for folder in file_types:
31+
folder_path = os.path.join(destination_dir, folder)
32+
os.makedirs(folder_path, exist_ok=True)
33+
34+
if organize_by_date:
35+
for year in range(2010, 2030): # Adjust the range based on your needs
36+
year_folder = os.path.join(destination_dir, str(year))
37+
os.makedirs(year_folder, exist_ok=True)
38+
39+
for month in range(1, 13):
40+
month_folder = os.path.join(year_folder, f"{month:02d}")
41+
os.makedirs(month_folder, exist_ok=True)
42+
43+
if organize_by_size:
44+
for size_category in ['small', 'medium', 'large']:
45+
size_folder = os.path.join(destination_dir, size_category)
46+
os.makedirs(size_folder, exist_ok=True)
47+
48+
# Scan the source directory and organize files
49+
for filename in os.listdir(source_dir):
50+
file_path = os.path.join(source_dir, filename)
51+
52+
if os.path.isfile(file_path):
53+
# Determine the file type based on extension
54+
file_type = None
55+
for category, extensions in file_types.items():
56+
if any(filename.lower().endswith(ext) for ext in extensions):
57+
file_type = category
58+
break
59+
60+
if organize_by_type and file_type:
61+
# Move the file to the corresponding subdirectory
62+
destination_folder = os.path.join(destination_dir, file_type)
63+
destination_path = os.path.join(destination_folder, filename)
64+
shutil.move(file_path, destination_path)
65+
print(f"Moved: {filename} to {file_type} folder")
66+
67+
if organize_by_date:
68+
# Get the creation date of the file
69+
creation_time = os.path.getctime(file_path)
70+
creation_date = datetime.datetime.fromtimestamp(creation_time)
71+
72+
# Determine the destination folder based on creation date
73+
destination_folder = os.path.join(
74+
destination_dir,
75+
str(creation_date.year),
76+
f"{creation_date.month:02d}",
77+
)
78+
79+
# Move the file to the corresponding subdirectory
80+
destination_path = os.path.join(destination_folder, filename)
81+
shutil.move(file_path, destination_path)
82+
print(f"Moved: {filename} to {creation_date.year}/{creation_date.month:02d} folder")
83+
84+
if organize_by_size:
85+
# Get the size of the file in bytes
86+
file_size = os.path.getsize(file_path)
87+
88+
# Determine the destination folder based on file size
89+
size_category = categorize_by_size(file_size)
90+
destination_folder = os.path.join(destination_dir, size_category)
91+
destination_path = os.path.join(destination_folder, filename)
92+
shutil.move(file_path, destination_path)
93+
print(f"Moved: {filename} to {size_category} folder")
94+
95+
# Get source and destination directories from the user
96+
source_directory = input("Enter the source directory path: ")
97+
destination_directory = input("Enter the destination directory path: ")
98+
99+
# Ask the user how they want to organize the files
100+
organize_by_type = input("Organize by file type? (yes/no): ").lower() == 'yes'
101+
organize_by_date = input("Organize by creation date? (yes/no): ").lower() == 'yes'
102+
organize_by_size = input("Organize by size? (yes/no): ").lower() == 'yes'
103+
104+
organize_files(source_directory, destination_directory, organize_by_type, organize_by_date, organize_by_size)

0 commit comments

Comments
 (0)