-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDecremental_Files_Sync_Selected_Dir.py
55 lines (44 loc) · 1.8 KB
/
Decremental_Files_Sync_Selected_Dir.py
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
import os
import shutil
import tkinter as tk
from tkinter import filedialog
import ctypes
import sys
def is_admin():
try:
return ctypes.windll.shell32.IsUserAnAdmin()
except:
return False
if not is_admin():
ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, " ".join(sys.argv), None, 1)
sys.exit()
def main():
# Open a file dialog to choose the source directory
root = tk.Tk()
root.withdraw() # Hide the main window
source = filedialog.askdirectory(title="Select Source Folder")
# If the user cancels the dialog, exit the script
if not source:
print("Source folder not selected. Exiting.")
exit()
# Open a file dialog to choose the destination directory
destination = filedialog.askdirectory(title="Select Destination Folder")
# If the user cancels the dialog, exit the script
if not destination:
print("Destination folder not selected. Exiting.")
exit()
# Get file sizes and create a list of (size, path) tuples
file_sizes = [(os.path.getsize(os.path.join(root, file)), os.path.join(root, file))
for root, dirs, files in os.walk(source) for file in files]
# Sort the list in descending order by file size
file_sizes.sort(reverse=True)
# Copy files to the destination directory
for size, file_path in file_sizes:
relative_path = os.path.relpath(file_path, source)
destination_path = os.path.join(destination, relative_path)
os.makedirs(os.path.dirname(destination_path), exist_ok=True)
shutil.copy2(file_path, destination_path)
print(f"Copied: {os.path.basename(file_path)} -> {destination_path}")
print("Files copied successfully.")
if __name__ == "__main__":
main()