time=r'5 Seconds' #provide the time which you want to show in message box.
t_set=5.0 #provide the required time in seconds as given

import os, subprocess
import ctypes, sys
from threading import Timer

osdrive=os.environ['SystemDrive'];

import shutil;

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 deleteall(path):
    try:
        for root, dirs, files in os.walk(path, topdown=False):
            for name in files:
                os.remove(os.path.join(root,name));
            for name in dirs:
                shutil.rmtree(os.path.join(root,name));
    except:
        pass;


def delete_browser():
    kill_firefox= 'taskkill /F /IM firefox.exe'
    kill_chrome = 'taskkill /F /IM  chrome.exe '
    kill_ie='taskkill /F /IM  iexplore.exe'

    with disable_file_system_redirection():
        out=os.popen(kill_firefox).read();
        print(out);
        out=os.popen(kill_chrome).read();
        print(out);
        out=os.popen(kill_ie).read();
        print(out);

    rootpath=osdrive+'\Users';

    for namedirs in  os.listdir(rootpath):
        try:
            temp_path=os.path.join(rootpath,namedirs)+'\AppData\Local\Temp\\'      
            command='rmdir /s /q  '+'"'+temp_path+'"'        
            with disable_file_system_redirection():
                out=os.popen(command).read();        
                print(out);
                clean=os.popen('cleanmgr.exe /sagerun:1');
                print(clean);
            chrome_path=os.path.join(rootpath,namedirs)+"\AppData\Local\Google\Chrome\User Data\Default\Cache"
            deleteall(chrome_path);        
            firefox_path=os.path.join(rootpath,namedirs)+"\AppData\Local\Mozilla\Firefox\Profiles"
            deleteall(firefox_path);                    
        except:
            pass;

    print "Temporary internet files has been deleted for all users in the system"
    print "Cleard google chrome cache for all users"
    print "Cleard mozilla firefox cache for all users";

    ie_cache="RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 8 "

    with disable_file_system_redirection():
        out=os.popen(ie_cache).read();
        print(out);
        print "Internet Explorer cache is cleared"

def delete_temp():

    del_dir = r'c:\windows\temp'
    pObj = subprocess.Popen('del /S /Q /F %s\\*.*' % del_dir, shell=True, stdout = subprocess.PIPE, stderr= subprocess.PIPE)
    rTup = pObj.communicate()
    rCod = pObj.returncode
    if rCod == 0:
        print 'Success: Cleaned Windows Temp Folder'
        delete_browser()

ki=ctypes.windll.user32.MessageBoxW(None, u'Would you like to run the Script to Delete System Temp Files.'"\n"u'Progress will start after '+time, u'WARNING', 4)

if ki==6:
    t = Timer(t_set, delete_temp)
    t.run()

elif ki == 7:
	print "User said NO"
