Description :
This script deletes users desktop,downloads and documents files and folders.
Standard user cannot delete files and folders , A admin user can only delete files and folders.
Instructions
Run as logged in user with admin privilages for that user.
Note:
1.Run as logged in user (Makes sure logged in user have folder delete permissions)
2.Run as LocalSystem user(It will delete all the users documents, downloads ,desktop even for logged in user)
import ctypes
import os
from subprocess import PIPE,Popen
import time
import shutil
start=time.time()
class disable_file_system_redirection:
_disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection
_revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection
def __enter__(self):
self.old_value = ctypes.c_long()
self.success = self._disable(ctypes.byref(self.old_value))
def __exit__(self, type, value, traceback):
if self.success:
self._revert(self.old_value)
def permissions(dirpath):
mode=0o777
if os.path.isdir(dirpath):
try:
for root,dirs,files in os.walk(dirpath,topdown=False):
for dircs in [os.path.join(root,d) for d in dirs]:
os.chmod(dircs,mode)
for s_file in [os.path.join(root,f) for f in files]:
os.chmod(s_file,mode)
except Exception as E:
print "File being Used %s"
def convert_bytes(size, unit=None):
if unit == "KB":
return (str(round(size / 1024, 3)))
elif unit == "MB":
return (str(round(size / (1024 * 1024), 3)))
elif unit == "GB":
return (str(round(size / (1024 * 1024 * 1024), 3)))
else:
return (str(size))
def size(path):
#initialize the size
total_size = 0
#use the walk() method to navigate through directory tree
for dirpath, dirnames, filenames in os.walk(path):
for i in filenames:
#use join to concatenate all the components of path
f = os.path.join(dirpath, i)
#use getsize to generate size in bytes and add it to the total size
total_size += os.path.getsize(f)
return total_size
users=os.popen("net user").read().split()[5:-4]
fil_users=[i.strip() for i in users if i.strip()!="Administrator" and i.strip()!="Guest"]
loggeduser=os.popen("whoami").read().split("\\")[1].strip()
n_fil_users=[]
for i in fil_users:
if i.upper()!=loggeduser.upper():
n_fil_users.append(i)
else:
pass
#desktop
path_desktop=[]
path_documents=[]
path_download=[]
for i in n_fil_users:
value1="C:\\Users\\"+i+"\\Desktop"
value2="C:\\Users\\"+i+"\\Documents"
value3="C:\\Users\\"+i+"\\Downloads"
#print value
if os.path.exists(value1):
path_desktop.append(value1)
path_documents.append(value2)
path_download.append(value3)
else:
pass
# print path_desktop
# print path_documents
# print path_download
#merged list
paths=[]
paths.extend(path_documents)
paths.extend(path_desktop)
paths.extend(path_download)
# print paths
for i in paths:
j=size(i)
print i+" size:"+convert_bytes(j,"MB")+" MB"
with disable_file_system_redirection():
for i in paths:
permissions(i)
print "Removing files and folders %s"%i
shutil.rmtree(i,ignore_errors=True)
#checking
print "Checking"
for i in paths:
j=size(i)
print i+" size:"+convert_bytes(j,"MB")+" MB"
end=time.time()
print "Execution time"
print end-start
Comments