locked
Set Variable in nested FOR /F Loop RRS feed

  • Question

  • Hi there

    at the moment I have an Problem I cant´t understand....

    I have to change the BCD store and add some entries. So I made first an text file with the partitions/installation to add.

    This looks so:

    E:\imageinfo\Windows 8.1 - Testimage V1.06
    F:\imageinfo\Windows 10 - Testimage V1.03

    From here I extract the drive letter (%part%) and the name (%desc%)

    Next step would be to create the entry via bcdedit. This gives me an guid which i need to alter the settings.

    But this doesn´t work.

    I use this code:

    setlocal enabledelayedexpansion

    for /f "tokens=1,4 delims=\ " %%i in ('type names.ttt') do (
    set part=%%i
    set desc=Windows_%%j
    bcdedit /store %bootstore% /create /d "%desc%" /application osloader > guid.ttt
    for /f "tokens=3 delims= " %%c in ('type guid.ttt') do set guid=%%c
    sleep 1
    bcdedit /store %bootstore% /set %guid% device partition=%part% >nul
    bcdedit /store %bootstore% /set %guid% osdevice partition=%part% >nul
    bcdedit /store %bootstore% /set %guid% path \Windows\system32\winload.efi >nul
    bcdedit /store %bootstore% /set %guid% systemroot \Windows >nul
    bcdedit /store %bootstore% /set %guid% bootmenupolicy Standard >nul
    bcdedit /store %bootstore% /set %guid% detecthal Yes >nul

    )

    The variable %guid% is always empty - no matter what I try.

    The command looks in the command windows like this

    bcdedit /store c:\boot\BCD_TEST set   device partition=J:

    Between set and device are three blanks - but it should be the %guid%

    Where´s my fault? Doesn´t it work in an loop? It should.

    One more thing - the part 

    bcdedit /store %bootstore% /create /d "%desc%" /application osloader > guid.ttt
    for /f "tokens=3 delims= " %%c in ('type guid.ttt') do set guid=%%c

    is working outside the loop as it should.

    It seems that %guid% is set right after the ) - but why and how to get this inside the brackets?

    Thank you!


    • Edited by logg.sar Wednesday, September 28, 2016 12:38 PM
    Wednesday, September 28, 2016 12:24 PM

Answers

  • In PowerShell type:

    $volumes='list volume' | diskpart

    This will list all volumes to a variable.  he rest is now very easy.


    \_(ツ)_/

    • Marked as answer by logg.sar Monday, October 17, 2016 1:13 PM
    Wednesday, September 28, 2016 3:13 PM

All replies

  • You will be much better off using PowerShell rather than clumsy cmd.exe shell script (batch).

    -- Bill Stewart [Bill_Stewart]

    Wednesday, September 28, 2016 2:26 PM
  • You will be much better off using PowerShell rather than clumsy cmd.exe shell script (batch).

    -- Bill Stewart [Bill_Stewart]

    I would love to, but explicit in this part there is a Powershell-limitation so I dont want to mix two scripting languages.

    The limitation I´m talking is that I have no chance with powershell to find any hidden volume.

    If you have an UEFI system type echo list vol | diskpart in an command window and you will see one hidden volume. At least I couldn´t find any way to see this with an powershell command.

    Well - probably I can get this information also from powershell, but its not as easy - I would have to parse the output of diskpart in a temporary file, search for 'hidden' and cut the string until i get what I need.

    In Batch this is a two-liner.

    Wednesday, September 28, 2016 3:07 PM
  • In PowerShell type:

    $volumes='list volume' | diskpart

    This will list all volumes to a variable.  he rest is now very easy.


    \_(ツ)_/

    • Marked as answer by logg.sar Monday, October 17, 2016 1:13 PM
    Wednesday, September 28, 2016 3:13 PM
  • explicit in this part there is a Powershell-limitation

    What does this mean? Why limit to an inferior tool when a superior tool is available?


    -- Bill Stewart [Bill_Stewart]

    Wednesday, September 28, 2016 7:35 PM
  • Hi there

    jrv pushed me to the right direction.

    I´ve completed my script and it works how ist should.

    Thank you

    Monday, October 17, 2016 1:14 PM