Reputation: 8312
I have a Output.txt
file which has following content:
Server1
APPNAME MEMORY
WINDOWS 54896378
LINUX 78542
MACOS 187963
Server2
APPNAME MEMORY
DATABASE 587412369
SCHEMA 78542
TABLESPACE 187963
I want to create a batch script which searches for all numeric values in Output.txt (like 54896378,78542,78542 etc.) and divide them by 1024*1024 sothat in Newoutput.txt file memory in BYTES can change into MB.
I tried below but not getting what I want:
@echo off
setlocal enabledelayedexpansion
for /F "delims= " %%a in ('findstr "[1-9][0-9]* 0"' Output.txt) do (
SET /A Result = %a / 1024*1024 > Newoutput.txt
)
EDIT1:
When Output.txt
file has following content then everything is working fine but Script is not converting FreePhysicalMemory
value 6621212 only i.e.:
Output.txt:
Server1
APPNAME MEMORY
WINDOWS 54896378
LINUX 78542
MACOS 187963
FreePhysicalMemory TotalVisibleMemorySize
6621212 8387172
Newoutput.txt:
Server1
APPNAME MEMORY
WINDOWS 13.58
LINUX 2.45
MACOS 1.8
FreePhysicalMemory TotalVisibleMemorySize
6621212 21.4
What changes we need to make in script..?
Upvotes: 1
Views: 208
Reputation: 11367
Do note that batch only works with integers. Several of your calculations would result in a value of 0 MBs. Here is a rough example of how to work with decimal values.
@echo off
call :Parse > Newoutput.txt
exit /b 0
:Parse
for /f "tokens=1,2" %%A in (Output.txt) do call :ToMB "%%~B" "%%~A" || echo(%%A %%B
exit /b 0
:IsNumber <String>
for /f "delims=0123456789" %%A in ("%~1") do exit /b 1
exit /b 0
:ToMB <String> <Name>
setlocal
call :IsNumber "%~1" || exit /b 1
set "Number=%~1"
set /a "Number/=1024"
set /a "Decimal=Number"
set /a "Number/=1024"
set /a "Decimal-=(Number * 1024)"
set /a "Decimal=(Decimal * 1000) / 1024"
set "Decimal=000%Decimal%"
set "Number= %Number%"
set "Name=%~2 "
echo %Name:~0,12%%Number:~-3%.%Decimal:~-3%
endlocal
exit /b 0
@echo off
call :Parse > Newoutput.txt
exit /b 0
:Parse
setlocal
for /f "tokens=1,* delims=]" %%A in ('type "Output.txt" ^|find /n /v ""') do (
for /f "tokens=1,2" %%X in ("%%~B") do call :Convert "%%~X" "%%~Y"
call :Blank "%%~B"
)
endlocal
exit /b 0
:Blank <String>
set "String=%~1"
if not defined String echo.
exit /b 0
:IsNumber <String>
for /f "delims=0123456789" %%A in ("%~1") do exit /b 1
if "%~1"=="" exit /b 2
exit /b 0
:Convert <String> <String>
call :Calculate "%~1" Y || call :Display "%~1" Y
call :Calculate "%~2" || call :Display "%~2"
echo.
exit /b 0
:Calculate <Number> [Pad]
call :IsNumber "%~1" || exit /b 1
set "Number=%~1"
set /a "Number/=1024"
set /a "Decimal=Number"
set /a "Number/=1024"
set /a "Decimal-=(Number * 1024)"
set /a "Decimal=(Decimal * 1000) / 1024"
set "Decimal=000%Decimal%"
set "Number=000%Number%"
call :Display "%Number:~-3%.%Decimal:~-3%" %2
exit /b 0
:Display <String> [Pad]
set "String=%~1"
set "Pad=%~2"
if defined Pad set "String=%String% "
if defined String set /p "=%String:~0,24%" <nul
exit /b 0
:Calculate <Number> [Pad]
call :IsNumber "%~1" || exit /b 1
set "Number="
set "Decimal="
for /f "tokens=1,2 delims=." %%A in ('"PowerShell %~1 / ( 1024 * 1024 )"') do (
set "Number=%%A"
set "Decimal=%%B000"
)
call :Display "%Number%.%Decimal:~0,3%" %2
exit /b 0
Upvotes: 3
Reputation: 56180
SET /A Result = %a / 1024*1024
This divides %a by 1024 and multiplies the output with 1024. This is not, what you want. The correct division should be:
SET /A Result = %a / 1024 / 1024
Or you can precalculate 1024*1024 = 1048576 and
SET /A Result = %a / 1048576
Upvotes: 3