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

前端 未结 4 1283
死守一世寂寞
死守一世寂寞 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).

提交回复
热议问题