There is internal variable @root, so you can remove this line and use @root instead of myroot.
root | set myroot=
This can handle the cases where hd number is 1 digits.
set /a mydisk=%@root:~3,1%+1
What happens if root hd number is more than 1 digit.
root is (hd10,0)..(hd19,0) => mydisk=2
root is (hd20,0)..(hd29,0) => mydisk=3
root is (hd90,0)..(hd99,0) => mydisk=10
root is (hd100,0)..(hd127,0) => mydisk=2
It will override one of (hd2)..(hd10) but it should be OK because mydisk will not be equal to root hd number.
If you really want to calculate mydisk = root_hd_number+1, this handle the case where root hd number is 1 to 3 digit. hd number overflows at 128.
This will not be used but since I have already think about it, I write it here as an example.
if _%@root:~4,1%==_, set /a mydisk=%@root:~3,1%+1 || if _%@root:~5,1%==_, set /a mydisk=%@root:~3,2%+1 || set /a mydisk=%@root:~3,3%+1
There is (hd) which means next available driver number for hard drive. You can use together with (hd-1) (last existing harddrive). (ref :
rmprebusb)
Map as (hd), then, after hook, it becomes (hd-1).
You don't have to use set, variable, string manipulation, calculation. It looks simple.
title win.vhd map next to last hd
find --set-root --ignore-floppies /win.vhd
map --mem --top /win.vhd (hd)
map --hook
root (hd-1,0)
chainloader /bootmgr