Solved

Defrag all volumes via vbs and task schedule

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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

A safe way to clean winsxs folder from your windows server 2008 R2 editions
This article explains how to install and use the NTBackup utility that comes with Windows Server.
This tutorial will walk an individual through setting the global and backup job media overwrite and protection periods in Backup Exec 2012. Log onto the Backup Exec Central Administration Server. Examine the services. If all or most of them are stop…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

752 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