Solved

Re: Silent Defrag of Windows 2000 PC

Posted on 2007-04-05
11
839 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
Comment Utility
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
Comment Utility
defrag.cmd implies you're using a batch file to accomplish this?
Shouldn't that be defrag.exe?
0
 

Author Comment

by:Manfredtoo
Comment Utility
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
 
LVL 67

Expert Comment

by:sirbounty
Comment Utility
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
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 95

Expert Comment

by:Lee W, MVP
Comment Utility
0
 
LVL 67

Expert Comment

by:sirbounty
Comment Utility
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
Comment Utility
Oh, I agree - which is why I don't generally recommend doing that.
0
 
LVL 12

Expert Comment

by:jahboite
Comment Utility
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
Comment Utility
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
Comment Utility
sirbounty: i meant wont work out!! amendments  :)
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
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…
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now