Solved

Defrag all volumes via vbs and task schedule

Posted on 2009-05-13
2
514 Views
Last Modified: 2012-05-06
I want to be able to use the code below to set a vbs to run in task scheduler to defrag all volumes regardless if the volume needs it or not. Will it work? Or is there other ways scripts to do it? I do not want to buy a third party product.
I have tested this seems to be working. I just need to verify that it defrags regardless if drive needs it or not.
strComputer = "."

Set objWMIService = GetObject("winmgmts:" _

    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

 

Set colVolumes = objWMIService.ExecQuery("Select * from Win32_Volume")

 

For Each objVolume in colVolumes

     errResult = objVolume.Defrag()

Next

Open in new window

0
Comment
Question by:timothydoerr
2 Comments
 
LVL 38

Accepted Solution

by:
Shift-3 earned 500 total points
ID: 24375536
You can do this more easily with a batch script.

Paste the script below into a text file with a .cmd extension.  Running it will defrag all local drives.


@echo off
 

for /F "tokens=1" %%G in ('wmic logicaldisk get deviceid^,drivetype ^| find "3"') do (

 defrag %%G -f

)

Open in new window

0
 

Author Comment

by:timothydoerr
ID: 24376620
I have tried (Shift-3) batch file, didnt see it doing anything, changed echo to on and its just scrolling for for /F "tokens=1" %%G in ('wmic logicaldisk get deviceid^,drivetype ^| find "3"') do (
 defrag %%G -f
)
0

Featured Post

Do email signature updates give you a headache?

Do you feel like all of your time is spent managing email signatures? Too busy to visit every user’s desk to make updates? Want high-quality HTML signatures on all devices, including on mobiles and Macs? Then, let Exclaimer solve all your email signature problems today!

Join & Write a Comment

Normally after a failure of Domain Controller, when promoting new DC the DC is renamed, we will discuss the options in Dcpromo to re-create the DC with the same name. Scenario: You are a small IT shop with two Domain Controllers (Domain Contr…
You might have come across a situation when you have Exchange 2013 server in two different sites (Production and DR). After adding the Database copy in ECP console it displays Database copy status unknown for the DR exchange server. Issue is strange…
To efficiently enable the rotation of USB drives for backups, storage pools need to be created. This way no matter which USB drive is installed, the backups will successfully write without any administrative intervention. Multiple USB devices need t…
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now