By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,846 Members | 1,631 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,846 IT Pros & Developers. It's quick & easy.

Defrag vbscript

P: 20
I have found a vbscript on the internet that I would like to add some additional info to:

See below. I want to be able to get and save the computer name to each defrag log on a specified server in a specified folder...I've add computerName to the code below. Can someone tell me if I'm on the right track?

Also would like to be able to save time started and finished of each defrag run.
Expand|Select|Wrap|Line Numbers
  1. Const LogFile = "C:\Defrag.log"
  2.  
  3. Const DriveTypeRemovable = 1
  4. Const DriveTypeFixed = 2
  5. Const DriveTypeNetwork = 3
  6. Const DriveTypeCDROM = 4
  7. Const DriveTypeRAMDisk = 5
  8.  
  9. Set FSO = CreateObject("Scripting.FileSystemObject")
  10. Set Drives = FSO.Drives
  11.  
  12. For each Drive in Drives
  13.  if Drive.DriveType = 2 then
  14.   RunCmd Drive
  15.  end if
  16. Next 
  17.  
  18. Sub RunCmd(DriveString)
  19. Set WshShell = WScript.CreateObject("WScript.Shell")
  20. RunString = "%comspec% /c echo " & WeekDayName(WeekDay(Now), True) & " " & Now & " " & DriveString
  21. Return = WshShell.Run(RunString & " >> " & ComputerName & LogFile & " 2>&1", 0, TRUE)
  22. RunString = "%comspec% /c %WINDIR%\System32\Defrag.exe " & DriveString & " -f"
  23. Return = WshShell.Run(RunString & " >> " & ComputerName & LogFile & " 2>&1", 0, TRUE)
  24. Set WshShell = Nothing
  25. End Sub
  26.  
Jun 7 '07 #1
Share this Question
Share on Google+
2 Replies


P: 14
I have found a vbscript on the internet that I would like to add some additional info to:

See below. I want to be able to get and save the computer name to each defrag log on a specified server in a specified folder...I've add computerName to the code below. Can someone tell me if I'm on the right track?

Also would like to be able to save time started and finished of each defrag run.
Expand|Select|Wrap|Line Numbers
  1. Const LogFile = "C:\Defrag.log"
  2.  
  3. Const DriveTypeRemovable = 1
  4. Const DriveTypeFixed = 2
  5. Const DriveTypeNetwork = 3
  6. Const DriveTypeCDROM = 4
  7. Const DriveTypeRAMDisk = 5
  8.  
  9. Set FSO = CreateObject("Scripting.FileSystemObject")
  10. Set Drives = FSO.Drives
  11.  
  12. For each Drive in Drives
  13.  if Drive.DriveType = 2 then
  14.   RunCmd Drive
  15.  end if
  16. Next 
  17.  
  18. Sub RunCmd(DriveString)
  19. Set WshShell = WScript.CreateObject("WScript.Shell")
  20. RunString = "%comspec% /c echo " & WeekDayName(WeekDay(Now), True) & " " & Now & " " & DriveString
  21. Return = WshShell.Run(RunString & " >> " & ComputerName & LogFile & " 2>&1", 0, TRUE)
  22. RunString = "%comspec% /c %WINDIR%\System32\Defrag.exe " & DriveString & " -f"
  23. Return = WshShell.Run(RunString & " >> " & ComputerName & LogFile & " 2>&1", 0, TRUE)
  24. Set WshShell = Nothing
  25. End Sub
  26.  
this is easy:
1.click Run in Start then
2.Type:
Expand|Select|Wrap|Line Numbers
  1. cmd /k defrag.exe /?
then OK it ;)

.:!M!LDEREMi:.
Jun 8 '07 #2

P: 20
this is easy:
1.click Run in Start then
2.Type:
Expand|Select|Wrap|Line Numbers
  1. cmd /k defrag.exe /?
then OK it ;)

.:!M!LDEREMi:.
I know how to run it. The question was how do you change the logname to represent each computer's harddrive that you defrag.

For example I am running this for 80 computers ...


C:\computer1\defraglog.txt
C:\computer2\defraglog.txt
..........
Jun 8 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.