99from pathlib import Path
1010from tkinter import filedialog
1111import tkinter as tk
12+ import ctypes
13+
14+
15+ def is_running_as_admin ():
16+ """Check if the script is running with administrative privileges"""
17+ if os .name == 'nt' : # Windows
18+ try :
19+ return ctypes .windll .shell32 .IsUserAnAdmin ()
20+ except :
21+ return False
22+ else :
23+ # Unix/Linux/Mac
24+ return os .geteuid () == 0
1225
1326
1427def show_banner ():
@@ -24,17 +37,17 @@ def get_directory(prompt_text):
2437 """Get directory path using file dialog"""
2538 root = tk .Tk ()
2639 root .withdraw () # Hide the main window
27-
40+
2841 print (f"{ prompt_text } " )
2942 print ("Opening file dialog..." )
30-
43+
3144 directory = filedialog .askdirectory (title = prompt_text )
3245 root .destroy ()
33-
46+
3447 if not directory :
3548 print ("No directory selected. Exiting..." )
3649 sys .exit (1 )
37-
50+
3851 print (f"Selected: { directory } " )
3952 return directory
4053
@@ -44,7 +57,7 @@ def get_extensions():
4457 extensions = []
4558 print ("\n Enter file extensions to move (without dots, e.g., 'txt', 'pdf'):" )
4659 print ("Type 'done' when finished." )
47-
60+
4861 while True :
4962 ext = input ("Extension: " ).strip ().lower ()
5063 if ext == 'done' :
@@ -54,7 +67,7 @@ def get_extensions():
5467 print (f"Added: .{ ext } " )
5568 elif ext in extensions :
5669 print (f".{ ext } already added" )
57-
70+
5871 return extensions
5972
6073
@@ -73,19 +86,19 @@ def move_files_by_extension(source_dir, dest_dir, extensions):
7386 """Move files with specified extensions from source to destination"""
7487 source_path = Path (source_dir )
7588 dest_path = Path (dest_dir )
76-
89+
7790 if not source_path .exists ():
7891 print (f"Error: Source directory '{ source_dir } ' does not exist" )
7992 return False
80-
93+
8194 if not dest_path .exists ():
8295 print (f"Error: Destination directory '{ dest_dir } ' does not exist" )
8396 return False
84-
97+
8598 moved_count = 0
86-
99+
87100 print (f"\n Searching for files with extensions: { ', ' .join (f'.{ ext } ' for ext in extensions )} " )
88-
101+
89102 for file_path in source_path .iterdir ():
90103 if file_path .is_file ():
91104 file_ext = file_path .suffix .lstrip ('.' ).lower ()
@@ -97,7 +110,7 @@ def move_files_by_extension(source_dir, dest_dir, extensions):
97110 moved_count += 1
98111 except Exception as e :
99112 print (f"Error moving { file_path .name } : { e } " )
100-
113+
101114 print (f"\n Completed! Moved { moved_count } files." )
102115 return True
103116
@@ -106,18 +119,18 @@ def move_folder(source_dir, dest_dir):
106119 """Move entire folder from source to destination"""
107120 source_path = Path (source_dir )
108121 dest_path = Path (dest_dir )
109-
122+
110123 if not source_path .exists ():
111124 print (f"Error: Source directory '{ source_dir } ' does not exist" )
112125 return False
113-
126+
114127 if not dest_path .exists ():
115128 print (f"Error: Destination directory '{ dest_dir } ' does not exist" )
116129 return False
117-
130+
118131 folder_name = source_path .name
119132 final_dest = dest_path / folder_name
120-
133+
121134 try :
122135 shutil .move (str (source_path ), str (final_dest ))
123136 print (f"Successfully moved folder '{ folder_name } ' to '{ dest_dir } '" )
@@ -132,7 +145,7 @@ def delete_permanently(path):
132145 if not os .path .exists (path ):
133146 print (f"Error: Path '{ path } ' does not exist" )
134147 return False
135-
148+
136149 try :
137150 if os .path .isfile (path ):
138151 os .remove (path )
@@ -157,14 +170,13 @@ def main_menu():
157170 print ("3. Delete permanently" )
158171 print ("4. Exit" )
159172 print ("=" * 50 )
160-
173+
161174 try :
162175 choice = input ("Enter your choice (1-4): " ).strip ()
163176 except (EOFError , KeyboardInterrupt ):
164- # Handle end of input or keyboard interrupt gracefully
165177 print ("\n Exiting..." )
166178 return
167-
179+
168180 if choice == '1' :
169181 handle_move_files ()
170182 elif choice == '2' :
@@ -173,47 +185,47 @@ def main_menu():
173185 handle_delete_permanently ()
174186 elif choice == '4' :
175187 print ("Goodbye!" )
176- return # Exit the function instead of sys.exit()
188+ return
177189 else :
178190 print ("Invalid choice. Please enter 1, 2, 3, or 4." )
179191
180192
181193def handle_move_files ():
182194 """Handle moving files by extension"""
183195 print ("\n --- MOVE FILES BY EXTENSION ---" )
184-
196+
185197 source = get_directory ("Select SOURCE directory (where files are located)" )
186198 dest = get_directory ("Select DESTINATION directory (where to move files)" )
187-
199+
188200 print (f"\n Source: { source } " )
189201 print (f"Destination: { dest } " )
190-
202+
191203 if not confirm_action ("Are these directories correct?" ):
192204 return
193-
205+
194206 extensions = get_extensions ()
195207 if not extensions :
196208 print ("No extensions specified. Returning to main menu." )
197209 return
198-
210+
199211 print (f"\n Will move files with extensions: { ', ' .join (f'.{ ext } ' for ext in extensions )} " )
200212 print (f"From: { source } " )
201213 print (f"To: { dest } " )
202-
214+
203215 if confirm_action ("Proceed with moving files?" ):
204216 move_files_by_extension (source , dest , extensions )
205217
206218
207219def handle_move_folder ():
208220 """Handle moving entire folder"""
209221 print ("\n --- MOVE ENTIRE FOLDER ---" )
210-
222+
211223 source = get_directory ("Select FOLDER to move" )
212224 dest = get_directory ("Select DESTINATION directory" )
213-
225+
214226 print (f"\n Folder to move: { source } " )
215227 print (f"Destination: { dest } " )
216-
228+
217229 if confirm_action ("Move this folder?" ):
218230 move_folder (source , dest )
219231
@@ -222,12 +234,12 @@ def handle_delete_permanently():
222234 """Handle permanently deleting items"""
223235 print ("\n --- DELETE PERMANENTLY ---" )
224236 print ("WARNING: This will permanently delete files/folders!" )
225-
237+
226238 path = get_directory ("Select file or folder to delete permanently" )
227-
239+
228240 print (f"\n Will permanently delete: { path } " )
229241 print ("This action cannot be undone!" )
230-
242+
231243 if confirm_action ("Are you absolutely sure you want to delete this?" ):
232244 if confirm_action ("Final confirmation - DELETE PERMANENTLY?" ):
233245 delete_permanently (path )
@@ -236,6 +248,12 @@ def handle_delete_permanently():
236248def main ():
237249 """Main program entry point"""
238250 try :
251+ if not is_running_as_admin ():
252+ print ("ERROR: This program must be run as Administrator." )
253+ print ("Right-click the Command Line or Powershell and choose 'Run as administrator'." )
254+ print ("Goodbye!" )
255+ sys .exit (1 )
256+
239257 show_banner ()
240258 main_menu ()
241259 except KeyboardInterrupt :
@@ -247,4 +265,4 @@ def main():
247265
248266
249267if __name__ == "__main__" :
250- main ()
268+ main ()
0 commit comments