PDA

View Full Version : DeFrag Script



darkness
01-07-2002, 01:45 PM
Happy Holidays Everyone, I would'eve posted over break but I decided to take a rest.

I promised a few people a copy of a defrag script that someone gave me a copy of so I will post it up here:

'This script launches defrag and sends keys to the UI in order to automate the defrag
'process.

set WshShell = CreateObject("WScript.Shell")

'Launch Defrag from the command line and wait for a second
WshShell.Run "dfrg.msc"
WScript.Sleep 1000


'Wait until the application has loaded - Check every second
While WshShell.AppActivate("Disk Defragmenter") = FALSE
wscript.sleep 1000
Wend

'Bring the application to the foreground
WshShell.AppActivate "Disk Defragmenter"
WScript.Sleep 200

'Send an ALT-A key to bring down the degrag menu
WshShell.SendKeys "%A"
WScript.Sleep 200

'Send a D to start the defrag
WshShell.SendKeys "D"

'Wait until the defrag is completed - Check for window every 5 seconds
While WshShell.AppActivate("Defragmentation Complete") = FALSE
wscript.sleep 5000
Wend

'Bring the msgbox to the foreground
WshShell.AppActivate "Defragmentation Complete"
WScript.Sleep 200

'Send a tab key to move the focus from View Report button to the Close Button
WshShell.Sendkeys "{TAB}"
Wscript.Sleep 500

'Send key to Close the Defragmentation Complete window
WshShell.Sendkeys "{ENTER}"
Wscript.Sleep 500

'Send and ALT-F4 to Close the Defrag program
WshShell.Sendkeys "%{F4}"



Michael McLaughlin/images/forums/icons/smile.gif
Systems Manager
Minnesota State University Mankato

mayton82
02-16-2002, 02:09 AM
I have read several of your posts already, and was wondering if you could add onto this script. I need a script that can disable the virtual memory, and restart the machine in safe mode, then run defrag is that possible. thanks

Computer Technition,
Assistant Computer Adminisrator
Salmen High School
Louisianna

P.S. I already have half the script written to do this, I only need a way to disable virtual memory I think there is a setting in the registry I just dont know where.

darkness
02-18-2002, 04:20 PM
If you can just email the script you have and I will try to finish it up.

I also have a couple backup scripts one that Creates a ERD disk at a scheduled event and another that runs a backup on a scheduled event that I hope to be posting soon.

email: darkness@campus.mnsu.edu

Michael McLaughlin/images/forums/icons/smile.gif
Systems Manager
Minnesota State University Mankato

mizan
06-19-2002, 05:21 AM
Is this a VBS script ? how to run it?

TQ.

darkness
06-19-2002, 02:51 PM
on a windows NT, 2000, xp machine you can cut and paste the code into a .vbs file and then double click it or have the windows scheduler do it for you.

Michael McLaughlin/images/forums/icons/smile.gif
Systems Manager
Minnesota State University Mankato

rexregum
06-21-2002, 08:14 PM
Great script! But how can one edit it so it would change drives? I have a computer with 3 HD's, and would like to be able to set up task manager to run defrag on all drives at different times.

g707
09-19-2002, 11:09 AM
Did you ever get this script to change drives?
If so, how did you do it?
Thanks

*******
* g707 *
*******

Jama
09-21-2002, 08:11 AM
g707, open notepad and copy this line to it;

defrag /c: /F /noprompt

Save this as Defrag C<font color=blue>.bat</font color=blue> Next open Task Scheduler and drag the batch file to it. Double-click it and choose the day and time for it to run.
Do the same for each drive you want to defrag changing the c: to the drive letter.
e.g. to defrag your E drive, use;

defrag /e: /F /noprompt

<pre>
Defrag Command Line Arguments

Syntax defrag [drive: | /all] [/F | /U | /Q] [/noprompt] [/concise | /detailed]

Parameters

drive: Drive letter of the disk to be optimized.
/all Defragment all local, nonremovable drives.
/F Defragment files and free space.
/U Defragment files only.
/Q Defragment free space only.

/concise Display the Hide Details view (default).
/detailed Display the Show Details view.
/noprompt Unattended mode; do not stop and display confirmation messages.


/SAGESET:n Display the System Agent-Aware Setting dialog box and
store the settings in the SETn registry key.

/SAGERUN:n Runs in unattended mode using the System Agent-aware
settings in the SETn registry key.</pre>
If a program supports command line arguments, you’re better off launching it with the desired arguments than scripting it. Don’t you think?

Jama

g707
09-22-2002, 11:25 PM
Jama,
You should try reading the whole post to understand why my question was asked.

There's isn't any command-line support in W2K for defragmenting a system.

*little minds cause big problems*

*******
* g707 *
*******

Jama
09-23-2002, 06:14 PM
1- What exactly did I miss? I’ve read and reread every post in this thread and I still can’t see any mention of W2k anywhere apart from one. And in that instance it was mentioned among other OSs and it had nothing to do with Defrag, but more to do with running the script!

2- Even if W2k was mentioned and the misunderstanding was my fault -which I’m adamant, it wasn’t- how should you have dealt with it?
A) Kindly point out that the answer -which I gave in good faith – isn’t what you were looking for and optionally thank me for the effort.
Or
B) Get down to the level of a 6 year old school kid and insult me!

3- For someone with such a “great mind” as you, I’m somewhat surprised and bewildered as to why you couldn’t see the obvious answer to your own question, even though it’s staring you in the face!

In case you still can’t see it, don’t worry, for I have squeezed every ounce of intelligence from my “little mind” and came up with the answer, here it is;

Tab, Tab then Down Arrow

Dead simple. Right?

If for some mysterious reason -which I can’t possibly comprehend- you don’t know what to do with this answer, here is a ready made solution;

'================================================= ===============

Set WshShell = CreateObject("WScript.Shell")

'Launch Defrag from the command line and wait for a second
WshShell.Run "dfrg.msc"
WScript.Sleep 1000

'Wait until the application has loaded - Check every second
While WshShell.AppActivate("Disk Defragmenter") = FALSE
wscript.sleep 1000
Wend

'Bring the application to the foreground
WshShell.AppActivate "Disk Defragmenter"
WScript.Sleep 1000
'
'Send the Tab key twice followed by the down arrow once to select
'the Next drive.
WshShell.SendKeys "{TAB 2}"
WshShell.SendKeys "{DOWN 1}"
'

'Send an ALT-A key to bring down the degrag menu
WshShell.SendKeys "%A"
WScript.Sleep 200

'Send a D to start the defrag
WshShell.SendKeys "D"

'Wait until the defrag is completed - Check for window every 5 seconds
While WshShell.AppActivate("Defragmentation Complete") = False
wscript.sleep 5000
Wend

'Bring the msgbox to the foreground
WshShell.AppActivate "Defragmentation Complete"
WScript.Sleep 200

'Send a tab key to move the focus from View Report button to the Close Button
WshShell.Sendkeys "{TAB}"
Wscript.Sleep 500

'Send key to Close the Defragmentation Complete window
WshShell.Sendkeys "{ENTER}"
Wscript.Sleep 500

'Send and ALT-F4 to Close the Defrag program
WshShell.Sendkeys "%{F4}"

Set WshShell = Nothing

'================================================= ===============


Being the nice person that I am, I’ve found two other solutions for you.
The first is an alternative to the above script. It’s called AutoDeFrag.exe. It’s a tiny (50k) command line launcher that takes a single argument (the drive letter of the drive you want optimized) and runs Defrag on that drive.
Its freeware and you can download it from <a target="_blank" href=http://www.morphasys.com/autodefrag/> MorphaSys </a>
I have not used it, but it’s worth a try.

The second is a solution to a problem you didn’t even know you had, the limitations of the Windows NT/2000 defragmentation utility.
It’s called PageDefrag and it allows you to defrag the Windows Registry Hives and Paging files, which the standard defrag utility wont touch.
Its freeware and you can download it from <a target="_blank" href=http://www.sysinternals.com/ntw2k/freeware/pagedefrag.shtml> Sysinternals </a>



Jama

g707
09-24-2002, 05:12 PM
No 3rd party tools are not wanted, but a pure keyboard sequence to run the defrag instead, hmmm, in itself defeats the purpose of scripting, but in this case, I don't see any other options, do you???
Basically I work at place that doesn't want to pay for any server defrag software, but doesn't approve of free/share-ware on their servers.


*********
To be honest, you fired the 1st insult with:
If a program supports command line arguments, you’re better off launching it with the desired arguments than scripting it.
*Don’t you think?* Sounds like belittling me...

I took that shot back at you only in defense...
You did at least give an answer (which is more than most get) and next time I'll to be more precise on the question I ask.

Also, your point that this post doesn't list W2K as is because this post is a continuation of several previous defrag W2K scripting posts.

You caught me on a bad day, I guess I did the same to you.... But thanks for the help anyways.

(And I did have the same sequence as you listed on in my script. My test wokrstation wouldn't accept the down arrow sequence, as I loaded diskeeper on it and the focus doesn't set to the drives after closing the report button, it would switch to the MMC instead.)


So for those who care, here's a copy of the script
that works for me:
Purpose: To defragment multilple drives automatically under a W2K platform:

'================================================= ===============

Set WshShell = CreateObject("WScript.Shell")

'Launch Defrag from the command line and wait for a second
WshShell.Run "dfrg.msc"
WScript.Sleep 1000
'

'Wait until the application has loaded - Check every second
While WshShell.AppActivate("Disk Defragmenter") = FALSE
wscript.sleep 1000
Wend
'

'Bring the application to the foreground
WshShell.AppActivate "Disk Defragmenter"
WScript.Sleep 1000
'

'Send an ALT-A key to bring down the degrag menu
WshShell.SendKeys "%A"
WScript.Sleep 200

'Send a D to start the defrag
WshShell.SendKeys "D"
'

'Wait until the defrag is completed - Check for window every 5 seconds
While WshShell.AppActivate("Defragmentation Complete") = False
wscript.sleep 5000
Wend
'

'Bring the msgbox to the foreground
WshShell.AppActivate "Defragmentation Complete"
WScript.Sleep 200
'

'Send a tab key to move the focus from View Report button to the Close Button
WshShell.Sendkeys "{TAB}"
Wscript.Sleep 500
'

'Send key to Close the Defragmentation Complete window
WshShell.Sendkeys "{ENTER}"
Wscript.Sleep 1000
'
'Sends a tab key to highlight the drive letter area
WshShell.SendKeys "{TAB}"
Wscript.Sleep 500

'Sends a down key to changes to the next drive letter
WshShell.SendKeys "{DOWN}"
WScript.Sleep 1000
'

'Bring the application to the foreground
WshShell.AppActivate "Disk Defragmenter"
WScript.Sleep 1000
'

'Send the Tab key twice followed by the down arrow once to select
'the Next drive.
WshShell.SendKeys "{TAB}"
WScript.Sleep 1000
'

'Send an ALT-A key to bring down the degrag menu
WshShell.SendKeys "%A"
WScript.Sleep 200

'Send a D to start the defrag
WshShell.SendKeys "D"

'Wait until the defrag is completed - Check for window every 5 seconds
While WshShell.AppActivate("Defragmentation Complete") = False
wscript.sleep 5000
Wend

'Bring the msgbox to the foreground
WshShell.AppActivate "Defragmentation Complete"
WScript.Sleep 200

'Send a tab key to move the focus from View Report button to the Close Button
WshShell.Sendkeys "{TAB}"
Wscript.Sleep 500

'Send key to Close the Defragmentation Complete window
WshShell.Sendkeys "{ENTER}"
Wscript.Sleep 500

'Repeat

'Send and ALT-F4 to Close the Defrag program
WshShell.Sendkeys "%{F4}"

Set WshShell = Nothing

'================================================= ===============

*******
* g707 *
*******