Finding disk space on windows

I am trying to obtain the total disk capacity and the amount of free disk space on a Windows 10 machine. I don’t see any Julia command that will do this so I thought that a Windows command would give me the required information.

julia> a = `Cmd /c dir`
`Cmd /c dir`

julia> b = readchomp(a)
" Volume in drive C is OS\r\n Volume Serial Number is 7825-EBFA\r\n\r\n Directory of C:\\Users\\jakez\\.julia\\dev\\Gantner\r\n\r\n2021-04-29  04:50 PM    <DIR>          .\r\n2021-04-29  04:50 PM    <DIR>          ..\r\n2021-04-12  06:33 PM    <DIR>          .vscode\r\n2021-04-12  06:24 PM    <DIR>          docs\r\n2021-04-20  04:03 PM    <DIR>          example\r\n2021-04-29  04:51 PM   
          1,088 LICENSE\r\n2021-05-12  11:34 AM               281 Manifest.toml\r\n2021-05-12  11:34 AM               179 Project.toml\r\n2021-04-29  04:57 PM               347 README.md\r\n2021-05-17  03:10 PM    <DIR>          src\r\n2021-05-12  11:22 AM    <DIR>          test\r\n               4 File(s)          1,895 bytes\r\n               7 Dir(s)  133,166,219,264 bytes free"      

julia> 

I am sure I can figure out a way to obtain the free disk space from this. However the Windows command

julia> a = `fsutil volume diskFree C:`
`fsutil volume diskFree C:`

julia> b = readchomp(a)
"Total free bytes        : 133,171,417,088 (124.0 GB)\r\nTotal bytes             : 510,208,765,952 (475.2 GB)\r\nTotal quota free bytes  : 133,171,417,088 (124.0 GB)"

julia> typeof(b)
SubString{String}

gives both the free disk space but also the total disk size. However it needs administrative privileges to execute. In this case I right clicked on Julia to run with administrative privileges. How can I get this to run without running Julia with elevated privileges?

Or is there a better way?

Use runas

https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2012-R2-and-2012/cc771525(v=ws.11)

When I remember correctly it will ask for password and saves it for successive runs if you use /savecred until the users password has been changed, then it will ask again. Something like that, you will need to experiment a bit.

1 Like

I didn’t want the extra bother to try and run with elevated privileges, so did another search and found another command that works. The code is shown:

"""
    function diskspace(drive::Char)
find size of disk and remaining free space
drive is one letter identifier for the windows drive
returns disksize, diskfree
"""
function diskspace(drive::Char)
    try
        if Sys.iswindows()
            # function to find vector element for the appropriate drive
            f(x) = for (i, ss) in enumerate(x)
                        # @show(i, ss)
                        ss[1:2] == "$(uppercase(drive)):" && return i
                    end

            a = `wmic logicaldisk get size, freespace, caption`  # windows shell command to read disk space
            b = readchomp(a)                                     # execute command
            c = split(b, "\r\r\n")                              # create vector of drives
            i = f(c)                                            # get appropriate element from vector
            # @show(a, b, c, i)
            d = split(c[i])                                     
            global disksize = d[2]
            global diskfree = d[3]
        else
            error("function diskspace is only implmented for windows")
        end
    catch
        error("drive letter $drive not recognized")
    end
    return disksize, diskfree
end

with the results:

julia> diskspace('c')
("132984524800", "510208765952")

“I edited the title to make it more meaningful for others”

1 Like