Jump to content

Win2000 defragment...


ezzetabi
 Share

Recommended Posts

As we all probably know Windows 2000 defragmenter do not accept any command line parameter and it is not easy to use in script since it need user interaction...

Not anymore!

here a ver .0001 Alpha script that allow to start defrag without any worry.

just call the script with a number as parameter, this number is the # drive that will be defraged.

E.G. you have in list c: e: and k:. if you start this script with a "3" as parameter with drive k: will be defraged.

If WinExists("Ezzetabi defrag") Then Exit
AutoItWinSetTitle("Ezzetabi defrag")

Opt ('WinTitleMatchMode', 4)
Opt ('MustDeclareVars', 1)
Opt ('TrayIconDebug', 1)

Dim $FINISH, $DRIVE = 1, $C
$FINISH = "Deframmentazione completata";<- Change according to your localization.

If $CMDLINE[0] > 0 Then
   $DRIVE = Int($CMDLINE[1]) - 1
   If $DRIVE < 0 Then $DRIVE = 0
EndIf

Run(@WindowsDir & '\system32\mmc.exe "' & @SystemDir & '\dfrg.msc"', '')
WinWait("classname=MMCMainFrame", "DEFRAG_LISTVIEW")

ControlFocus("classname=MMCMainFrame", "DEFRAG_LISTVIEW", "SysListView321")
ControlSend("classname=MMCMainFrame", "DEFRAG_LISTVIEW", "SysListView321", "{home}")

For $C = 1 To $DRIVE
   ControlSend("classname=MMCMainFrame", "DEFRAG_LISTVIEW", "SysListView321", "{down}")
Next

ControlClick("classname=MMCMainFrame", "DEFRAG_LISTVIEW", "Button2")
WinWait("classname=#32770", $FINISH)
ControlClick("classname=#32770", $FINISH, "Button2")
WinClose("classname=MMCMainFrame", "DEFRAG_LISTVIEW")

This code do not manage the event that you have less than 15% of free space... If some may send me the WinSpy screen of the 15% message I'll would be very grateful.

Edited by ezzetabi
Link to comment
Share on other sites

And after I wondered... What if I want to defrag all drives without worring too much?

And so the defrag all script is born:

If WinExists("Ezzetabi defrag") Then Exit
AutoItWinSetTitle("Ezzetabi defrag")

Opt ('WinTitleMatchMode', 4)
Opt ('MustDeclareVars', 1)
Opt ('TrayIconDebug', 1)

Dim $FINISH, $C, $N_DRIVES = 0, $DRIVE
$FINISH = "Deframmentazione completata"; <-- Change this according to your localization.

For $C = 67 To 90
   If DriveGetType(Chr($C) & ":\") == "RAMDisk" Or DriveGetType(Chr($C) & ":\") == "Fixed" Then
      $N_DRIVES = $N_DRIVES + 1 
   EndIf
Next

For $DRIVE = 1 To $N_DRIVES
   Run(@WindowsDir & '\system32\mmc.exe "' & @SystemDir & '\dfrg.msc"', '')
   WinWait("classname=MMCMainFrame", "DEFRAG_LISTVIEW")
   
   ControlSend("classname=MMCMainFrame", "DEFRAG_LISTVIEW", "SysListView321", "{home}")
   
   For $C = 1 To $DRIVE - 1
      ControlSend("classname=MMCMainFrame", "DEFRAG_LISTVIEW", "SysListView321", "{down}")
   Next
   
   ControlClick("classname=MMCMainFrame", "DEFRAG_LISTVIEW", "Button2")
   WinWait("classname=#32770", $FINISH)
   ControlClick("classname=#32770", $FINISH, "Button2")
   WinClose("classname=MMCMainFrame", "DEFRAG_LISTVIEW")
Next
Edited by ezzetabi
Link to comment
Share on other sites

  • 1 year later...

Hello scripters ;) ,

I'm new in the AutoIt World, and you know what ?... I'm french !!!

I'm learning this new (for me) script language which seems to be very powerfull !

The preceding script is very interesting and works very well with the user logged on the screen ! But when I execute it with Scheduled Tasks in Windows 2000 with another user, the script starts but stay waiting just after running the mmc i.e. there : WinWait("classname=MMCMainFrame", "DEFRAG_LISTVIEW") !

With Task Manager, I see that the mmc is running but it does nothing, not running the defragmenter at all !

Why ? May someone explain this to me ! My purpose is not defragmenting my disks but learning to do scripts that can run in batch mode, with Scheduled Tasks and a special account (admin of course) ! Thanks for responses...

Bye :">

Link to comment
Share on other sites

For my Win2k auto defragging needs, I combined the srvany.exe from the Win2k Resource Kit with contig from sysinternals.com. I am able to schedule a defrag to run even when the machine is not logged in. The only issue is it needs to run as a user with administrative privileges, but it works.

Here's my simple autoit script:

CODE
Dim $day

$defragtime = 1

$domain = "Domain"

$password = "password"

$logdir = "\\server\logs\"

While 1

If @HOUR = $defragtime Then

If $day <> @YDAY Then

RunAsSet("administrator", $domain, $password)

$driveno = DriveGetDrive( "fixed")

For $i = 1 To $driveno[0]

RunWait(@ComSpec & " /c contig.exe -s " & $driveno[$i] & "\*.*", @SystemDir)

$file = FileOpen($logdir & @ComputerName & "-" & @YEAR & @MON & @MDAY & "-log.txt", 2)

FileWriteLine($file, "Defrag of " & $driveno[$i] & "Complete")

FileClose($file)

Next

$day = @YDAY

Sleep(50000)

EndIf

Else

Sleep(50000)

EndIf

WEnd

Of course the user the service runs as also needs write access to the log folder. I also had the output of contig redirected to a log file, but that got too large.

To install as a service, use srvany.exe like this

instsrv.exe SchDefrag %windir%\srvany.exe

Then modify the service in service manager to run the compiled script, where ever that is, and run as a user with admin privileges. Then start the service, as long as the service is running, the script will run at whatever time you specify, no need for task scheduler or anything like that.

By the way, this will also work fine on XP and I'm assuming Win2k3.

In addition to changing the run as for the service, you also need to add one registry entry for srvany.

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\SchDefrag\Parameters]
"Application"="C:\\WINDOWS\\schdefrag.exe"
Edited by archrival
Link to comment
Share on other sites

Hello Forum ...

i had the same idea on WinXP ... maybe you can take some part´s for win2k ?

it´s work´s on my german XP ... was not testet under other languages

maybe it help´s

bye Lukestar ...

; ----------------------------------------------------------------------------------
;
; AutoIt Version: 3.1.0
; Author:        Martin Heinze <martinheinze@gmx.net>
;
; Script Function:
; Windows XP Microsoft Defragmentierung automatisch für alle Laufwerke
;
; ----------------------------------------------------------------------------------


; ----------------------------------------------------------------------------------
; Neulinge, bitte nur die kommentierten Zeilen verändern.
; Für Schäden übernehme ich keine Haftung!
; ----------------------------------------------------------------------------------

; Prozessanzeige starten
ProgressOn("Windows XP Defragmentierung aller Laufwerke", "Laufwerke werden defragmentiert", "0 Prozent")
$proz = 0
ProgressSet( $proz, $proz & " Prozent")


; Globale Variablen definieren
$fixed = 0
$run = 0

; Anzahl der Laufwerke im System ermitteln
$var = DriveGetDrive( "Fixed" )
If Not @error Then $fixed = $fixed + 1

; Microsoft Defrag starten
Run("c:\windows\System32\mmc.exe c:\windows\system32\dfrg.msc")

; Erste Defragmentierung starten, eine Festplatte muss es ja wohl geben, oder ?;-)
$run = $run + 1
WinWait("Defragmentierung","Defragmentierung")
If Not WinActive("Defragmentierung","Defragmentierung") Then WinActivate("Defragmentierung","Defragmentierung")
WinWaitActive("Defragmentierung","Defragmentierung")
Sleep(250)
Send("{TAB}")
Sleep(250)
Send("{TAB}")
Sleep(250)
Send("{TAB}")
Sleep(250)
Send("{TAB}")
Sleep(250)
Send("{SPACE}")

WinWait("Defragmentierung","Beri&cht anzeigen")
If Not WinActive("Defragmentierung","Beri&cht anzeigen") Then WinActivate("Defragmentierung","Beri&cht anzeigen")
WinWaitActive("Defragmentierung","Beri&cht anzeigen")
Sleep(250)
Send("{ALTDOWN}s{ALTUP}")

; Prozentzahl errechnen
$proz = (100/$fixed)*$run
; Prozessanzeige aktualisieren
ProgressSet( $proz, $proz & " Prozent")

; und nun die restlichen in der Schleife ...
While $run < $fixed

WinWait("Defragmentierung","Defragmentierung")
If Not WinActive("Defragmentierung","Defragmentierung") Then            WinActivate("Defragmentierung","Defragmentierung")
WinWaitActive("Defragmentierung","Defragmentierung")
Sleep(250)
Send("{Down}")
Sleep(250)
Send("{TAB}")
Sleep(250)
Send("{TAB}")
Sleep(250)
Send("{SPACE}")

WinWait("Defragmentierung","Beri&cht anzeigen")
If Not WinActive("Defragmentierung","Beri&cht anzeigen") Then           WinActivate("Defragmentierung","Beri&cht anzeigen")
WinWaitActive("Defragmentierung","Beri&cht anzeigen")
Sleep(250)
Send("{ALTDOWN}s{ALTUP}")

$run = $run + 1
; Prozentzahl errechnen
$proz = (100/$fixed)*$run
; Prozessanzeige aktualisieren
ProgressSet( $proz, $proz & " Prozent")

WEnd

sleep(250)
; Prozessanzeige schließen
ProgressOff()

; Defrag fertig Programm wird beendet
WinWait("Defragmentierung","Defragmentierung")
If Not WinActive("Defragmentierung","Defragmentierung") Then WinActivate("Defragmentierung","Defragmentierung")
WinWaitActive("Defragmentierung","Defragmentierung")
Sleep(250)
Send("{ALTDOWN}d{ALTUP}b")

; Fertig und Schluß
Link to comment
Share on other sites

Well... What can I say... Thanks archrival and Lukestar ! But my purpose was not there...

I don't care of the defragmentation of my disks ! The script is only an exercise used to learn AutoIt and how to execute a script in a batch environment !

Your solutions may be quiet good !!! But I don't care...

A function like WinActivate don't work in batch mode (Scheduled Tasks with an account different from the account logged on the screen) ! The state of the window (WinGetState) will never show the 'active bit' on !

So, please friends, answer me about the batch mode... not about the defragmentation !

Thanks a lot...

Link to comment
Share on other sites

As we all probably know Windows 2000 defragmenter do not accept any command line parameter and it is not easy to use in script since it need user interaction...

Hi Ezzetabi-

I've found that what happens when defrag is done, can be unpredictable. Sometimes there is no status message. Sometimes a message box pops up asking if I want to see the log file. Also, on some machines, Dell has put in a diagnostics drive (60MB) that appears first in the list of drives. That means that the location of the C-drive in the list is not always the same.

My question is, "does your program always end properly, or would the above cases cause it to fail?"

Am I fool :P ? Am I the only one that wants to execute an AutoIt script in batch mode ;) ? That's not possible ! Please help me :mad2: !!!

:oops:

Hi Zigloo-

I think you need to start your own post. You are asking a question that is not part of this thread. You might get much more response that way. Also, can if you do start a new thread, can you give and example of what you want to do, so we can better understand? Thanks... :dance:

Edited by jefhal
...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format
Link to comment
Share on other sites

Or run this commandline util:

Hi JdeB- How much trust would you put in "Morphasys", the group that makes autodefrag? It looks interesting, but who knows what's in it?

For my Win2k auto defragging needs, I combined the srvany.exe from the Win2k Resource Kit with contig from sysinternals.com.

Hi Archrival- Just wondering what contig does over the long run to your disk fragmentation? That is, if it is individually contigging files, isn't it also moving them here and there to find space? That means that all of my Word related files, or SolidWorks related files are now somewhat scattered? I would guess that application files would get further and further apart over time?
...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format
Link to comment
Share on other sites

Hi JdeB- How much trust would you put in "Morphasys", the group that makes autodefrag? It looks interesting, but who knows what's in it?

Hi Archrival- Just wondering what contig does over the long run to your disk fragmentation? That is, if it is individually contigging files, isn't it also moving them here and there to find space? That means that all of my Word related files, or SolidWorks related files are now somewhat scattered? I would guess that application files would get further and further apart over time?

Contig makes all files contiguous, that's about it, it doesn't move any files to the beginning of the disk or anything advanced like that. It just uses the built in defrag functions in NT. I personally use defrag.exe in XP/2003, another option for 2K systems would be DIRMS, it's a little more advanced, it also uses the builtin defrag functions. They also provide a program called buzzsaw that can continuously keep your hard drive defragged, it's not free though.

Edited by archrival
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...