import os
import shutil
import subprocess
import re

dirPath = []
x=0
ext = [".lnk"]
cmd = subprocess.Popen("net users",shell=True,stdout=subprocess.PIPE,stdin=subprocess.PIPE)
r,e=cmd.communicate()
if e:
    print(e)
else:
    value = re.findall("The command.+",r)
    for i in value :
        val = r.replace(i,"")
    keyword = "-------------------------------------------------------------------------------"
    before_keyword, keyword, after_keyword = (val.strip()).partition(keyword)
    val_list = after_keyword.split()
    for i in val_list:
        if i.strip() == "Administrator" or i.strip() == "DefaultAccount" or i.strip() == "Guest" or i.strip() == "WDAGUtilityAccount" :
            pass 
        else:
            path = "C:\\Users\\"+i.strip()+"\\Desktop"
            dirPath.append(path)

for i in dirPath:
    fileList = os.listdir(i)
    try:
        os.chmod(dirPath,0644)
    except:
        pass
    for f in fileList:
        try:
            if f.endswith(tuple(ext)):
                pass 
            elif os.path.isfile(os.path.join(i, f)):
                os.remove(os.path.join(i, f))
            elif os.path.isdir(os.path.join(i, f)):
                shutil.rmtree(os.path.join(i, f))
            
        except:
            x+=1
            
    if x==0:
        print "Cleared successfully : "+i
    else:
        print "Some error removing Files or Folders : "+i
    
