Ask Rich: if I wanted to perform math on the output of a cmd line string… can I do it?

I was asked if it was possible to perform math on the output of command line string. Can it be done?

Sure.

Example command given:

C:\Windows\System32>wmic os get TotalVirtualMemorySize /every:5 /repeat:5 | findstr /r [0-9]

This returns a value, in my case, of: 4800564

Wants to divide by 1024 and check ever 5 seconds for 5 iterations. Here’s my adapted batch script.

@echo off
@setlocal enableextensions enabledelayedexpansion
set max_iterations=5
set iteration=1
:loop
if %iteration% GTR %max_iterations% (goto end)
for /f tokens=1,* delims== %%x in ('wmic os get TotalVirtualMemorySize ^| findstr /r ^[0-9]^') do set /A var=%%x/1024
echo %var%
goto sleep
:sleep
ping -n 5 127.0.0.254 >NUL 2>&1
set /A iteration=%iteration%+1
goto loop
:end
echo bye
askrich1

Published by

Rich

Just another IT guy.

Leave a Reply

Your email address will not be published. Required fields are marked *