Solved

Re: Silent Defrag of Windows 2000 PC

Posted on 2007-04-05
11
845 Views
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...

Cheers
0
Comment
Question by:Manfredtoo
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 95

Accepted Solution

by:
Lee W, MVP earned 500 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
0
 
LVL 67

Expert Comment

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

Author Comment

by:Manfredtoo
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.
0
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 
LVL 67

Expert Comment

by:sirbounty
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?
0
 
LVL 67

Expert Comment

by:sirbounty
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
0
 
LVL 95

Expert Comment

by:Lee W, MVP
ID: 18861615
0
 
LVL 67

Expert Comment

by:sirbounty
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 :)
0
 
LVL 95

Expert Comment

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

Expert Comment

by:jahboite
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.

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
 

Author Comment

by:Manfredtoo
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...
0
 

Author Comment

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

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

840 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