Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: How to get the free space available in Disk and mount volumes using a command  (Read 26428 times)

0 Members and 1 Guest are viewing this topic.

Helen09122010

    Topic Starter


    Rookie

    • Experience: Beginner
    • OS: Unknown
    Hi There,

    I am unable to find a solution to get the free space available on the disk drives and mount volumes (in windows server). i am looking for a command or batch file where i can get output like this. Drive F: has several mount points. How do i get an output like this in MB. This a sample in KB

    Name                              Capacity                             Free space                     PercentFree                                       
    ----                               --------                            ---------                      -----------                                       
    C:\                            146777567232                            101463191552                   69.13 %                                                                                   
    E:\                             68713955328                            67994939392                    98.95 %                                                                                   
    Q:\                              1069253632                            1061010432                     99.23 %                                                                                   
    O:\                             68713955328                            21001535488                    30.56 %                                                                                   
    P:\                             68713955328                            68644261888                    99.90 %                                                                                   
    F:\                             68713955328                            68644233216                    99.90 %                                                                                   
    F:\D01\                      1642818691072                            19775197184                     1.20 %                                                                                     
    F:\RRep\                      68713955328                            68644286464                    99.90 %                                                                                   
    F:\D033\                      549761228800                            21194547200                     3.86 %                                                                                     
    F:\D02\                     1642818691072                            36596097024                     2.23 %                                                                                     
    F:\Bkup\                    1642818691072                            1419910320128                  86.43 %                                                                                   
    F:\D04\                     1095212449792                            4592365568                      0.42 %                                                                                     
    F:\T01\                      1095212449792                            815251521536                   74.44 %                                                                                   
    F:\TDB\                    1095212449792                            56733552640                    96.49 %   

    regards
    Helen

    Linux711



      Mentor

      Thanked: 59
      • Yes
      • Programming Blog
    • Certifications: List
    • Computer: Specs
    • Experience: Experienced
    • OS: Windows 7
    YouTube

    "Genius is persistence, not brain power." - Me

    "Insomnia is just a byproduct of, "It can't be done"" - LaVolpe

    Helen09122010

      Topic Starter


      Rookie

      • Experience: Beginner
      • OS: Unknown
      thanks very much Linux711

      Helen09122010

        Topic Starter


        Rookie

        • Experience: Beginner
        • OS: Unknown
        Hi Linux711,

        the solution found in the link does not solve the problem.

        for /f "usebackq delims== tokens=2" %%x in (`wmic logicaldisk where "DeviceID='C:'" get FreeSpace /format:value`) do set FreeSpace=%%x
        for /f "usebackq delims== tokens=2" %%x in (`wmic logicaldisk where "DeviceID='C:'" get Size /format:value`) do set Size=%%x
        set FreeMB=%FreeSpace:~0,-6%
        set SizeMB=%Size:~0,-6%
        set /a Percentage=100 * FreeMB / SizeMB
        echo C: is %Percentage% % free

        it only gives me the space avaible on a disk and not on mounted volume. Also you got to pass each drive letter as an argument to the file

        Any other help please?


        thanks

        Helen

        Helen09122010

          Topic Starter


          Rookie

          • Experience: Beginner
          • OS: Unknown
          Hey All,

          I found out the command that helps me to get volume information

          wmic Volume get Name, Capacity, FreeSpace

          Can someone help me to format it neatly and get the size in GB and get the % free

          thanks
          Helen

          foxidrive



            Specialist
          • Thanked: 268
          • Experience: Experienced
          • OS: Windows 8
          Does WMIC in that command report your mount points too?  Is that an issue?

          Batch math can only natively handle 2^31 - 1 so you will have to resort to WSH or Powershell for the math.

          Sidewinder



            Guru

            Thanked: 139
          • Experience: Familiar
          • OS: Windows 10
          Batch code does not support floating point arithmetic and numbers are limited to 32 bit precision. An alternate solution would be VBScript.

          Code: [Select]
          strComputer = "."
          Set objWMIService = GetObject("winmgmts:" _
              & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

          Set colDisks = objWMIService.ExecQuery _
              ("Select * from Win32_LogicalDisk where drivetype <> 5")

          WScript.Echo "Name  Capacity      Free  Free %"
          For each objDisk in colDisks
              percentfree = (objdisk.Freespace / objdisk.Size) * 100
              With objDisk
                WScript.Echo .DeviceId & "    " & pad(FormatNumber(.Size/1073741824, 2)) & "  " & pad(FormatNumber(.Freespace/1073741824, 2)) & "   " &  FormatNumber(percentfree, 2)
              End With
             
          Next

          function pad(strText)
              strText = Space(10) + strText
              strText = Right(strText, 8)
              pad = strText
          end function

          Save script with a VBS extension and run from the command line as: cscript //nologo scriptname.vbs

          If you have access to Powershell, that would also be a good alternate solution. 8)
          The true sign of intelligence is not knowledge but imagination.

          -- Albert Einstein

          Squashman



            Specialist
          • Thanked: 134
          • Experience: Experienced
          • OS: Other

          Hey foxidrive & sidewinder,
          Check out Judago's website for a work around on the batch math limitations.
          Judago.webs.com

          foxidrive



            Specialist
          • Thanked: 268
          • Experience: Experienced
          • OS: Windows 8
          Thanks Squashman.   What a monster that batch file is! :)

          Sidewinder



            Guru

            Thanked: 139
          • Experience: Familiar
          • OS: Windows 10
          Hey foxidrive & sidewinder,
          Check out Judago's website for a work around on the batch math limitations.
          Judago.webs.com

          Impressive piece of work. This work around now tops Letterman's Top Ten Reasons To Switch To Powershell.  :o
          The true sign of intelligence is not knowledge but imagination.

          -- Albert Einstein

          foxidrive



            Specialist
          • Thanked: 268
          • Experience: Experienced
          • OS: Windows 8
          This version has trivial changes but it handles drive arrays up to 99 TB. 

          Code: [Select]
          strComputer = "."
          Set objWMIService = GetObject("winmgmts:" _
              & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

          Set colDisks = objWMIService.ExecQuery _
              ("Select * from Win32_LogicalDisk where drivetype <> 5")

          WScript.Echo "Name   Capacity       Free   Free %"
          For each objDisk in colDisks
              percentfree = (objdisk.Freespace / objdisk.Size) * 100
              With objDisk
                WScript.Echo .DeviceId & "    " & pad(FormatNumber(.Size/1073741824, 2)) & "  " & pad(FormatNumber(.Freespace/1073741824, 2)) & "   " &  FormatNumber(percentfree, 2)
              End With
             
          Next

          function pad(strText)
              strText = Space(10) + strText
              strText = Right(strText, 9)
              pad = strText
          end function

          Helen09122010

            Topic Starter


            Rookie

            • Experience: Beginner
            • OS: Unknown
            thanks All for the answers.

            With wmic Volume i can get free space available on mount point's as well. But the issue now is iterating through a list of servers across my company's servers from my laptop.

            I am trying to combine DOS command and SQL to achieve the result. That eliminates issues with DOS command

            thanks
            Helen