Schedule a defrag only when needed

R

Rojo Habe

I'm trying to schedule a task to analyse a volume and then go ahead and
defrag it if necessary (i.e. if it's fragmented past a certain threshold).
There is no command line switch for defrag.exe that will do this in one hit
so I need to write a script:

defrag <volume> /a
if [disk needs defragging] then
defrag <volume>

Unfortunately I know nothing the syntax required so I was hoping someone out
there could help me out with the middle line. I'll be happy just to test
for the text "You should defrag this volume." I'm assuming the easiest way
to perform this test would be to write the fragmentation report to a text
file, i.e. the first line would read:

defrag <volume> /a >defrag.txt

or something similar, and then the next line would be searching this file
for a specific string to determine whether or not to execute line 3.

Or am I overcomplicating things here?
 
R

Ramesh, MS-MVP

Hi Rojo,

Use this VBScript:

'- - -
Set objShell = CreateObject("Wscript.Shell")
Set objWshScriptExec = objShell.Exec("defrag.exe c: -a")
strOutput=objWshScriptExec.StdOut.ReadAll
strSearch = "You do not need to defragment this volume"
If InStr(1, strOutput, strSearch) = 0 Then
objShell.Run ("defrag.exe c:")
End If
'- - -

Copy the above contents to Notepad, and save as Dfrg.VBS. You may schedule
this script if needed.

More information: The script checks if the string "You do not need to
defragment this volume" is present in the "defrag -a" output. If the string
is not present, the script initiates a defrag (hardcoded for the C: drive).
Another method, reading the % of fragmentation level is also possible using
scripting. Let me know if you need that as well.

--
Regards,

Ramesh Srinivasan, Microsoft MVP [Windows XP Shell/User]
Windows® XP Troubleshooting http://www.winhelponline.com


I'm trying to schedule a task to analyse a volume and then go ahead and
defrag it if necessary (i.e. if it's fragmented past a certain threshold).
There is no command line switch for defrag.exe that will do this in one hit
so I need to write a script:

defrag <volume> /a
if [disk needs defragging] then
defrag <volume>

Unfortunately I know nothing the syntax required so I was hoping someone out
there could help me out with the middle line. I'll be happy just to test
for the text "You should defrag this volume." I'm assuming the easiest way
to perform this test would be to write the fragmentation report to a text
file, i.e. the first line would read:

defrag <volume> /a >defrag.txt

or something similar, and then the next line would be searching this file
for a specific string to determine whether or not to execute line 3.

Or am I overcomplicating things here?
 
R

Rojo Habe

Many thanks for your speedy reply. That works just fine. I should be able
to adapt it for percentage fragmented - it's just a matter of changing the
search string.

I've just pasted it in three times for three volumes, changing the drive
letter as appropriate. It would probably be more efficient in a For... Next
loop but I should be able to work that out myself with a little tinkering.

Many thanks once again.
 
R

Rojo Habe

.... On second thought it's not as much like VBA as I'd hoped. Can't seem to
define my variables. I'll stick with three iterations of the same code for
now! Unless someone can kindly construct a loop that runs the script for
drives C, H and F (in that order) for me?


Rojo Habe said:
Many thanks for your speedy reply. That works just fine. I should be
able to adapt it for percentage fragmented - it's just a matter of
changing the search string.

I've just pasted it in three times for three volumes, changing the drive
letter as appropriate. It would probably be more efficient in a For...
Next loop but I should be able to work that out myself with a little
tinkering.

Many thanks once again.


Ramesh said:
Hi Rojo,

Use this VBScript:

'- - -
Set objShell = CreateObject("Wscript.Shell")
Set objWshScriptExec = objShell.Exec("defrag.exe c: -a")
strOutput=objWshScriptExec.StdOut.ReadAll
strSearch = "You do not need to defragment this volume"
If InStr(1, strOutput, strSearch) = 0 Then
objShell.Run ("defrag.exe c:")
End If
'- - -

Copy the above contents to Notepad, and save as Dfrg.VBS. You may
schedule
this script if needed.

More information: The script checks if the string "You do not need to
defragment this volume" is present in the "defrag -a" output. If the
string
is not present, the script initiates a defrag (hardcoded for the C:
drive).
Another method, reading the % of fragmentation level is also possible
using
scripting. Let me know if you need that as well.

--
Regards,

Ramesh Srinivasan, Microsoft MVP [Windows XP Shell/User]
Windows® XP Troubleshooting http://www.winhelponline.com


I'm trying to schedule a task to analyse a volume and then go ahead and
defrag it if necessary (i.e. if it's fragmented past a certain
threshold).
There is no command line switch for defrag.exe that will do this in one
hit
so I need to write a script:

defrag <volume> /a
if [disk needs defragging] then
defrag <volume>

Unfortunately I know nothing the syntax required so I was hoping someone
out
there could help me out with the middle line. I'll be happy just to test
for the text "You should defrag this volume." I'm assuming the easiest
way
to perform this test would be to write the fragmentation report to a text
file, i.e. the first line would read:

defrag <volume> /a >defrag.txt

or something similar, and then the next line would be searching this file
for a specific string to determine whether or not to execute line 3.

Or am I overcomplicating things here?
 
R

Rojo Habe

Ignore my last. I persevered and finally came up with:

dim x(3)
x(1) = "c"
x(2) = "h"
x(3) = "f"

for n = 1 to 3
Set objShell = CreateObject("Wscript.Shell")
Set objWshScriptExec = objShell.Exec("defrag.exe " & x(n) & ": -a")
strOutput=objWshScriptExec.StdOut.ReadAll
strSearch = "You do not need to defragment this volume"
If InStr(1, strOutput, strSearch) = 0 Then
objShell.Run ("defrag.exe " & x(n) & ":")
End If
Next

which seems to work just fine.


Rojo Habe said:
... On second thought it's not as much like VBA as I'd hoped. Can't seem
to define my variables. I'll stick with three iterations of the same code
for now! Unless someone can kindly construct a loop that runs the script
for drives C, H and F (in that order) for me?


Rojo Habe said:
Many thanks for your speedy reply. That works just fine. I should be
able to adapt it for percentage fragmented - it's just a matter of
changing the search string.

I've just pasted it in three times for three volumes, changing the drive
letter as appropriate. It would probably be more efficient in a For...
Next loop but I should be able to work that out myself with a little
tinkering.

Many thanks once again.


Ramesh said:
Hi Rojo,

Use this VBScript:

'- - -
Set objShell = CreateObject("Wscript.Shell")
Set objWshScriptExec = objShell.Exec("defrag.exe c: -a")
strOutput=objWshScriptExec.StdOut.ReadAll
strSearch = "You do not need to defragment this volume"
If InStr(1, strOutput, strSearch) = 0 Then
objShell.Run ("defrag.exe c:")
End If
'- - -

Copy the above contents to Notepad, and save as Dfrg.VBS. You may
schedule
this script if needed.

More information: The script checks if the string "You do not need to
defragment this volume" is present in the "defrag -a" output. If the
string
is not present, the script initiates a defrag (hardcoded for the C:
drive).
Another method, reading the % of fragmentation level is also possible
using
scripting. Let me know if you need that as well.

--
Regards,

Ramesh Srinivasan, Microsoft MVP [Windows XP Shell/User]
Windows® XP Troubleshooting http://www.winhelponline.com


I'm trying to schedule a task to analyse a volume and then go ahead and
defrag it if necessary (i.e. if it's fragmented past a certain
threshold).
There is no command line switch for defrag.exe that will do this in one
hit
so I need to write a script:

defrag <volume> /a
if [disk needs defragging] then
defrag <volume>

Unfortunately I know nothing the syntax required so I was hoping someone
out
there could help me out with the middle line. I'll be happy just to
test
for the text "You should defrag this volume." I'm assuming the easiest
way
to perform this test would be to write the fragmentation report to a
text
file, i.e. the first line would read:

defrag <volume> /a >defrag.txt

or something similar, and then the next line would be searching this
file
for a specific string to determine whether or not to execute line 3.

Or am I overcomplicating things here?
 
R

Ramesh, MS-MVP

Nice work, Rojo. Glad it worked out well for you.

--
Regards,

Ramesh Srinivasan, Microsoft MVP [Windows XP Shell/User]
Windows® XP Troubleshooting http://www.winhelponline.com


Ignore my last. I persevered and finally came up with:

dim x(3)
x(1) = "c"
x(2) = "h"
x(3) = "f"

for n = 1 to 3
Set objShell = CreateObject("Wscript.Shell")
Set objWshScriptExec = objShell.Exec("defrag.exe " & x(n) & ": -a")
strOutput=objWshScriptExec.StdOut.ReadAll
strSearch = "You do not need to defragment this volume"
If InStr(1, strOutput, strSearch) = 0 Then
objShell.Run ("defrag.exe " & x(n) & ":")
End If
Next

which seems to work just fine.
 
R

Ramesh, MS-MVP

I've written another script which returns the collection of fixed drive
letters, which itself is an array.

How to schedule a defrag so that it runs only when needed:
http://www.winhelponline.com/articl...-defrag-so-that-it-runs-only-when-needed.html

This method should suit any user.

--
Regards,

Ramesh Srinivasan, Microsoft MVP [Windows XP Shell/User]
Windows® XP Troubleshooting http://www.winhelponline.com


.... On second thought it's not as much like VBA as I'd hoped. Can't seem to
define my variables. I'll stick with three iterations of the same code for
now! Unless someone can kindly construct a loop that runs the script for
drives C, H and F (in that order) for me?


Rojo Habe said:
Many thanks for your speedy reply. That works just fine. I should be
able to adapt it for percentage fragmented - it's just a matter of
changing the search string.

I've just pasted it in three times for three volumes, changing the drive
letter as appropriate. It would probably be more efficient in a For...
Next loop but I should be able to work that out myself with a little
tinkering.

Many thanks once again.


Ramesh said:
Hi Rojo,

Use this VBScript:

'- - -
Set objShell = CreateObject("Wscript.Shell")
Set objWshScriptExec = objShell.Exec("defrag.exe c: -a")
strOutput=objWshScriptExec.StdOut.ReadAll
strSearch = "You do not need to defragment this volume"
If InStr(1, strOutput, strSearch) = 0 Then
objShell.Run ("defrag.exe c:")
End If
'- - -

Copy the above contents to Notepad, and save as Dfrg.VBS. You may
schedule
this script if needed.

More information: The script checks if the string "You do not need to
defragment this volume" is present in the "defrag -a" output. If the
string
is not present, the script initiates a defrag (hardcoded for the C:
drive).
Another method, reading the % of fragmentation level is also possible
using
scripting. Let me know if you need that as well.

--
Regards,

Ramesh Srinivasan, Microsoft MVP [Windows XP Shell/User]
Windows® XP Troubleshooting http://www.winhelponline.com


I'm trying to schedule a task to analyse a volume and then go ahead and
defrag it if necessary (i.e. if it's fragmented past a certain
threshold).
There is no command line switch for defrag.exe that will do this in one
hit
so I need to write a script:

defrag <volume> /a
if [disk needs defragging] then
defrag <volume>

Unfortunately I know nothing the syntax required so I was hoping someone
out
there could help me out with the middle line. I'll be happy just to test
for the text "You should defrag this volume." I'm assuming the easiest
way
to perform this test would be to write the fragmentation report to a text
file, i.e. the first line would read:

defrag <volume> /a >defrag.txt

or something similar, and then the next line would be searching this file
for a specific string to determine whether or not to execute line 3.

Or am I overcomplicating things here?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top