Free space in a CMD shell

后端 未结 9 1726
北海茫月
北海茫月 2020-12-02 10:04

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

相关标签:
9条回答
  • 2020-12-02 10:37

    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.
    0 讨论(0)
  • 2020-12-02 10:40

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

    0 讨论(0)
  • 2020-12-02 10:43

    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%
    
    0 讨论(0)
  • 2020-12-02 10:43

    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}'
    
    0 讨论(0)
  • 2020-12-02 10:46

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

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

    0 讨论(0)
  • 2020-12-02 10:46

    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)

    0 讨论(0)
提交回复
热议问题