0

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?

aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • How does the output of the `bcdedit` line look like? Anyway, escape the pipe symbol like `^|` so that it becomes executed inside of the `for /F` loop. *N. B.:* use `exit /B` rather than `exit`… – aschipfl Aug 01 '21 at 15:12

2 Answers2

1

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
Ricardo Bohner
  • 301
  • 2
  • 10
  • 1
    `BCDEdit /Enum` is the same as `BCDEdit /Enum ALL` which is the same as `BCDEdit`, so there is no need to use the `/Enum` option, unless you are supplying an identifier. In this case, the result seeked is always attributed only to the same identifier, `{bootmgr}`, which is why I used that specifically in my answer. It's more efficient to run your `find.exe`/`findstr.exe` match against a smaller number of strings/lines, than to to run it against all of the identifiers information output with just `BCDEdit`. Also as shown in my answer, the string `timeout` isn't consistent accross all languages. – Compo Aug 02 '21 at 14:17
0

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 %$%.

Compo
  • 36,585
  • 5
  • 27
  • 39