我尝试编写一个批处理文件,它为我提供了可用的物理内存。此批处理应每 X 秒写入一个时间戳并在文本文件中写入以下命令:
wmic OS get FreePhysicalMemory
我的批处理文件如下所示:
:loop
(
@echo %time% %date%
wmic OS get FreePhysicalMemory /Value
)>testMEMORY.txt
timeout /t 10
goto loop
这是输出:
13:54:54,76 04.11.2019
F r e e P h y s i c a l M e m o r y = 7 2 0 6 6 4 8
我不明白,为什么我有很多新行,并且每个字符后面有一个空格。有人可以帮我吗?
最佳答案
WMIC的输出是unicode!
尾随<CR>
可以通过将值传递给另一个 FOR /F
来删除循环。
这也删除了虚拟的“空白”行(实际上是 <CR>
)
@echo off
If Exist testMEMORY.txt Del testMEMORY.txt
:loop
(
@echo %time% %date%
for /f "skip=1 delims=" %%a in ('wmic OS get FreePhysicalMemory /Value') do (
for /f "delims=" %%b in ("%%a") do (
echo %%b
)
)
)>>testMEMORY.txt
timeout /t 10 /nobreak>nul
goto loop
所以,我得到的结果是 testMEMORY.txt
类似的东西:
15:32:19.02 04/11/2019 FreePhysicalMemory=528592 15:32:29.19 04/11/2019 FreePhysicalMemory=530804 15:32:39.14 04/11/2019 FreePhysicalMemory=531156 15:32:49.21 04/11/2019 FreePhysicalMemory=530968 15:32:59.16 04/11/2019 FreePhysicalMemory=531936 15:33:09.23 04/11/2019 FreePhysicalMemory=530376 15:33:19.18 04/11/2019 FreePhysicalMemory=524980 15:33:29.16 04/11/2019 FreePhysicalMemory=521816 15:33:39.21 04/11/2019 FreePhysicalMemory=465820 15:33:49.17 04/11/2019 FreePhysicalMemory=461312 15:33:59.17 04/11/2019 FreePhysicalMemory=385172 15:34:14.21 04/11/2019 FreePhysicalMemory=529800 15:34:24.13 04/11/2019 FreePhysicalMemory=541064
关于Windows Batch wmic OS 获取 FreePhysicalMemory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58693989/