Defrag vbscript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jaeden99
    New Member
    • May 2007
    • 19

    Defrag vbscript

    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.
    Code:
    Const LogFile = "C:\Defrag.log"
    
    Const DriveTypeRemovable = 1
    Const DriveTypeFixed = 2
    Const DriveTypeNetwork = 3
    Const DriveTypeCDROM = 4
    Const DriveTypeRAMDisk = 5
    
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set Drives = FSO.Drives
    
    For each Drive in Drives
     if Drive.DriveType = 2 then
      RunCmd Drive
     end if
    Next 
    
    Sub RunCmd(DriveString)
    Set WshShell = WScript.CreateObject("WScript.Shell")
    RunString = "%comspec% /c echo " & WeekDayName(WeekDay(Now), True) & " " & Now & " " & DriveString
    Return = WshShell.Run(RunString & " >> " & ComputerName & LogFile & " 2>&1", 0, TRUE)
    RunString = "%comspec% /c %WINDIR%\System32\Defrag.exe " & DriveString & " -f"
    Return = WshShell.Run(RunString & " >> " & ComputerName & LogFile & " 2>&1", 0, TRUE)
    Set WshShell = Nothing
    End Sub
    Last edited by Dököll; Jun 8 '07, 12:35 AM. Reason: Code tags
  • mailderemi
    New Member
    • Jun 2007
    • 14

    #2
    Originally posted by jaeden99
    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.
    Code:
    Const LogFile = "C:\Defrag.log"
    
    Const DriveTypeRemovable = 1
    Const DriveTypeFixed = 2
    Const DriveTypeNetwork = 3
    Const DriveTypeCDROM = 4
    Const DriveTypeRAMDisk = 5
    
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set Drives = FSO.Drives
    
    For each Drive in Drives
     if Drive.DriveType = 2 then
      RunCmd Drive
     end if
    Next 
    
    Sub RunCmd(DriveString)
    Set WshShell = WScript.CreateObject("WScript.Shell")
    RunString = "%comspec% /c echo " & WeekDayName(WeekDay(Now), True) & " " & Now & " " & DriveString
    Return = WshShell.Run(RunString & " >> " & ComputerName & LogFile & " 2>&1", 0, TRUE)
    RunString = "%comspec% /c %WINDIR%\System32\Defrag.exe " & DriveString & " -f"
    Return = WshShell.Run(RunString & " >> " & ComputerName & LogFile & " 2>&1", 0, TRUE)
    Set WshShell = Nothing
    End Sub
    this is easy:
    1.click Run in Start then
    2.Type:
    Code:
    cmd /k defrag.exe /?
    then OK it ;)

    .:!M!LDEREMi:.

    Comment

    • jaeden99
      New Member
      • May 2007
      • 19

      #3
      Originally posted by mailderemi
      this is easy:
      1.click Run in Start then
      2.Type:
      Code:
      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\de fraglog.txt
      C:\computer2\de fraglog.txt
      ..........

      Comment

      Working...