繁体   English   中英

批次:截断字符串,然后保存到变量

[英]Batch: Truncate String before saving to variable

也许这是一个简单的问题。 我想制作一个小的批处理脚本,输出已安装,已使用和可用的RAM。 用过的RAM没问题,我只是用

 for /f "skip=1" %%a in ('wmic os get freephysicalmemory') do ( 

并以MB为单位输出可用RAM。 但是当我使用

for /f "skip=1" %%p in ('wmic computersystem get totalphysicalmemory') do ( 

它以KB为单位输出总RAM,从而创建一个大于32位的字符串(在我的情况下为8441917440)。在将其保存为变量之前,有没有办法截断它? 像截断后三位数或六位数一样? 否则我将无法使用它进行计算。

谢谢!!

您可以使用PowerShell进行大于32位数字的数学运算。

@echo off
setlocal enabledelayedexpansion

:: Get free physical memory, measured in KB
for /f "tokens=2 delims==" %%A in ('wmic os get freephysicalmemory /value ^| find "="') do set free_physical_memory_kb=%%A

:: Get total physical memory, measured in B
for /f "tokens=2 delims==" %%A in ('wmic computersystem get totalphysicalmemory /value ^| find "="') do set total_physical_memory_b=%%A

:: Batch can't do math with numbers larger than 429496728, but PowerShell can
:: The huge number can still be stored in a batch variable because it gets treated
:: as a string until calculations are done with it
for /f %%A in ('powershell !total_physical_memory_b!/1024') do set total_physical_memory_kb=%%A

:: Convert the KB variables to MB
:: Batch can only do integer math, so the numbers will be rounded down
set /a free_physical_memory_mb=%free_physical_memory_kb%/1024
set /a total_physical_memory_mb=%total_physical_memory_kb%/1024

:: Get the percentage available (again, using PowerShell because batch can only do integer math)
for /f %%A in ('powershell !free_physical_memory_mb!/!total_physical_memory_mb!*100') do set free_percent=%%A

echo Free memory: %free_physical_memory_mb% MB
echo Total memory: %total_physical_memory_mb% MB
echo Percentage free: %free_percent%%%

pause

这是一个脚本,可在一次对PowerShell的调用中执行所有计算:

@echo off
setlocal

:: Get free physical memory, measured in KB
for /f "skip=1" %%A in ('wmic os get freephysicalmemory') do for %%B in (%%A) do set free_KB=%%B

:: Get total physical memory, measured in B
for /f "skip=1" %%A in ('wmic computersystem get totalphysicalmemory') do for %%B in (%%A) do set total_B=%%B

:: Compute values in MB
for /f "tokens=1-3" %%A in (
  'powershell -command "& {[int]$total=%total_B%/1MB;[int]$free=%free_KB%/1KB;$used=$total-$free;echo $total' '$used' '$free}"'
) do (
  set total_MB=%%A
  set used_MB=%%B
  set free_MB=%%C
)

:: Print the results
echo Total: %total_MB% MB
echo  Used: %used_MB% MB
echo  Free: %free_MB% MB

批处理和PowerShell之间的上下文切换相当慢。 使用混合批处理/ JScript更快。

我已经编写了一个名为JEVAL.BAT混合JScript /批处理实用程序,可以很方便地将JScript合并到任何批处理文件中

以下使用JEVAL.BAT的脚本的速度大约是使用PowerShell的速度的两倍:

@echo off
setlocal

:: Get free physical memory, measured in KB
for /f "skip=1" %%A in ('wmic os get freephysicalmemory') do for %%B in (%%A) do set free_KB=%%B

:: Get total physical memory, measured in B
for /f "skip=1" %%A in ('wmic computersystem get totalphysicalmemory') do for %%B in (%%A) do set total_B=%%B

:: Compute values in MB
for /f "tokens=1-3" %%A in (
  'jeval "total=Math.round(%total_B%/1024/1024);free=Math.round(%free_KB%/1024);total+' '+(total-free)+' '+free"'
) do (
  set total_MB=%%A
  set used_MB=%%B
  set free_MB=%%C
)

:: Print the results
echo Total: %total_MB% MB
echo  Used: %used_MB% MB
echo  Free: %free_MB% MB

如果可以忍受变化,则切掉最后三(6)位数字很容易:

set  x=8441917440
echo cut last 3 digits: %x:~0,-3%
echo cut last 6 digits: %x:~0,-6%
REM or to cut it in the variable:
set x=%x:~0,-6%
echo cut last 6 digits: %x%

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM