1

So I think I have this correct but for some reason it's not reading from the output file "ram.dat". Can anyone find the error in this?

@echo off
set percent=90

:ramcalc
cls
if %percent% GTR 90 Echo Needs To Be Less Than 90
if %percent% LSS 1 Echo Needs To Be Greater Than 1
echo Type Percent Of Ram To Calculate
set /p percent=1-90:
if %percent% GTR 90 goto ramcalc
if %percent% LSS 1 goto ramcalc

cls Calculating...
setlocal ENABLEDELAYEDEXPANSION
set vidx=0
for /F "tokens=*" %%A in (ram.dat) do (
    SET /A vidx=!vidx! + 1
    set var!vidx!=%%A
    echo %%A
)
set var > test.txt
calc %var2%+%var3%+%var4%+%var5% >tmp
set /p add= < tmp
del tmp
calc %add%/1000000 >tmp
set /p divide= < tmp
del tmp
calc %divide%*0.%percent% >tmp
set /p ram= < tmp
del tmp
set /a round=%ram%+0
set ram=%round%
calc %ram%/1024 >tmp
set /p gb= < tmp
del tmp
set /a ramb=%gb%+0

cls
echo %percent% Rounded Is %ram%MB Approxamatly %ramb%GB 
pause
goto ramcalc

I'm going to be using this in a dynamic memory modification and it is just a modified sample of my code.

4

1 回答 1

1

当它突然消失时,我正在输入您另一个问题的答案。在那个问题中,您正在处理三个复杂性。1) DOS 中的数学运算仅限于 32 位数字。2) WMIC 输出 Unicode,因此它会抛出for /F命令。3)endlocal丢弃ram变量。这是我想解决所有三个问题的方法:

@echo off

wmic memorychip get capacity>ram.dat
type ram.dat>ram.txt
setlocal ENABLEDELAYEDEXPANSION

set /p percent=Enter percentage (1-100): 

set vidx=0
set var1=0
set var2=0
set var3=0

for /F "skip=1 delims=" %%i in (ram.txt) do (
  if "!tmp!" NEQ "" (
    SET /A vidx=!vidx! + 1
    set var!vidx!=%%i
  )
)

set /a var1/=1048576
set /a var2/=1048576
set /a var3/=1048576
set /a total=!var1!+!var2!+!var3!
set ram=!total!MB

del ram.dat
del ram.txt

endlocal &set /a ram=%total% * %percent% / 100

set ram=%ram%MB
set ram

这将计算 3 个内存卡插槽的 ram,ram在运行批处理文件结束时设置环境变量。您当前问题中的新代码看起来像您想要计算可用内存的百分比。我认为您不能在 DOS 中进行十进制数学运算,因此您将不得不使用另一个公式。

编辑 1

我更新了示例以包含您在第二个问题中包含的百分比。您仍然需要添加边界检查。

于 2013-06-14T03:44:42.957 回答