I_Ridanovic
I_Ridanovic

Reputation: 149

Windows volume get size, free space

I need to get total space and used space of a SAN volume in Windows. There are too many volumes on the server to mount using the Windows letter mount points. Instead the server software mounts all available volumes in C:\Volumes path.

wmic logicaldisk get volumename, size, freespace

is fast and works great, but it only lists the volumes mounted as Windows letters.

dir \s \a C:\Volumes\SomeVolumeName

returns the information I need, but it's very slow as it recurses through all the files. I have hundreds of thousands of files on each volume.

du C:\Volumes\SomeVolumeName

is slow for the same reason as the dir command.

Is there any other way to get fast stats on a volume from the Windows command line or even better from Python?

Upvotes: 0

Views: 2741

Answers (3)

David Lopes
David Lopes

Reputation: 559

in this .bat script you can catch the volume free space in GB and compare it, you can to catch the total volume. try this:

@echo off & setlocal ENABLEDELAYEDEXPANSION
for /f "tokens=2,3,4" %%a in ('echo list volume ^| diskpart') do (
::echo o volume %%b
::fsutil volume diskfree %%b:
set result=E
set size=0
set free=0
FOR /f "tokens=1,2*delims=:" %%i IN ('fsutil volume diskfree %%b:') DO (
::echo Dtotal "%%i"
    if "%%i" == "Total free bytes        " ( 
        FOR /f "tokens=1,2delims=(" %%m in ('echo %%j') do (
            ::echo splittotal %%i : %%n
            FOR /f "tokens=1delims=," %%k in ('echo %%n') do (
                ::echo splittotalGB %%i :%%k
                set free=%%k
            )
        )
        set result=A
    )
    if "%%i" == "Total bytes             " (
        FOR /f "tokens=1,2delims=(" %%m in ('echo %%j') do (
            ::echo splittotal %%i : %%n
            FOR /f "tokens=1delims=," %%k in ('echo %%n') do (
                ::echo splittotalGB %%i :%%k
                set size=%%k
            )
        )
    )
)
if "!result!" == "A" (
    echo result !size!x!free!
    set /a nfree=!free!*1
    set /a min=40
    if !free! lss !min! (
        echo ok
        set MSG=The partition %%b on server_complete_name have only !free! from total !size!
    )
)
)

Upvotes: 0

Venkatakrishnan
Venkatakrishnan

Reputation: 786

You can get the requried data from the below script. Enter all the volumes in your system in the array $DriveLetter and it doesn't take much time to execute

$DriveLetter="C,D"
$DriveLetters=$DriveLetter.Split(',')
foreach ($item in $DriveLetters)
{
$data=Get-PSDrive $item | Select-Object Used,Free
$UsedData=$data.Used
$FreeSPace=$data.Free
"Used Space in Drive $item : $UsedData"
"Free space in Drive $item : $FreeSPace"
}

Hope it HElps.

Upvotes: 0

Loïc
Loïc

Reputation: 11943

I think you are looking for psutil

psutil (python system and process utilities) is a cross-platform library for retrieving information on running processes and system utilization (CPU, memory, disks, network) in Python. It is useful mainly for system monitoring, profiling and limiting process resources and management of running processes. It implements many functionalities offered by command line tools such as: ps, top, lsof, netstat, ifconfig, who, df, kill, free, nice, ionice, iostat, iotop, uptime, pidof, tty, taskset, pmap. It currently supports Linux, Windows, OSX, Sun Solaris, FreeBSD, OpenBSD and NetBSD, both 32-bit and 64-bit architectures, with Python versions from 2.6 to 3.5 (users of Python 2.4 and 2.5 may use 2.1.3 version). PyPy is also known to work.

https://pythonhosted.org/psutil/

And for you, particulary this part of the documentation : https://pythonhosted.org/psutil/#disks

Upvotes: 1

Related Questions