<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: defrag code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 26 Jul 2008 07:25:37 GMT</pubDate>
    <description>DZone Snippets: defrag code</description>
    <item>
      <title>Automate Defrag of Windows Host via Python</title>
      <link>http://snippets.dzone.com/posts/show/3925</link>
      <description>Automation script to defrag a Windows machine. The defrag results will be emailed to specified email address. This script can also handle running defrag over multiple volumes. NOTE: if you are running this on Windows 2003 64-bit you have to place a copy of defrag.exe in the same directory as the python script. Otherwise Windows will barf and say it doesn't know what defrag is. Don't know why this happens.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#*****************************************************************#&lt;br /&gt;#                                                                 #&lt;br /&gt;# Filename:             autodefrag.py                             #&lt;br /&gt;# Date Written:         4/27/2007                                 #&lt;br /&gt;#                                                                 #&lt;br /&gt;# Purpose:              Intelligent interface to Windows defrag   #&lt;br /&gt;#                       command. Emails results when complete.    #&lt;br /&gt;#                       Can handle multiple volumes. Add this     #&lt;br /&gt;#                       script to Windows task scheduler to       #&lt;br /&gt;#                       fully automate defragmentation.           #&lt;br /&gt;#                                                                 #&lt;br /&gt;# OS:        		Windows 2003, XP                          #&lt;br /&gt;#                                                                 #&lt;br /&gt;#*****************************************************************#&lt;br /&gt;&lt;br /&gt;import smtplib, sys, MimeWriter, StringIO, base64, os, time&lt;br /&gt;&lt;br /&gt;##VARIABLES########################################################&lt;br /&gt;&lt;br /&gt;drives = ["C:","D:","E:"]           # list of volumes to defragment&lt;br /&gt;SMTP_server = "somemailserver.com"  #SMTP email server&lt;br /&gt;email_to = "someone@example.com"   &lt;br /&gt;email_from = "someone2@example.com"&lt;br /&gt;logfile_path = "c:/"               #path to where logfile is saved&lt;br /&gt;&lt;br /&gt;##END_VARIABLES####################################################&lt;br /&gt;&lt;br /&gt;def mail(serverURL=None, sender='', to='', subject='', text=''):&lt;br /&gt;&lt;br /&gt;    message = StringIO.StringIO()&lt;br /&gt;    writer = MimeWriter.MimeWriter(message)&lt;br /&gt;    writer.addheader('Subject', subject)&lt;br /&gt;    writer.startmultipartbody('mixed')&lt;br /&gt;&lt;br /&gt;    # start off with a text/plain part&lt;br /&gt;    part = writer.nextpart()&lt;br /&gt;    body = part.startbody('text/plain')&lt;br /&gt;    body.write(text)&lt;br /&gt;&lt;br /&gt;    # now add an attachment&lt;br /&gt;    part = writer.nextpart()&lt;br /&gt;    part.addheader('Content-Transfer-Encoding', 'base64')&lt;br /&gt;    body = part.startbody('text/plain')&lt;br /&gt;    base64.encode(open(filename, 'rb'), body)&lt;br /&gt;&lt;br /&gt;    # finish off&lt;br /&gt;    writer.lastpart()&lt;br /&gt;&lt;br /&gt;    # send the mail&lt;br /&gt;    smtp = smtplib.SMTP(serverURL)&lt;br /&gt;    smtp.sendmail(sender, to, message.getvalue())&lt;br /&gt;    smtp.quit()&lt;br /&gt;&lt;br /&gt;def timestamp():&lt;br /&gt;    #mytime = time.asctime(time.localtime())&lt;br /&gt;    mytime = time.strftime("%Y%m%d", time.localtime())&lt;br /&gt;    return mytime&lt;br /&gt;&lt;br /&gt;##MAIN#############################################################&lt;br /&gt;#get the host name&lt;br /&gt;hs = os.popen('hostname')&lt;br /&gt;computer_name = hs.read()&lt;br /&gt;hs.close()&lt;br /&gt;&lt;br /&gt;mytime = timestamp()&lt;br /&gt;filename = logfile_path + "DEFRAG" + mytime + ".log"&lt;br /&gt;&lt;br /&gt;#execute the defrag command for each drive&lt;br /&gt;for d in drives:&lt;br /&gt;    cmd = "defrag.exe -v " + d + " &gt;&gt;" + filename&lt;br /&gt;    print "Starting defragment: ", cmd&lt;br /&gt;    errorlevel = os.system(cmd)&lt;br /&gt;    if errorlevel == 0:&lt;br /&gt;        print "Emailing results"&lt;br /&gt;        mail(SMTP_server,email_from,email_to,'Disk Defrag of ' + computer_name + ' Complete','Disk defragmentation finished\n')&lt;br /&gt;    else:&lt;br /&gt;        print "Error occurred! Emailing results"&lt;br /&gt;        mail(SMTP_server,email_from,email_to,'Disk Defrag of ' + computer_name + ' Error','Disk defragmentation encountered an error')&lt;br /&gt;&lt;br /&gt;        #hold the cmd window open if error occurred - remove this if you want window to exit&lt;br /&gt;        hold = raw_input('\nPress ENTER to exit')&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 27 Apr 2007 17:00:08 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3925</guid>
      <author>minitotoro (Natalie)</author>
    </item>
  </channel>
</rss>
