How to get integer of free disk space in Batch fil

2019-01-19 17:44发布

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
exit

But output in freespace.log file.

FreeSpace    
9772687360   
57401442304  
7346626560   
0            
0            

I need to pick integer only and summation. After summation output like this.

74520756224

I search on Google as my best but I can't find solutions. Please help me:)

4条回答
Deceive 欺骗
2楼-- · 2019-01-19 18:20

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"
查看更多
ゆ 、 Hurt°
3楼-- · 2019-01-19 18:30

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%
查看更多
姐就是有狂的资本
4楼-- · 2019-01-19 18:32

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%
查看更多
手持菜刀,她持情操
5楼-- · 2019-01-19 18:32

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).

查看更多
登录 后发表回答