?
Solved

Quarterly defrag

Posted on 2009-04-28
5
Medium Priority
?
406 Views
Last Modified: 2012-05-06
A few months ago, I posted a question about using a script that would run against all the in a list.  I wanted it to schedule a defrag at the end of the current work day, and continue to run every 6 months thereafter.

1. quarterly - I now need to change it to quarterly and am running into difficulty.
2. re-run script OK - I would also like to further ensure that if the script is re-run against the same machine, that it replaces the previous tasks named defrag.  I think this happens as-is but am not quite certain.
3. ignore error - Most importantly, I would like the script to continue through the list regardless of any problems that might have been experienced with one machine.  If it doesn't find something with a machine, It should continue through the list regardless.

Any help with the script pasted below is greatly appreciated.
@echo off
Set TaskRun="cmd /c defrag c: -f"
Set TimeToRun=20:00:00
Set RecurRun=monthly /mo 3 /tn "Quarterly defrag"
For /f %%a in (c:\computers.txt) do (
  schtasks /create /s %%a /ru system /sc once /tn "One time defrag" /st %TimeToRun% /tr %TaskRun%
  schtasks /create /s %%a /ru system /sc monthly /mo 3 /tn "Quarterly defrag" /st %TimeToRun% /tr %TaskRun%
)

Open in new window

0
Comment
Question by:rtod2
  • 3
  • 2
5 Comments
 
LVL 47

Expert Comment

by:Donald Stewart
ID: 24251876
I know this isnt exactly what you were asking for, but you may like it.
 
This is what I use to schedule my defrag jobs. It schedules it for the first day of every month.
The script that it calls(below) defrags all hard disks and also creates an error log that is automatically displayed at finish. I use the .BAT below in a startup script.
@ECHO OFF
if exist c:\windows\tasks\DEFRAG.job goto END
copy  /y \\server\share\defrag_all2.vbs C:\windows\system32\
at \\%computername% 20:00 /interactive /every:1 C:\windows\system32\defrag_all2.vbs
ren c:\windows\tasks\at1.job DEFRAG.job
:END

'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
 

Author Comment

by:rtod2
ID: 24252146
That's really nice!  I was trying to avoid the need for retracement though in our environment.  I just want it to ensure it completes with the parameters I have specified. Do I need to do anything to ensure that?
0
 
LVL 47

Expert Comment

by:Donald Stewart
ID: 24252258
"I was trying to avoid the need for retracement though in our environment."
 
Not sure what you mean?
0
 

Author Comment

by:rtod2
ID: 24253209
I have the first item 1 resolved.  Items 2 and 3 below remain based on the pasted script.

2. re-run script OK - I need to ensure that if the script is re-run against the same machine, that it replaces the previous tasks named defrag.  I think this happens as-is but am not quite certain.

3. ignore error - I need the script to continue through the list of machines regardless of any problems that might have been experienced with one machine.  If it doesn't find something with a machine, It should continue through the list regardless.


@echo off
Set TaskRun="cmd /c defrag c: -f"
Set TimeToRun=20:00:00
Set RecurRun=monthly /mo 3 /tn "Quarterly defrag"
For /f %%a in (c:\computers.txt) do (
  schtasks /create /s %%a /ru system /sc once /tn "One time defrag" /st %TimeToRun% /tr %TaskRun%
  schtasks /create /s %%a /ru system /sc monthly /mo 3 /tn "Quarterly defrag" /st %TimeToRun% /tr %TaskRun%
)

Open in new window

0
 
LVL 47

Accepted Solution

by:
Donald Stewart earned 1500 total points
ID: 24253920
Just tested what you have
2. doesnt replace (says already exists) so you could add these lines
del "c:\windows\tasks\One time defrag.job"
del "c:\windows\tasks\Quarterly defrag.job"
3. continues to run thru list if machine not found
 

@echo off
del "c:\windows\tasks\One time defrag.job"
del "c:\windows\tasks\Quarterly defrag.job"
Set TaskRun="cmd /c defrag c: -f"
Set TimeToRun=20:00:00
Set RecurRun=monthly /mo 3 /tn "Quarterly defrag"
For /f %%a in (c:\computers.txt) do (
  schtasks /create /s %%a /ru system /sc once /tn "One time defrag" /st %TimeToRun% /tr %TaskRun%
  schtasks /create /s %%a /ru system /sc monthly /mo 3 /tn "Quarterly defrag" /st %TimeToRun% /tr %TaskRun%
)

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This article provides a convenient collection of links to Microsoft provided Security Patches for operating systems that have reached their End of Life support cycle. Included operating systems covered by this article are Windows XP,  Windows Server…
In this modest contribution, I want to share with the IT community (especially system administrators, IT Support Engineers and IT Help Desks) about Windows crashes/hangs and how to deal with these particular problems.
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

862 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