0

此命令在管理员 CMD 中显示引导管理器等待标准操作系统自动引导的秒数:

bcdedit /enum | find "timeout"

我想以秒为单位将输出值批量压入环境变量吗?但它不是这样工作的:

for /f "tokens=2*" %%a in ('bcdedit /enum | find "timeout"') do set "value=%%a"
echo %value%
pause
exit

有谁知道如何设置令牌命令?

4

2 回答 2

1

您必须在 | 之前使用转义字符“^” 而且脚本必须以管理员身份运行,因为 bcdedit 命令需要管理员权限。

@echo off

net session >nul 2>&1 || (powershell start -verb runas '"%~0"' &exit /b)

for /f "tokens=2*" %%a in ('bcdedit /enum ^| find /i "timeout"') do set "value=%%a"
                                                               
echo %value%
pause
exit
于 2021-08-01T22:59:37.267 回答
0

我首先要确保我只在适当的标识符下输出{bootmgr}信息, . 然后我不会只使用英语字符串timeout

窗口“以管理员身份运行”:

For /F "Delims=" %G In ('%SystemRoot%\System32\bcdedit.exe /Enum {bootmgr} 2^>NUL ^| %SystemRoot%\System32\findstr.exe /RC:"[ ][ ]*[0123456789][0123456789]*$"') Do @For %H In (%G) Do @Set "$=%H"

或从 “以管理员身份运行”:

@Echo Off
SetLocal EnableExtensions
Set "$=" & For /F "Delims=" %%G In (
    '%SystemRoot%\System32\bcdedit.exe /Enum {bootmgr} 2^>NUL^
    ^|%SystemRoot%\System32\findstr.exe /RC:"[ ][ ]*[0123456789][0123456789]*$'
) Do For %%H In (%%G) Do Set "$=%%H"

在此示例中,从成功结果开始的秒数应保存为可访问为 的变量的字符串值%$%

于 2021-08-01T15:29:53.230 回答