Free space in a CMD shell

后端 未结 9 1880
北海茫月
北海茫月 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: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%
    

提交回复
热议问题