Re: Silent Defrag of Windows 2000 PC

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("").run "cmd /c defrag.cmd c: -f", 0

Thanks guys inadvance...

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lee W, MVPTechnology and Business Process AdvisorCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
defrag.cmd implies you're using a batch file to accomplish this?
Shouldn't that be defrag.exe?
ManfredtooAuthor Commented:
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.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

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?
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') "cmd /c defrag c: -f", 0
Lee W, MVPTechnology and Business Process AdvisorCommented:
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 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 :)
Lee W, MVPTechnology and Business Process AdvisorCommented:
Oh, I agree - which is why I don't generally recommend doing that.
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
ManfredtooAuthor Commented:
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...
ManfredtooAuthor Commented:
sirbounty: i meant wont work out!! amendments  :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.