Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # to_delete_if_admin.py # please take caution with this
- import os
- import sys
- import ctypes
- filedir = 'F:/'
- files = os.listdir(filedir)
- print(files)
- def chkdsk():
- # Run chkdsk with the /f option to fix any errors, and /r to recover data from bad sectors
- os.system("chkdsk F: /f /r")
- def delete_files(n):
- files = os.listdir(filedir) # get a list of files in the F drive
- count = 0
- for i in range(len(files)):
- file_path = os.path.join(filedir, files[i])
- try:
- os.remove(file_path) # delete the file
- print(f"Deleted: {files[i]}")
- count += 1
- except:
- print(f"!!! Denied: {files[i]}")
- print(f"++++++ {count} files deleted")
- return len(files) - count
- if 'ADA' in files:
- # Get the path of the current script
- script_path = sys.argv[0]
- # Call the ShellExecute function to run the script with admin permissions
- ctypes.windll.shell32.ShellExecuteW(None, "runas", "python", f"\"{script_path}\"", None, 5)
- # Ask the user what operation they want to perform
- operation = input("What operation do you want to perform? (chkdsk/delete): ")
- if operation.lower() == "chkdsk":
- chkdsk()
- elif operation.lower() == "delete":
- num_files_left = delete_files(10) # delete 10 files at a time
- while True:
- prev_num_files_left = num_files_left
- num_files_left = delete_files(10)
- if num_files_left == prev_num_files_left:
- break
- else:
- print("Permission Denied.")
- print("\nDone.\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement