Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

batch-file

cmd

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

like image 224
nakorndev Avatar asked Oct 28 '12 07:10

nakorndev


2 Answers

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%
like image 74
dbenham Avatar answered Nov 15 '22 09:11

dbenham


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"
like image 21
Jay Avatar answered Nov 15 '22 09:11

Jay