How can you define a bcdedit value from a variable by batch (tokens)?

703 views Asked by At

This command shows in an admin CMD how many seconds the boot manager waits for the standard operating system to boot automatically:

bcdedit /enum | find "timeout"

Do I want to press the output value in seconds into an environment variable in batch? But it doesn't work like this:

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

Does anyone know how to set up the token command?

2

There are 2 answers

0
Compo On

I would first of all ensure that I'm only outputting the information under the appropriate identifier, {bootmgr}. Then I would not use the English language only string timeout.

From a window 'Run as administator':

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"

Or from a 'Run as administrator':

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

In this example the number of seconds from a successful result should be saved as the string value of a variable accessible as %$%.

1
Ricardo Bohner On

You have to use an escape character "^" before the | and also the script has to run as admin because the bcdedit command requires admin privileges.

@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