Jump to content

Auto frag at the end of installation


Recommended Posts


Here a vbs script that will defrag all drives on the local computer.

Dim Act, Fso, strDrv, Drvs

Set Act = CreateObject("Wscript.shell")

Set Fso = CreateObject("Scripting.FileSystemObject")

Drvs = Array("C:\","D:\","E:\","F:\","G:\","H:\","I:\","J:\","K:\","L:\","M:\","N:\","O:\","P:\",_

            "Q:\","R:\","S:\","T:\","U:\","V:\","W:\","X:\","Y:\","Z:\")

  For Each strDrv In Drvs

If Fso.DriveExists(strDrv) Then

Act.Run("Defrag " & strDrv & " -F"),1,true

End If

Next

Link to comment
Share on other sites

I always recommend to clients that they purchase Raxco's PerfectDisk. Some can afford it, some can't, some just don't care, etc.. but I always try my best to make my scripts accomodating to all scenarios so that I don't have a dozen versions of the same script floating around. This VBScript checks to see if PerfectDisk is installed. If it is installed then that's what is used and also it will delete the executable and shortcut for Sysinternal's PageDefrag. So I have one script that works in all scenarios for me.

Option Explicit
Dim ws, fs, progfiles, raxco, sysint
Set ws = WScript.CreateObject("WScript.Shell")
Set fs = CreateObject("Scripting.FileSystemObject")
progfiles = ws.ExpandEnvironmentStrings ("%PROGRAMFILES%")
raxco = progfiles & "\Raxco\PerfectDisk"
sysint = progfiles & "\Utilities\Sysinternals"

'** Subroutine; PerfectDisk
Sub PerfectDisk
Dim progs, utl
progs = ws.SpecialFolders("AllUsersPrograms")
utl = progs & "\Utilities"
If fs.FileExists(utl & "\Page Defrag.lnk") Then fs.DeleteFile(utl & "\Page Defrag.lnk"), True
If fs.FileExists(sysint & "\pagedfrg.exe") Then fs.DeleteFile(sysint & "\pagedfrg.exe"), True
If fs.FileExists(sysint & "\pagedfrg.hlp") Then fs.DeleteFile(sysint & "\pagedfrg.hlp"), True
If fs.FileExists(raxco & "\PDcmd.exe") Then ws.Run("""%PROGRAMFILES%\Raxco\PerfectDisk\PDcmd.exe"" /SmartPlacement /WAIT /AllDrives"),0,True
If fs.FileExists(raxco & "\PDcmd.exe") Then ws.Run("""%PROGRAMFILES%\Raxco\PerfectDisk\PDcmd.exe"" /SCHEDBOOT /All /AllDrives"),0,True
End Sub

'** Subroutine; Windows Defrag
Sub WindowsDefrag
ws.Run("defrag %SYSTEMDRIVE% -f"),0,True
If fs.FileExists(sysint & "\pagedfrg.exe") Then ws.Run("""%PROGRAMFILES%\Utilities\Sysinternals\pagedfrg.exe"" -o"),0,True
End Sub

'** Run Tasks
If fs.FolderExists(raxco) Then
PerfectDisk
Else
WindowsDefrag
End If

Please note that there is line wrapping in this post on some of the longer lines.

Edited by RogueSpear
Link to comment
Share on other sites

Hey guys,

wow, some vbs experts out there ... :-)

I have combined rogue´s and Doug Knox´s defrag.all scripts but I am having a bit of a problem with it:

' - Defrag all hard disks -

Option Explicit
Dim ws, fs, progfiles, raxco
Set ws = WScript.CreateObject("WScript.Shell")
Set fs = CreateObject("Scripting.FileSystemObject")
progfiles = ws.ExpandEnvironmentStrings ("%PROGRAMFILES%")
raxco = progfiles & "\Raxco\PerfectDisk"

'** Subroutine; PerfectDisk
Sub PerfectDisk
If fs.FileExists(raxco & "\PDcmd.exe") Then ws.Run("""%PROGRAMFILES%\Raxco\PerfectDisk\PDcmd.exe"" /SmartPlacement /WAIT /AllDrives"),0,True
End Sub

'** Subroutine; Windows Defrag
Sub WindowsDefrag
Dim d, dc
  Set dc = fs.Drives
  For Each d in dc
     If d.DriveType = 2 Then
Return = ws.Run("defrag " & d & " -f", 1, TRUE)
     End If
  Next

End Sub

'** Run Tasks
If fs.FolderExists(raxco) Then
PerfectDisk
Else
WindowsDefrag
End If


WScript.quit

The things is that if I call this script as an scheduled task, the task wscipt.exe will not end afterwards (don´t have perfect disc installed at the moment). If I call the original vbs script by Doug it will:

'defrag_all.vbs - Defrags all hard disks - Can be run as a Scheduled Task
'© Doug Knox - 3/29/2002

Set WshShell = WScript.CreateObject("WScript.Shell")

  Dim fso, d, dc
  Set fso = CreateObject("Scripting.FileSystemObject")
  Set dc = fso.Drives
  For Each d in dc
     If d.DriveType = 2 Then
Return = WshShell.Run("defrag " & d & " -f", 1, TRUE)
     End If
  Next

Set WshShell = Nothing

Any idea why ? Also I am not sure that all drives in fact get defragmented in the first script by windows defrag (the first script ends much sooner as the second, although I did not change much ... mhh, what could that be ?!? )

Thanks for pointing out any errors, I am not very good at scripting :-)

Bye,

Alex

Link to comment
Share on other sites

is there a way to moniter what drive is being defraged with thos scripts?

cause what i was thinking is if someone was watching there computer or accidently left the moniter on wouldent it be nice to have a screensave bouncing around the words "curently defragmenting the X drive." on the screen? maybe there would be a way to make a status indacatere apper below it but i dont know if thats posible

i could code it in vb but i dont know how thos scripts would act:

are all the drives defraged at once or is it all linear meaning c drive defrags the d drive then e...

if its linear then could i just have a timer control runing the words around the screen taking from the strdrive variable?

any help on this would be nice

Link to comment
Share on other sites

Well,

if you use Perfect disc, an icon appears and tells you what PD is doing if you move the mouse over it. Also PD defrags all drives at once.

If you use Windows defrag, nothing appears, however, the script calls the cmd version of defrag which means you get a status on each drive formatted in a cmd. This of course only happens if you run the script as the same user that is logged in. I have scheduled it using the SYSTEM account and then I don´t get any visual feedback.

By the way, I think I have found the problem with the combined script I posted above. Somehow wscript did not like the word "RETURN" in line 23 saying it was not defined. It did work in the script by Doug though. Strange. dropping that and just using ws.run seems to work:

' - Defrag all hard disks -

Option Explicit
Dim ws, fs, progfiles, raxco
Set ws = WScript.CreateObject("WScript.Shell")
Set fs = CreateObject("Scripting.FileSystemObject")
progfiles = ws.ExpandEnvironmentStrings ("%PROGRAMFILES%")
raxco = progfiles & "\Raxco\PerfectDisk"

'** Subroutine; PerfectDisk
Sub PerfectDisk
If fs.FileExists(raxco & "\PDcmd.exe") Then ws.Run("""%PROGRAMFILES%\Raxco\PerfectDisk\PDcmd.exe"" /SmartPlacement /WAIT /AllDrives"),0,True
End Sub

'** Subroutine; Windows Defrag
Sub WindowsDefrag
Dim d, dc
  Set dc = fs.Drives
  For Each d in dc
     If d.DriveType = 2 Then
ws.Run("Defrag " & d & " -F"),1,true
     End If
  Next

End Sub

'** Run Tasks
If fs.FolderExists(raxco) Then
PerfectDisk
Else
WindowsDefrag
End If


WScript.quit

Link to comment
Share on other sites

PerfectDisk has a file in the install called config.ini in which you can set most if not all of the various program options ahead of the install. You then need to add CONFIG=1 to your msiexec command line statement at the time of install.

Also, in the online help for PerfectDisk is a rather complete explanation of all of the commandline options for PDcmd.exe.

Link to comment
Share on other sites

I always recommend to clients that they purchase Raxco's PerfectDisk. Some can afford it, some can't, some just don't care, etc.. but I always try my best to make my scripts accomodating to all scenarios so that I don't have a dozen versions of the same script floating around. This VBScript checks to see if PerfectDisk is installed. If it is installed then that's what is used and also it will delete the executable and shortcut for Sysinternal's PageDefrag. So I have one script that works in all scenarios for me.

Thanks for the scripts, but small problem lol.

Im a little noobish to vb coding. How do I run this? :unsure:

Edited by Grake
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
  • Recently Browsing   0 members

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