-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDuplicateFileFinder.py
More file actions
284 lines (227 loc) · 9.91 KB
/
DuplicateFileFinder.py
File metadata and controls
284 lines (227 loc) · 9.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/usr/bin/env python3
"""
duplicatefinder.py
A Python implementation of the duplicate file finder.
Finds duplicate files based on content (SHA-256).
Features:
- Recursive directory scanning
- Content-based duplicate detection (SHA-256)
- Friendly, human-readable output
- Cross-platform
"""
import sys
import os
# Ensure UTF-8 output for emojis/colors on Windows
if sys.stdout.encoding != 'utf-8':
sys.stdout.reconfigure(encoding='utf-8')
import hashlib
import argparse
from collections import defaultdict
from pathlib import Path
# ============================================================================
# ANSI Colors
# ============================================================================
GREEN = "\033[32m"
RED = "\033[31m"
BLUE = "\033[34m"
YELLOW = "\033[33m"
RESET = "\033[0m"
# ============================================================================
# Helpers
# ============================================================================
def format_size(size_bytes: int) -> str:
"""Formats file size into a human-readable string (e.g., 4.2 MB)."""
s = float(size_bytes)
units = ["B", "KB", "MB", "GB", "TB"]
i = 0
while s >= 1024 and i < 4:
s /= 1024
i += 1
# Match C++ precision logic: 0 decimals for Bytes, 2 for others
if i == 0:
return f"{int(s)} {units[i]}"
else:
return f"{s:.2f} {units[i]}"
def compute_file_hash(path: str) -> str:
"""Computes the SHA-256 hash of a file."""
sha256 = hashlib.sha256()
buffer_size = 8192
try:
with open(path, "rb") as f:
while True:
chunk = f.read(buffer_size)
if not chunk:
break
sha256.update(chunk)
except OSError as e:
print(f"Warning: Skipping {path} ({e})", file=sys.stderr)
return ""
return sha256.hexdigest()
def print_progress_bar(current: int, total: int, bar_length: int = 30):
"""Displays a green progress bar in the terminal."""
fraction = current / total if total > 0 else 1.0
percentage = int(fraction * 100)
filled_length = int(bar_length * fraction)
bar = "█" * filled_length + "-" * (bar_length - filled_length)
# \r is carriage return to update the line in-place
sys.stdout.write(f"\r{GREEN}[{bar}] {current}/{total} ({percentage}%){RESET}")
sys.stdout.flush()
# ============================================================================
# Scanning Logic
# ============================================================================
def run_scan(directory_path: str):
target_dir = Path(directory_path).resolve()
if not target_dir.exists() or not target_dir.is_dir():
print(f"Error: Directory not found or invalid: {target_dir}", file=sys.stderr)
sys.exit(1)
print(f"Counting files in {target_dir} ...")
all_files = []
try:
for root, _, files in os.walk(target_dir):
for name in files:
filepath = Path(root) / name
# Simple check for regular file (excluding symlinks as per previous logic)
if not filepath.is_symlink():
all_files.append(filepath)
except Exception as e:
print(f"Error during directory traversal: {e}", file=sys.stderr)
sys.exit(1)
total_count = len(all_files)
if total_count == 0:
print("\nNo files found to scan.")
# We still print footer as requested
print(f"\n{YELLOW}Don't worry: all duplicate files have been verified using SHA-256 and you can be 100% confident that files detected as duplicates are identical.{RESET}")
print(f"{BLUE}If you want to support development, you can make a donation here: https://www.paypal.com/paypalme/EnricoArama{RESET}")
return
print(f"Processing {total_count} files ...")
# Step 1: Group files by size
files_by_size = defaultdict(list)
processed_count = 0
# Temporary list for files that actually need hashing
potential_duplicates_by_size = defaultdict(list)
for filepath in all_files:
try:
size = filepath.stat().st_size
files_by_size[size].append(filepath)
except OSError:
pass
# We count it as processed for the size check stage
# But we only reach 100% after hashing, so we'll redistribute the progress.
# Actually, let's just use a single continuous counter.
# To make it smooth, we show progress here, but we'll continue the same counter in the hashing phase.
# However, it's better to:
# 1. Quick size scan (optional bar)
# 2. Hashing (the real slow part)
# Let's refine the progress:
# Files that don't need hashing reach "processed" state during hashing loop.
# Files that DO need hashing reach "processed" state AFTER hashing.
# To keep total_count accurate:
# 1. Identify files that ARE NOT potential duplicates (unique size).
# 2. Increment progress for them immediately.
# 3. Increment progress for the rest as they are hashed.
unique_sized_files_count = sum(1 for paths in files_by_size.values() if len(paths) < 2)
processed_count = unique_sized_files_count
# Show initial progress for skipping unique sizes
print_progress_bar(processed_count, total_count)
duplicates = []
for size, paths in files_by_size.items():
if len(paths) < 2:
continue
# Optimization: empty files
if size == 0:
duplicates.append({
'file_size': 0,
'files': paths
})
processed_count += len(paths)
print_progress_bar(processed_count, total_count)
continue
files_by_hash = defaultdict(list)
for p in paths:
h = compute_file_hash(str(p))
processed_count += 1
print_progress_bar(processed_count, total_count)
if h: # if hash computation succeeded
files_by_hash[h].append(p)
for h, final_paths in files_by_hash.items():
if len(final_paths) > 1:
duplicates.append({
'file_size': size,
'files': final_paths
})
# Clear progress bar line
sys.stdout.write("\r" + " " * 60 + "\r")
sys.stdout.flush()
# ============================================================================
# Output Generation
# ============================================================================
if not duplicates:
print("\nNo duplicate files found.")
else:
print("\nFound duplicate files (same content):\n")
total_files = 0
total_wasted_bytes = 0
for group in duplicates:
files = group['files']
size = group['file_size']
count = len(files)
if count == 0: continue
rep_name = files[0].name
# First file is GREEN (Keep)
print(f"📄 {GREEN}{rep_name} (x{count}){RESET} — identical content")
for i, fpath in enumerate(files):
is_last = (i == count - 1)
prefix = "└─ " if is_last else "├─ "
# First file (index 0) gets GREEN, others RED
color = GREEN if i == 0 else RED
print(f" {prefix}{color}{fpath}{RESET}")
# Advice message in BLUE
print(f"{BLUE}Tip: keep only the file {files[0].name} and remove those highlighted in red.{RESET}")
print()
total_files += count
total_wasted_bytes += (count - 1) * size
if duplicates:
print(f"Total duplicate groups: {len(duplicates)}")
print(f"Total duplicated files: {total_files}")
print(f"Total wasted space: {format_size(total_wasted_bytes)}")
# Final footer messages (always displayed)
print()
print(f"{YELLOW}Don't worry: all duplicate files have been verified using SHA-256 and you can be 100% confident that files detected as duplicates are identical.{RESET}")
print(f"{BLUE}If you want to support development, you can make a donation here: https://www.paypal.com/paypalme/EnricoArama{RESET}")
# ============================================================================
# Main Helper
# ============================================================================
def print_help(prog_name: str):
print(f"Usage: {prog_name} <command> [options]\n")
print("Commands:")
print(" scan \"DIRECTORY_PATH\" Scan the directory recursively for duplicate files.")
print(" --help Show this help message.\n")
print("Description:")
print(" DuplicateFinder detects file duplicates based on EXACT CONTENT.")
print(" File names and timestamps are ignored.")
print(" It uses file size grouping and SHA-256 hashing for accuracy.\n")
print("Example:")
print(f" {prog_name} scan \"C:\\Users\\MyUser\\Documents\"")
# ============================================================================
# Main Entry Point
# ============================================================================
def main():
if len(sys.argv) < 2:
print_help(sys.argv[0])
sys.exit(1)
arg1 = sys.argv[1]
if arg1 in ("--help", "-h"):
print_help(sys.argv[0])
sys.exit(0)
if arg1 == "scan":
if len(sys.argv) < 3:
print("Error: Missing directory path.", file=sys.stderr)
print(f"Usage: {sys.argv[0]} scan \"DIRECTORY_PATH\"")
sys.exit(1)
run_scan(sys.argv[2])
sys.exit(0)
print(f"Unknown command: {arg1}", file=sys.stderr)
print_help(sys.argv[0])
sys.exit(1)
if __name__ == "__main__":
main()