Advertisement
here2share

# to_delete_if_admin.py

Sep 13th, 2023 (edited)
1,496
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.42 KB | None | 0 0
  1. # to_delete_if_admin.py # please take caution with this
  2.  
  3. import os
  4. import sys
  5. import ctypes
  6.  
  7. filedir = 'F:/'
  8. files = os.listdir(filedir)
  9. print(files)
  10.  
  11. def chkdsk():
  12.     # Run chkdsk with the /f option to fix any errors, and /r to recover data from bad sectors
  13.     os.system("chkdsk F: /f /r")
  14.    
  15. def delete_files(n):
  16.     files = os.listdir(filedir) # get a list of files in the F drive
  17.     count = 0
  18.    
  19.     for i in range(len(files)):
  20.         file_path = os.path.join(filedir, files[i])
  21.         try:
  22.             os.remove(file_path) # delete the file
  23.             print(f"Deleted: {files[i]}")
  24.             count += 1
  25.         except:
  26.             print(f"!!! Denied: {files[i]}")
  27.     print(f"++++++ {count} files deleted")
  28.     return len(files) - count
  29.  
  30. if 'ADA' in files:
  31.     # Get the path of the current script
  32.     script_path = sys.argv[0]
  33.     # Call the ShellExecute function to run the script with admin permissions
  34.     ctypes.windll.shell32.ShellExecuteW(None, "runas", "python", f"\"{script_path}\"", None, 5)
  35.  
  36.     # Ask the user what operation they want to perform
  37.     operation = input("What operation do you want to perform? (chkdsk/delete): ")
  38.     if operation.lower() == "chkdsk":
  39.         chkdsk()
  40.     elif operation.lower() == "delete":
  41.         num_files_left = delete_files(10) # delete 10 files at a time
  42.         while True:
  43.             prev_num_files_left = num_files_left
  44.             num_files_left = delete_files(10)
  45.             if num_files_left == prev_num_files_left:
  46.                 break
  47.  
  48. else:
  49.  
  50.     print("Permission Denied.")
  51.  
  52. print("\nDone.\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement