# Finding disk space on windows

**URL:** https://discourse.julialang.org/t/finding-disk-space-on-windows/62004
**Category:** General Usage
**Created:** [May 28, 2021, 4:27pm UTC](https://discourse.julialang.org/t/finding-disk-space-on-windows/62004 "2021-05-28T16:27:10Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Jake](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jake/32/46007_2.png) [@Jake](https://discourse.julialang.org/u/Jake)
#### Post date: [May 28, 2021, 4:27pm UTC](https://discourse.julialang.org/t/finding-disk-space-on-windows/62004/1 "2021-05-28T16:27:10Z")

</div>

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
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
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?

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [May 28, 2021, 5:38pm UTC](https://discourse.julialang.org/t/finding-disk-space-on-windows/62004/2 "2021-05-28T17:38:27Z")

</div>

Use `runas`

> **[Runas](https://learn.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.

---

<div class="post-metadata">

### Author: ![Jake](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jake/32/46007_2.png) [@Jake](https://discourse.julialang.org/u/Jake)
#### Post date: [May 28, 2021, 10:35pm UTC](https://discourse.julialang.org/t/finding-disk-space-on-windows/62004/3 "2021-05-28T22:35:55Z")

</div>

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:

```julia
"""
    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
julia> diskspace('c')
("132984524800", "510208765952")

```

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