137

Is there a way to get the amount of free diskspace of a disk or a folder in a CMD without having to install some thirdparty applications?

I have a CMD that copies a big file to a given directory and could of course use the errorlevel return from the copy command, but then I have to wait for the time it takes to copy the file (eg...to that then the disk is full and the copy operation fails).

I would like to know before I start the copy if it is any idea at all. Tried the DU.EXE utility from Sysinternals, but that show occupied space only.

Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
  • i have to assume the "third party install" rather means "any extra software install". that the practical aspect of neither needing to provide nor to install anything. only on-board means. – Alexander Stohr Mar 16 '20 at 17:11

9 Answers9

174

If you run "dir c:\", the last line will give you the free disk space.

Edit: Better solution: "fsutil volume diskfree c:"

Nico
  • 13,320
  • 6
  • 32
  • 33
  • 2
    And it also works with mountpoints, which isn't case with dir! – LogicDaemon Oct 06 '14 at 14:56
  • 3
    Dir works well, but note you'll have to create a dummy file if your drive is empty. Else, it doesn't give the drive free space (<3 Ms :) – Balmipour Apr 02 '16 at 16:12
  • is there a way to get just the number of free bytes ? – Mina Jacob Jun 08 '16 at 03:36
  • Also, except for the root dir, `dir ..*` (or `dir c:\thing\..*`) will show a very short listing, whether or not there are zero or many files in the directory. – azhrei Jan 17 '17 at 02:17
  • fsutil way may not work for virtual drives like IMDisk RAM Drive. You're going to get: Error: Incorrect function. The FSUTIL utility requires a local NTFS volume. – user2846246 Apr 25 '17 at 08:20
  • 1
    FSUTIL will not work on machines with some access restriction policy applied. (even if 'dir' will do the job - sort of.) – Alexander Stohr Mar 16 '20 at 17:00
  • 1
    The fsutil solution returns `Error: Access is denied.` unless I run it with elevated privileges. – Palec Jul 25 '22 at 10:56
61

A possible solution:

dir|find "bytes free"

a more "advanced solution", for Windows Xp and beyond:

wmic /node:"%COMPUTERNAME%" LogicalDisk Where DriveType="3" Get DeviceID,FreeSpace|find /I "c:"

The Windows Management Instrumentation Command-line (WMIC) tool (Wmic.exe) can gather vast amounts of information about about a Windows Server 2003 as well as Windows XP or Vista. The tool accesses the underlying hardware by using Windows Management Instrumentation (WMI). Not for Windows 2000.


As noted by Alexander Stohr in the comments:

  • WMIC can see policy based restrictions as well. (even if 'dir' will still do the job),
  • 'dir' is locale dependent.
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 5
    +1 for WMI. Should be the only stable solution. Relying on a specific language (for find) is probably a bad idea :) – Joey Mar 17 '09 at 00:37
  • To put the free space into a variable one could use the following: `@FOR /F "tokens=2 delims==" %%S IN ('wmic /NODE:"%COMPUTERNAME%" LogicalDisk Where ^(DriveType^="3" and DeviceID^="%some_folder:~0,2%"^) Get FreeSpace /VALUE') DO @SET freespace=%%S` – Davor Josipovic Sep 15 '13 at 09:32
  • @Joey, What's wrong with `fsutil` as mentioned in the other answer? – Pacerier Apr 23 '15 at 17:39
  • 4
    @Pacerier: I think I remember `fsutil` needing Administrator privileges to even run, regardless of what you're doing with it. – Joey Apr 24 '15 at 08:16
  • @Joey, Odd, `fsutil volume diskfree c:` works fine for me (win 8.1) even on guest account. Which system are you using? – Pacerier May 24 '15 at 22:31
  • @VonC, My system is reporting `435,809,374,208` for wmic, `435,809,333,248` for dir, and `435,809,529,856` for fsutil. What's the reason for the difference of in the numbers reported and which would be the accurate one? – Pacerier May 24 '15 at 22:31
  • @Pacerier 6+ years later, I am not too sure, I'll look it up. – VonC May 24 '15 at 22:34
  • Would there be anyway to turn the output of this into GBs? – techguy1029 Apr 03 '19 at 22:42
  • 1
    @techguy1029 Yes: https://superuser.com/a/924992/141 or https://superuser.com/a/896771/141 – VonC Apr 04 '19 at 04:54
  • 1
    WMIC can see policy based restrictions as well. (even if 'dir' will still do the job.) – Alexander Stohr Mar 16 '20 at 17:02
  • 'dir' is locale dependent. – Alexander Stohr Mar 16 '20 at 17:02
  • 1
    @AlexanderStohr Good points. I have included your comment in the answer for more visibility. – VonC Mar 16 '20 at 17:09
17

Using this command you can find all partitions, size & free space: wmic logicaldisk get size, freespace, caption

Andronicus
  • 25,419
  • 17
  • 47
  • 88
Dewsri De Mel
  • 171
  • 1
  • 3
10

You can avoid the commas by using /-C on the DIR command.

FOR /F "usebackq tokens=3" %%s IN (`DIR C:\ /-C /-O /W`) DO (
    SET FREE_SPACE=%%s
)
ECHO FREE_SPACE is %FREE_SPACE%

If you want to compare the available space to the space needed, you could do something like the following. I specified the number with thousands separator, then removed them. It is difficult to grasp the number without commas. The SET /A is nice, but it stops working with large numbers.

SET EXITCODE=0
SET NEEDED=100,000,000
SET NEEDED=%NEEDED:,=%

IF %FREE_SPACE% LSS %NEEDED% (
    ECHO Not enough.
    SET EXITCODE=1
)
EXIT /B %EXITCODE%

UPDATE:

Much has changed since 2014. Here is a better answer. It uses PowerShell which is available on all currently supported Microsoft Windows systems.

The code below would be much clearer and easier to understand if the script were written in PowerShell without using cmd.exe as a wrapper. If you are using PowerShell Core, change powershell to pwsh.

SET "NEEDED=100,000,000"
SET "NEEDED=%NEEDED:,=%"

powershell -NoLogo -NoProfile -Command ^
    $Free = (Get-PSDrive -Name 'C').Free; ^
    if ($Free -lt [int64]%NEEDED%) { exit $true } else { exit $false }
IF ERRORLEVEL 1 (
    ECHO "Not enough disk space available."
) else (
    ECHO "Available disk space is adequate."
)
lit
  • 14,456
  • 10
  • 65
  • 119
  • Great answer. To make it work I had to add parenthesis around the "values": ` SET EXITCODE=0 SET NEEDED=100,000,000` ` SET NEEDED=%NEEDED:,=%` ` IF (%FREE_SPACE%) LSS (%NEEDED%) (` ` ECHO Not enough free space. Required: %NEEDED%` ` SET EXITCODE=1` ` ) ELSE (` ` ECHO Enough free space. Required: %NEEDED%` ` SET EXITCODE=0` ` )` ` EXIT /B %EXITCODE%` – swissben Nov 05 '21 at 12:43
8

df.exe

Shows all your disks; total, used and free capacity. You can alter the output by various command-line options.

You can get it from http://www.paulsadowski.com/WSH/cmdprogs.htm, http://unxutils.sourceforge.net/ or somewhere else. It's a standard unix-util like du.

df -h will show all your drive's used and available disk space. For example:

M:\>df -h
Filesystem      Size  Used Avail Use% Mounted on
C:/cygwin/bin   932G   78G  855G   9% /usr/bin
C:/cygwin/lib   932G   78G  855G   9% /usr/lib
C:/cygwin       932G   78G  855G   9% /
C:              932G   78G  855G   9% /cygdrive/c
E:              1.9T  1.3T  621G  67% /cygdrive/e
F:              1.9T  201G  1.7T  11% /cygdrive/f
H:              1.5T  524G  938G  36% /cygdrive/h
M:              1.5T  524G  938G  36% /cygdrive/m
P:               98G   67G   31G  69% /cygdrive/p
R:               98G   14G   84G  15% /cygdrive/r

Cygwin is available for free from: https://www.cygwin.com/ It adds many powerful tools to the command prompt. To get just the available space on drive M (as mapped in windows to a shared drive), one could enter in:

M:\>df -h | grep M: | awk '{print $4}'
Community
  • 1
  • 1
tommym
  • 2,230
  • 15
  • 11
  • 5
    do you know why you've been minused? Because you don't read question carefully. Read: «without having to install some thirdparty applications». Though I personally like unxutils, it's not answer here. – LogicDaemon Oct 06 '14 at 14:52
  • 7
    It's still a decent quality answer for those coming here other than the OP – Peter Feb 23 '15 at 11:18
6

The following script will give you free bytes on the drive:

@setlocal enableextensions enabledelayedexpansion
@echo off
for /f "tokens=3" %%a in ('dir c:\') do (
    set bytesfree=%%a
)
set bytesfree=%bytesfree:,=%
echo %bytesfree%
endlocal && set bytesfree=%bytesfree%

Note that this depends on the output of your dir command, which needs the last line containing the free space of the format 24 Dir(s) 34,071,691,264 bytes free. Specifically:

  • it must be the last line (or you can modify the for loop to detect the line explicitly rather than relying on setting bytesfree for every line).
  • the free space must be the third "word" (or you can change the tokens= bit to get a different word).
  • thousands separators are the , character (or you can change the substitution from comma to something else).

It doesn't pollute your environment namespace, setting only the bytesfree variable on exit. If your dir output is different (eg, different locale or language settings), you will need to adjust the script.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
1

I make a variation to generate this out from script:

volume C: - 49 GB total space / 29512314880 byte(s) free

I use diskpart to get this information.

@echo off
setlocal enableextensions enabledelayedexpansion
set chkfile=drivechk.tmp
if "%1" == "" goto :usage
set drive=%1
set drive=%drive:\=%
set drive=%drive::=%
dir %drive%:>nul 2>%chkfile%
for %%? in (%chkfile%) do (
  set chksize=%%~z?
)
if %chksize% neq 0 (
  more %chkfile%
  del %chkfile%
  goto :eof
)
del %chkfile%
echo list volume | diskpart | find /I " %drive% " >%chkfile%
for /f "tokens=6" %%a in ('type %chkfile%' ) do (
    set dsksz=%%a
)
for /f "tokens=7" %%a in ('type %chkfile%' ) do (
    set dskunit=%%a
)
del %chkfile%
for /f "tokens=3" %%a in ('dir %drive%:\') do (
  set bytesfree=%%a
)
set bytesfree=%bytesfree:,=%
echo volume %drive%: - %dsksz% %dskunit% total space / %bytesfree% byte(s) free
endlocal

goto :eof
:usage
  echo.
  echo   usage: freedisk ^<driveletter^> (eg.: freedisk c)
1

Using paxdiablo excellent solution I wrote a little bit more sophisticated batch script, which uses drive letter as the incoming argument and checks if drive exists on a tricky (but not beauty) way:

@echo off
setlocal enableextensions enabledelayedexpansion
set chkfile=drivechk.tmp
if "%1" == "" goto :usage
set drive=%1
set drive=%drive:\=%
set drive=%drive::=%
dir %drive%:>nul 2>%chkfile%
for %%? in (%chkfile%) do (
  set chksize=%%~z?
)
if %chksize% neq 0 (
  more %chkfile%
  del %chkfile%
  goto :eof
)
del %chkfile%
for /f "tokens=3" %%a in ('dir %drive%:\') do (
  set bytesfree=%%a
)
set bytesfree=%bytesfree:,=%
echo %bytesfree% byte(s) free on volume %drive%:
endlocal

goto :eof
:usage
  echo.
  echo   usage: freedisk ^<driveletter^> (eg.: freedisk c)

note1: you may type simple letter (eg. x) or may use x: or x:\ format as drive letter in the argument

note2: script will display stderr from %chkfile% only if the size bigger than 0

note3: I saved this script as freedisk.cmd (see usage)

Community
  • 1
  • 1
Zsolt Hidasi
  • 101
  • 1
  • 2
0

Is cscript a 3rd party app? I suggest trying Microsoft Scripting, where you can use a programming language (JScript, VBS) to check on things like List Available Disk Space.

The scripting infrastructure is present on all current Windows versions (including 2008).

gimel
  • 83,368
  • 10
  • 76
  • 104
  • 1
    `cscript` *is* Microsoft Scripting. It is the console version of the Windows scripting interpreter (`wscript` is the GUI version). – Synetech Jul 28 '14 at 20:12
  • i have to assume the "third party install" rather means "any extra software install". that the practical aspect of neither needing to provide nor to install anything. only on-board means. – Alexander Stohr Mar 16 '20 at 17:11