Kaseya Community

Output Fragmented % to text file?

This question is answered

I need to figure a way to output the fragmented disk % into a text file... this VBS script is the closest thing I've got, except it seems to run kinda weird (runs once with a false report, runs again 5 minutes later with a true result). Also, it seems to require WMI Windows Installer Provider installed first. Which isn't that big of a deal if I can get this script to run cleanly.

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.DefragAnalysis(blnRecommended, objReport)
    If errResult = 0 then
        Wscript.Echo "File percent fragmentation: " & _
            objReport.FilePercentFragmentation
    End If
Next

 

Edit: Err, ya know what? The code above seems to only work for Windows 7 for some reason.. this sucks.



[edited by: aabbasi at 3:16 PM (GMT -7) on 3-19-2012] typo
Verified Answer
  • Vista / 7:

     


    for /f "delims== tokens=2" %a in ('defrag d: /a ^| find "Total fragmented space"') do @echo %a

    XP:


    for /f "tokens=8 delims= " %a in ('defrag c: /a ^| find "file fragmentation"') do @echo %a

      

     



    [edited by: Dan at 1:15 PM (GMT -7) on 3-20-2012] f
All Replies
  • Vista / 7:

     


    for /f "delims== tokens=2" %a in ('defrag d: /a ^| find "Total fragmented space"') do @echo %a

    XP:


    for /f "tokens=8 delims= " %a in ('defrag c: /a ^| find "file fragmentation"') do @echo %a

      

     



    [edited by: Dan at 1:15 PM (GMT -7) on 3-20-2012] f
  • Dan you are a God! The Windows 7 one works perfectly. However the XP one seems to output "%str:, =g%". Couldn't appreciate this anymore, thank you.



    [edited by: aabbasi at 12:25 PM (GMT -7) on 3-20-2012] typo
  • I updated my above post.  Try that for XP

  • Freakin beautiful, thanks mate!!

  • Hi aabbasi,

    I am trying to achieve the exact same thing. Would you mind posting your code?