Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
659 views
in Technique[技术] by (71.8m points)

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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%

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...