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

Thanks guys inadvance...

Cheers
ManfredtooAsked:
Who is Participating?
 
Lee W, MVPConnect With a Mentor Technology 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
0
 
sirbountyCommented:
defrag.cmd implies you're using a batch file to accomplish this?
Shouldn't that be defrag.exe?
0
 
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.
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
sirbountyCommented:
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?
0
 
sirbountyCommented:
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
0
 
Lee W, MVPTechnology and Business Process AdvisorCommented:
0
 
sirbountyCommented:
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 :)
0
 
Lee W, MVPTechnology and Business Process AdvisorCommented:
Oh, I agree - which is why I don't generally recommend doing that.
0
 
jahboiteCommented:
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.

Code:

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"
          f.Close
          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"
               f.Close
               wshell.Run drive.driveletter & ":\frag.bat",0,True
               fso.DeleteFile drive.driveletter & ":\frag.bat",True
          'Else
          '&&reporting stuff here&&
          End If
     
     
     End If
   Next


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
     Loop

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

     f2.Close
     Set f2 = Nothing

End Function
0
 
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...
0
 
ManfredtooAuthor Commented:
sirbounty: i meant wont work out!! amendments  :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.