Defrag all harddrives with simpler code!

Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
9 posts Page 1 of 1
Contributors
User avatar
Proprogrammer
VIP - Donator
VIP - Donator
Posts: 415
Joined: Sun Oct 03, 2010 11:14 pm

What will i need?
1 button

put this code into the click event basicly this will defrag every drive on a computer, no more is to be said!
Code: Select all
Set WshShell = WScript.CreateObject("WScript.Shell")

   Dim fso, d, dc
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set dc = fso.Drives
   For Each d in dc
      If d.DriveType = 2 Then
   Return = WshShell.Run("defrag " & d & " -f", 1, TRUE)
      End If
   Next

Set WshShell = Nothing
Or you can save it as a .vbs if you dont want to make a actual program out of it.
JUST CALLS APON THE WINDOWS DEFRAGGER!
------------------------------------------------------------------------------
Proprogrammer, not just a Programmer.
User avatar
lesan101
Top Poster
Top Poster
Posts: 193
Joined: Tue Jun 29, 2010 8:10 am

hmm what does that mean? defrag?
Image
User avatar
Proprogrammer
VIP - Donator
VIP - Donator
Posts: 415
Joined: Sun Oct 03, 2010 11:14 pm

well what this does is compress and reorders your files to make your computer run faster and use up less space.
------------------------------------------------------------------------------
Proprogrammer, not just a Programmer.
User avatar
lesan101
Top Poster
Top Poster
Posts: 193
Joined: Tue Jun 29, 2010 8:10 am

ohh nice nice. : )kk thanks.
Image
User avatar
CodenStuff
Site Admin
Site Admin
Posts: 4392
Joined: Tue Aug 04, 2009 1:47 am

Nice one Proprogrammer this could be used as part of a computer maintenance application or something cooll;
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
User avatar
Usman55
VIP - Site Partner
VIP - Site Partner
Posts: 2821
Joined: Thu Dec 24, 2009 7:52 pm

CodenStuff wrote:
Nice one Proprogrammer this could be used as part of a computer maintenance application or something cooll;
I agree with that. But still it can be a stand'alone application which works very fast!
Image
User avatar
Usman55
VIP - Site Partner
VIP - Site Partner
Posts: 2821
Joined: Thu Dec 24, 2009 7:52 pm

Sorry, but heres what I get:

Error 1 Name 'WshShell' is not declared. C:\Documents and Settings\Administrator\Local Settings\Application Data\Temporary Projects\ObsprO Defragmenter\Form1.vb 8 9 ObsprO Defragmenter
Error 2 Name 'WScript' is not declared. C:\Documents and Settings\Administrator\Local Settings\Application Data\Temporary Projects\ObsprO Defragmenter\Form1.vb 8 20 ObsprO Defragmenter
Error 3 'Return' statement in a Sub or a Set cannot return a value. C:\Documents and Settings\Administrator\Local Settings\Application Data\Temporary Projects\ObsprO Defragmenter\Form1.vb 15 4 ObsprO Defragmenter
Error 4 Expression expected. C:\Documents and Settings\Administrator\Local Settings\Application Data\Temporary Projects\ObsprO Defragmenter\Form1.vb 15 11 ObsprO Defragmenter
Error 5 Name 'WshShell' is not declared. C:\Documents and Settings\Administrator\Local Settings\Application Data\Temporary Projects\ObsprO Defragmenter\Form1.vb 19 9 ObsprO Defragmenter

And this the total code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
WshShell = WScript.CreateObject("WScript.Shell")

Dim fso, d, dc
fso = CreateObject("Scripting.FileSystemObject")
dc = fso.Drives
For Each d In dc
If d.DriveType = 2 Then
Return = WshShell.Run("defrag " & d & " -f", 1, TRUE)
End If
Next

WshShell = Nothing
End Sub
Image
User avatar
Proprogrammer
VIP - Donator
VIP - Donator
Posts: 415
Joined: Sun Oct 03, 2010 11:14 pm

I dont know why that happend maybe it is just for certain Windows systems or something, i dont know.
------------------------------------------------------------------------------
Proprogrammer, not just a Programmer.
User avatar
Axel
Coding God
Coding God
Posts: 1928
Joined: Sun Jun 27, 2010 9:15 pm

but... then its just using the system's defrag software wich is actually pointless lol
http://vagex.com/?ref=25000
9 posts Page 1 of 1
Return to “Tutorials”