Solved

Defrag all volumes via vbs and task schedule

Posted on 2009-05-13
2
520 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
[X]
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
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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

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…
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
This tutorial will give a an overview on how to deploy remote agents in Backup Exec 2012 to new servers. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as connecting to a remote Back…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…
Suggested Courses

630 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