Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

VB script

Posted on 2009-07-06
4
447 Views
Last Modified: 2012-05-07
I am trying to defrag all drives on a windows 2003 server box. When I try to run the script below it does not work. Can anyone recomend a new script or how to fix this one?
'Defrags all drives
 
Const LogFile = "f:\log\Defrag.log"
 
Const DriveTypeRemovable = 1
Const DriveTypeFixed = 2
Const DriveTypeNetwork = 3
Const DriveTypeCDROM = 4
Const DriveTypeRAMDisk = 5
 
Set FSO = CreateObject("Scripting.FileSystemObject")
Set Drives = FSO.Drives
 
For each Drive in Drives
 if Drive.DriveType = 2 then
  RunCmd Drive
 end if
Next
 
Sub RunCmd(DriveString)
Set WshShell = WScript.CreateObject("WScript.Shell")
RunString = "%comspec% /c echo " & WeekDayName(WeekDay(Now), True) & " " & Now & " " & DriveString
Return = WshShell.Run(RunString & " >> " & LogFile & " 2>&1", 0, TRUE)
RunString = "%comspec% /c %WINDIR%\System32\Defrag.exe " & DriveString & " -f"
Return = WshShell.Run(RunString & " >> " & LogFile & " 2>&1", 0, TRUE)
Set WshShell = Nothing
End Sub

Open in new window

0
Comment
Question by:ipcipher
  • 2
  • 2
4 Comments
 
LVL 47

Accepted Solution

by:
Donald Stewart earned 500 total points
ID: 24791693
I use the second script on this page with great success

http://www.dougknox.com/utility/scripts_desc/defrag_all.htm

'defrag_all2.vbs
'Defrags all hard disks - Can be run as a Scheduled Task
'Modified to create an error log and display it
'© Doug Knox - 4/13/2002
 
Option Explicit
 
Dim WshShell, fso, d, dc, ErrStr(), Return, X, A(), MyFile, I, MyBox, Drive
 
Set WshShell = WScript.CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
X = 0
 
   Set dc = fso.Drives
For Each d in DC 
	If d.DriveType = 2 Then
	   X = X + 1
 
'Determine drive letter of first fixed disk
'This is the drive that the error report will be placed on
		If X = 1 Then
		   Drive = d
		End If
	End If
Next
 
ReDim A(X)
ReDim ErrStr(X)
 
X = 0
For Each d in dc
      If d.DriveType = 2 Then
      X = X + 1
      Return = WshShell.Run("defrag " & d & " -f", 1, TRUE)
 
'Determine the Error code returned by Defrag for the current drive and save it
If return = 0 then
  ErrStr(x) = ErrStr(x) &  "Drive " & d & " Defrag completed successfully" & vbCRLF
elseif return = 1 then
  ErrStr(x) = ErrStr(x) &  "Drive " & d & " Defrag aborted with error level " & return & " (defrag was cancelled manually) " & vbCRLF
elseif return = 2 then
  ErrStr(x) = ErrStr(x) &  "Drive " & d & " Defrag aborted with error level " & return & " (there was a command line error. Check your command line for valid switches and drives)" & vbCRLF
elseif return = 3 then
  ErrStr(x) = ErrStr(x) &  "Drive " & d & " Defrag aborted with error level " & return & " (there was an unknown error)" & vbCRLF
elseif return = 4 then
  ErrStr(x) = ErrStr(x) &  "Drive " & d & " Defrag aborted with error level " & return & " (defrag could not run due to insufficient memory resources)" & vbCRLF
  'errorlevel 5 is not currently used
elseif return = 5 then
  ErrStr(x) = ErrStr(x) &  "Drive " & d & " Defrag aborted with error level " & return & " (general error)" & vbCRLF
elseif return = 6 then
  ErrStr(x) = ErrStr(x) &  "Drive " & d & " Defrag aborted with error level " & return & "(System error: either the account used to run defrag is not an administrator, there is a problem loading the resource DLL, or a defrag engine could not be accessed. Check for proper user permissions and run Sfc.exe to validate system files)" & vbCRLF
elseif return = 7 then
  ErrStr(x) = ErrStr(x) &  "Drive " & d & " Defrag aborted with error level " & return & " (There is not enough free space on the drive. Defrag needs 15% free space to run on a volume)" & vbCRLF
else
  ErrStr(x) = ErrStr(x) &  "Drive " & d & " Defrag aborted with an unknown error level: " & return & vbCRLF
end if
 
       End If
   Next
 
'Create the Error Report in the root of the first fixed disk.
Set MyFile = fso.OpenTextFile(Drive & "\defragreport.txt", 2, True)
MyFile.WriteLine(Date) & vbCRLF
MyFile.WriteLine(Time) & vbCRLF
   For I = 1 to X
      MyFile.WriteLine(ErrStr(I))
   Next
   MyFile.Close
 
Return = WshShell.Run(Drive & "\defragreport.txt",3,True)
 
Set WshShell = Nothing
Set fso = Nothing

Open in new window

0
 
LVL 1

Author Closing Comment

by:ipcipher
ID: 31600130
Hey thanks! Your script works good. Do you know any good scripting editors?
0
 
LVL 47

Expert Comment

by:Donald Stewart
ID: 24798467
0
 
LVL 1

Author Comment

by:ipcipher
ID: 24798776
Thank you for your help..... I wish I could give you more points..... Keep up the good work.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to install USMT 11 120
OPINIONS please : best Active Directory Monitoring tool 5 159
MS Endpoint Protection 2 44
server crashed 2 48
Many admins will agree: WSUS is is a nice invention but using it on the client side when updating a newly installed computer is still time consuming as you have to do several reboots and furthermore, the procedure of installing updates, rebooting an…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

837 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