1

我有一个Output.txt包含以下内容的文件:

Server1
APPNAME  MEMORY
WINDOWS  54896378
LINUX    78542
MACOS    187963

Server2
APPNAME     MEMORY
DATABASE    587412369
SCHEMA      78542
TABLESPACE  187963

我想创建一个批处理脚本,用于搜索 Output.txt 中的所有数值(如 54896378、78542、78542 等)并将它们除以 1024*1024,以便在 Newoutput.txt 文件中的 BYTES 内存可以更改为 MB。

我在下面尝试但没有得到我想要的:

@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
)

编辑1

Output.txt文件具有以下内容时,一切正常,但脚本仅转换FreePhysicalMemory值 6621212 即:

输出.txt:

Server1
APPNAME  MEMORY
WINDOWS  54896378
LINUX    78542
MACOS    187963

FreePhysicalMemory  TotalVisibleMemorySize  
6621212                 8387172   

新输出.txt:

Server1
APPNAME  MEMORY
WINDOWS  13.58
LINUX    2.45
MACOS    1.8

FreePhysicalMemory  TotalVisibleMemorySize  
6621212                 21.4          

我们需要在脚本中进行哪些更改..?

4

2 回答 2

3
SET /A Result = %a / 1024*1024

这将 %a 除以 1024 并将输出乘以 1024。这不是您想要的。正确的划分应该是:

SET /A Result = %a / 1024 / 1024

或者您可以预先计算 1024*1024 = 1048576 和

SET /A Result = %a / 1048576
于 2013-10-17T14:30:56.540 回答
3

请注意,批处理仅适用于整数。您的一些计算会产生 0 MB 的值。这是一个关于如何使用十进制值的粗略示例。

@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

  • 更新:在输出中添加了 AppName 以及一些格式。(多于)
  • 更新:添加了 Newoutput.txt 重定向示例。(多于)
  • 更新:添加了对所有标记的转换支持并改进了格式。(以下)
  • 更新:为 find 命令添加了第一行跳过修复。(以下)

@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

  • 更新:将 PowerShell 添加到计算例程以处理高达 2^64 的值(下)

: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
于 2013-10-17T14:46:13.800 回答