Finding disk space on windows

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