Re: Silent Defrag of Windows 2000 PC

Posted on 2007-04-05
Medium Priority
Last Modified: 2008-01-09
Hi Experts
I got a problem and need some help in silent defragmentation of pc for windows 2000.  i have a sample script that only works in windows XP environment. my objective is to anyone one who clicks on the vbs script he/she is able to defrag the pc silently without knowing its processing regardless of administrators or normal users. here is the sample scripts that works in WinXP that works well.

strComputer = "."
Set objWMI = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
CreateObject("wscript.shell").run "cmd /c defrag.cmd c: -f", 0

Thanks guys inadvance...

Question by:Manfredtoo
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +1
LVL 96

Accepted Solution

Lee W, MVP earned 2000 total points
ID: 18858533
The Windows 2000 defrag does not have the scripting ability that XP's does.

There is a way using vbscrpt to script it - I've seen it - but it's not nearly as easy.  I would suggest you are better off purchasing a 3rd party program like Diskeeper or Perfect Disk
LVL 67

Expert Comment

ID: 18858571
defrag.cmd implies you're using a batch file to accomplish this?
Shouldn't that be defrag.exe?

Author Comment

ID: 18861508
sirbounty:  hiya sirbounty again, its actually defrag... typo.... actually under my environment there mixture of windows xp and 2000 hope there will be a defrag script does it all.
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

LVL 67

Expert Comment

ID: 18861528
My 2000 knowledge is, sadly, waning... : |
I thought you could script a batch solution under any OS since 9x, but perhaps I'm mistaken, since lee's pointed out that it's limited.
I don't know that I've seen the vbs method you mentioned either leew - would that be an option here?
LVL 67

Expert Comment

ID: 18861542
Why are you using wmi in this?  Any particular reason - or anything else in your script that wasn't posted?

Why not simply something like this:

Dim objShell:Set objShell=CreateObject("Wscript.Shell')
objShell.run "cmd /c defrag c: -f", 0
LVL 96

Expert Comment

by:Lee W, MVP
ID: 18861615
LVL 67

Expert Comment

ID: 18861630
The problem with that method in this scenario - it uses SendKeys, which never work reliably when you're trying to script something silently, particularly when a user is active on the system.
Sendkeys does just what you tell it to...in that script example, it must first of all find the Defrag applet (which kills the 'silent' requirement here).
The main thing I don't like about SendKeys though - is once it grabs its window - they're "nothing" to prevent the user from taking focus from it and putting it on something else.
So if the key requirements to your app were <Alt> F + N + T, and the user opened up Explorer in the midst of that key sequence, the latter part of it is going to get sent to Explorer - not the app intended.
I thought you meant you had found an api call or something (I was getting excited :)
LVL 96

Expert Comment

by:Lee W, MVP
ID: 18861639
Oh, I agree - which is why I don't generally recommend doing that.
LVL 12

Expert Comment

ID: 18861685
I can't claim that this code will work on 2000 though I believe it does.
It's a vb script which can be scheduled with windows scheduler.

The script will enumerate all type 2 drives (fixed disks) in the machine and will launch defrag.exe to do the analyse stage. It will create an analysis report in the directory you specify. If the analyse stage returned "You do not need to defragment this volume." the drive doesn't get defragged, otherwise it does. The script dumps a defrag report and then moves to the next drive enumerated.


Option Explicit

Dim fso, wshell, colDrives, drive, f, makefol

Const read = 1, write = 2, append = 8
dim yyyymmdd
dim pathtoreport
dim pathtoreportroot

yyyymmdd = Right(Date, 4) & Mid(Date, 4, 2) & Left(Date, 2)

'change <somepath> to some path e.g.  C:\Temp
pathtoreportroot = "<somepath>\Reports\Defrag\"

set fso = CreateObject("Scripting.FileSystemObject")
if not fso.folderexists(pathtoreportroot & yyyymmdd & "\") then
makefol = fso.createfolder(pathtoreportroot & yyyymmdd)
end if
pathtoreport = pathtoreportroot & yyyymmdd & "\" & yyyymmdd & "_"

Set wshell = wscript.CreateObject("WScript.Shell")

set colDrives = fso.drives

For Each drive In colDrives 'get a report by creating a batch file and running it, cleanup last
          if drive.drivetype = "2" Then 'check that it's a physical hard drive
          fso.CreateTextFile drive.driveletter & ":\fragcheck.bat",True
          set f = fso.OpenTextFile(drive.driveletter & ":\fragcheck.bat",write)
          f.WriteLine "@echo off"
          f.WriteLine "c:\windows\system32\defrag.exe " & drive.driveletter & ": -a > " & pathtoreport & drive.driveletter & "_fragreport.txt"
          wshell.Run drive.driveletter & ":\fragcheck.bat",0,True
          fso.DeleteFile drive.driveletter & ":\fragcheck.bat",True
          If Not checkdefrag(pathtoreport & drive.driveletter & "_fragreport.txt") = "True" Then
               fso.CreateTextFile drive.driveletter & ":\frag.bat",True
               set f = fso.OpenTextFile(drive.driveletter & ":\frag.bat",write)
               f.WriteLine "@echo off"
               f.WriteLine "c:\windows\system32\defrag.exe " & drive.driveletter & ": -f -v > " & pathtoreport & drive.driveletter & "_fragsummary.txt"
               wshell.Run drive.driveletter & ":\frag.bat",0,True
               fso.DeleteFile drive.driveletter & ":\frag.bat",True
          '&&reporting stuff here&&
          End If
     End If

Function checkdefrag(report)

     Dim n, f2, regexp1, tempcheck

     Set regExp1 = New regexp 'create a regular expression
     With regexp1    
     .global = True
     .ignorecase = True
     .pattern = "You do not need to defragment this volume."     'set search criteria
     End With

     n = 1

     Set f2 = fso.OpenTextFile(report,read)

     Do While f2.AtEndOfStream <> True
          If regexp1.test(f2.ReadLine) = True Then
               tempcheck = True
          End If

     If tempcheck = True Then
          checkdefrag = True
          checkdefrag = False
     End If

     Set f2 = Nothing

End Function

Author Comment

ID: 18864298
leew & sirbounty: leew i seen the site u provided... i do agree with sirbountry silent work out.... prehaps diskeeper might be the best or other... but just to check out leew. will regular users able to defrag or they need administrative rights?? i have yet to use the software...

Author Comment

ID: 18864335
sirbounty: i meant wont work out!! amendments  :)

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month12 days, 22 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question