How to get integer of free disk space in Batch file?

前端 未结 4 1279
死守一世寂寞
死守一世寂寞 2020-12-11 04:32

I\'m trying to get integer of free disk space in Batch file. This is a my (very) simple code.

@echo off
wmic logicaldisk get freespace >> freespace.log         


        
相关标签:
4条回答
  • 2020-12-11 05:09

    I second @dbenham's recommendation to switch to PowerShell (or at least VBScript or JScript). However, if you want to stick with batch and don't need exact numbers you could do something like this:

    @echo off
    
    setlocal EnableDelayedExpansion
    
    for /f %%v in ('wmic logicaldisk get freespace ^| findstr /r "[0-9]"') do (
      set val=%%v
      set val=!val:~-6!
      set /a sum+=val
    )
    
    echo !sum!
    
    endlocal
    

    The line set val=!val:~-6! removes the last 6 digits from each value. That way you can calculate with Megabytes instead of Bytes, but (obviously) lose some precision, because the value is truncated (and not rounded correctly).

    0 讨论(0)
  • 2020-12-11 05:11

    Here is a pure batch solution that can handle numbers greater than ~ 2G:

    @echo off
    setlocal EnableDelayedExpansion
    set /A "SUMMEG=0" & set /A "SUMONE=0"
    set /A "NUM=6" & set "ZER=000000"
    for /F "tokens=2 delims==" %%F in ('
        wmic LOGICALDISK GET FreeSpace /VALUE
    ') do (
        for %%G in (%%F) do (
            set "ONE=%ZER%%%G"
            for /F "tokens=* delims=0" %%H in ("!ONE:~,-%NUM%!") do set "MEG=%%H"
            for /F "tokens=* delims=0" %%H in ("!ONE:~-%NUM%!") do set "ONE=%%H"
            set /A "SUMMEG+=MEG" & set /A "SUMONE+=ONE"
        )
    )
    if not "!SUMONE:~,-%NUM%!"=="" (set /A "SUMMEG+=!SUMONE:~,-%NUM%!")
    set "SUMONE=%ZER%!SUMONE!" & set "SUMONE=!SUMONE:~-%NUM%!"
    for /F "tokens=* delims=0" %%H in ("%SUMMEG%%SUMONE%") do set "FRE=%%H"
    endlocal & if "%FRE%"=="" (set "FRE=0") else (set "FRE=%FRE%")
    echo %FRE%
    
    0 讨论(0)
  • 2020-12-11 05:24

    Use below code. Try replacing the caption text with deviceid if it shows ? or blank.

    @echo off
    setlocal
    set drive=C
    set free=?
    rem Note: WMIC will output unicode text
    wmic logicaldisk where (caption = "%drive%:") get freespace>"%temp%\free.tmp"
    for /f %%A in ('type "%temp%\free.tmp"') do (set free=%%A)
    echo Free space: %free% bytes
    rem if exist "%temp%\free.tmp" del "%temp%\free.tmp"
    
    0 讨论(0)
  • 2020-12-11 05:28

    Here is an exact solution using hybrid batch and powershell. It really should be done with pure powershell, vbscript, or jscript.

    The non-intuitive use of tokens and the IF statement are to compensate for the weird interaction between FOR /F and the unicode output of WMIC.

    @echo off
    setlocal enableDelayedExpansion
    set /a freeSpace=0
    for /f "skip=1 tokens=1,2" %%A in ('wmic logicaldisk get freespace') do (
      if "%%B" neq "" for /f %%N in ('powershell !freeSpace!+%%A') do (
        set freeSpace=%%N
      )
    )
    echo freeSpace=%freeSpace%
    
    0 讨论(0)
提交回复
热议问题