Solved

Defrag all volumes via vbs and task schedule

Posted on 2009-05-13
2
515 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

To effectively work with Diskpart on a Server Core, it is necessary to write some small batch script's, because you can't execute diskpart in a remote powershell session. To get startet, place the Diskpart batch script's into a share on your loca…
A safe way to clean winsxs folder from your windows server 2008 R2 editions
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 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…

910 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

23 Experts available now in Live!

Get 1:1 Help Now