简体   繁体   中英

Batch: Truncate String before saving to variable

maybe it's a simple question. I want to make a small batch script that outputs installed, used and free RAM. Used RAM is no problem, I just use

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

and it outputs the free RAM in MB. But when I use

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

it outputs the total RAM in kB, which creates a string > 32 Bit, in my case 8441917440. Is there a way to truncate this before saving it into a variable? Like cutoff the last three or six digits? Otherwise I won't be able to calculate with it.

Thanks!!

You can use PowerShell to do math with numbers larger than 32-bit.

@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

Here is a script that performs all the computations in one call to 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

The context switching between batch and PowerShell is fairly slow. It is faster to use hybrid batch/JScript.

I have written a hybrid JScript/batch utility called JEVAL.BAT that makes it convenient to incorporate JScript within any batch file .

The following script using JEVAL.BAT is about twice as fast as using 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

to cut the last 3 (6) digits is easy, if you can live with the delta:

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%

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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