Hi Everyone,
Please use this script to delete the temporary files from the specific path for all users in a system.
Note : This Script run as a “System User”
And also please refer this link for your reference:
https://scripts.comodo.com/frontend/web/topic/delete-custom-temp-files
import os
drive = os.environ['SYSTEMDRIVE']
del_dir=[drive+r"\Windows\Temp",drive+r"\Windows\Logs\CBS"]
import os
import subprocess
import ctypes
import stat
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)
Path=[]
USER=os.popen("wmic useraccount get Name").read()
Name=[i.strip() for i in USER.split("
") if i.strip()]
Profile=Name[1:]
print 'Deleting temp files begins... '
for j in Profile:
temp = drive+"\\Users\\%s\\APPDATA\\LOCAL\\TEMP" %(j)
del_dir.append(temp);
for i in del_dir:
for dirpath, dirnames, files in os.walk(i):
for i in files:
i = os.path.join(dirpath,i)
os.chdir(dirpath)
if os.path.exists(i):
try:
os.chmod(i,0644)
os.remove(i)
print "Deleted "+ i
print "Deleted Successfully"
except Exception as error:
print "The file working in another process" + i
Output:
20170707-Delete-Temp-Files.json (2.23 KB)