如何在批处理文件中获取可用磁盘空间的整数? [英] How to get integer of free disk space in Batch file?

查看: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
exit

但是在 freespace.log 文件中输出.

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

我尽力在 Google 上搜索,但找不到解决方案.请帮帮我:)

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

推荐答案

这是一个使用混合批处理和 Powershell 的精确解决方案.确实应该使用纯 powershell、vbscript 或 jscript 来完成.

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

token 和 IF 语句的非直观使用是为了弥补 FOR/F 和 WMIC 的 unicode 输出之间的奇怪交互.

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%

这篇关于如何在批处理文件中获取可用磁盘空间的整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆