Description :
This script file download defragller and defrag the drive which has more than 30% fragmentation.
Note:
Run as LocalSystem User
import os
import urllib2
import math
import re
import ctypes
from subprocess import PIPE,Popen
url='http://download.ccleaner.com/dfsetup221.exe'
silent='/S'
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 defrag(fragmetvol,path):
with disable_file_system_redirection():
print("Fragment analysis....")
process=Popen("defrag -a %s:\ "%fragmetvol,stderr=PIPE,stdout=PIPE)
res,err=process.communicate()
if err:
print err
else:
print res
fragmetpercent=re.findall("Total\s+fragmented\s+space\s+=\s+\d+%",res)
fragpercentage=int(fragmetpercent[0].split()[-1].replace("%",""))
if fragpercentage>30:
os.chdir(path)
print os.popen('df.exe %s'%fragmetvol).read()
else:
print "The fragement percentage is less than 30% \n "+fragmetpercent[0]
def Download(URL):
import urllib2
import os
print "Download started"
fileName =URL.split('/')[-1]
src_path=os.environ['ProgramData']
silent='/S'
fp = os.path.join(src_path, fileName)
request = urllib2.Request(URL, headers={'User-Agent' : "Magic Browser"})
parsed = urllib2.urlopen(request)
if os.path.exists(src_path):
print "Path already exists"
if not os.path.exists(src_path):
os.makedirs(src_path)
with open(fp, 'wb') as f:
while True:
chunk=parsed.read(100*1000*1000)
if chunk:
f.write(chunk)
else:
break
print "The file downloaded successfully in specified path",fp
try:
print'Downloaded Application %s Installation Started'%fileName
os.popen(fp+' '+silent).read()
print '%s Application Successfully Installed'%fileName
os.popen("del /Q 'C:\users\public\desktop\Defraggler.lnk'").read()
except:
return 'No : '+fp+' is exist'
a=os.path.join(r'C:\Program Files',r'Defraggler')
Download(url)
vols=os.popen("wmic logicaldisk where drivetype=3 get caption").read().split()[1:]
fragl=[i.replace(":","") for i in vols]
for i in fragl:
defrag(i,a)
os.chdir(a)
print os.popen('uninst.exe'+' '+"/S").read()