1

I need to get the boot partition's DRIVE NAME from a bat script.

I get the boot partition number with the reagentc.exe /info as follows:

Windows RE location: \\?\GLOBALROOT\device\harddisk0\partition1\Recovery\WindowsRE

but I need the Drive name. How could that be retrieved from bat?

ViV
  • 1,998
  • 8
  • 27
  • 54

2 Answers2

3

Here's my solution.

First create a drives.bat with the following content (it will list the logical drives and their disk partitions):

@if (@X)==(@Y) @end /* JSCRIPT COMMENT **


@echo off
cscript //E:JScript //nologo "%~f0"
exit /b

// Sources of wisdom:
//http://social.msdn.microsoft.com/Forums/vstudio/en-US/659030c8-bcf5-4542-bbc6-eaf9679e090a/cannot-create-object-wmi-in-javascript
//http://blogs.technet.com/b/heyscriptingguy/archive/2005/05/23/how-can-i-correlate-logical-drives-and-physical-disks.aspx
//http://stackoverflow.com/a/1144788/388389

************** end of JSCRIPT COMMENT **/


String.prototype.replaceAll = function (find, replace) {
    var str = this;
    return str.replace(new RegExp(find.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'), 'g'), replace);
};


var winmgmts= GetObject("winmgmts:\\\\.\\root\\cimv2")
var drives = winmgmts.ExecQuery( "SELECT * FROM Win32_DiskDrive", null, 48 );
//WScript.Echo(drives);

var drvs = new Enumerator(drives);
for (;!drvs.atEnd();drvs.moveNext()) {
    var drive=drvs.item();

    WScript.Echo( "Physical Disk: " + drive.Caption + " -- " + drive.DeviceID );
    var deviceID = drive.DeviceID.replaceAll( "\\" ,"\\\\");
    var colPartitions = winmgmts.ExecQuery( "ASSOCIATORS OF {Win32_DiskDrive.DeviceID=\"" + 
        deviceID + "\"} WHERE AssocClass =  Win32_DiskDriveToDiskPartition" , null, 48 );

    var colParts = new Enumerator(colPartitions);
    for (;!colParts.atEnd();colParts.moveNext()) {

        var partition=colParts.item();

        //WScript.Echo( "Disk Partition: " + partition.DeviceID );
        var colLogicalDisks = winmgmts.ExecQuery( "ASSOCIATORS OF {Win32_DiskPartition.DeviceID=\"" +
                partition.DeviceID + "\"} WHERE AssocClass = Win32_LogicalDiskToPartition" , null, 48);
        var colLD = new Enumerator(colLogicalDisks);

        if (typeof colLD.item() != "undefined") {
            for (;!colLD.atEnd();colLD.moveNext()) {
                var logicalDisk=colLD.item();
                WScript.Echo( "  Logical Disk: " + logicalDisk.DeviceID + " Disk Partition:  harddisk" + partition.DeviceID.split("#")[1].split(",")[0] + "\\partition" + partition.DeviceID.split("#")[2] );
            }
        } else {
            WScript.Echo( "  Disk Partition: harddisk" + partition.DeviceID.split("#")[1].split(",")[0] + "\\partition" + partition.DeviceID.split("#")[2] );
        }

    }   

}

Then in the same directory create an re_d.bat (you can call it whatever you want) in the same directory with the following content:

@echo off

for /f  "tokens=5,6 delims=\" %%a in ('reagentc.exe /info 2^>nul ^|find /i " Windows RE location:"') do (
    set "re_loc=%%a\%%b"
)



for /f "tokens=2 delims=:" %%a in ('drives.bat ^|find /i "%re_loc%"') do (
    set "re_dsk=%%a"
)

echo RE LOCATION : %re_loc%
echo RE LOGICAL DISK : %re_dsk%:

And see of the output of the second .bat pleases you.

npocmaka
  • 55,367
  • 18
  • 148
  • 187
0

If by drive name you mean Volume label then you can use the below to find the name the 5th word in will be the drive name.

vol %SystemDrive%

enter image description here

Badradish
  • 196
  • 1
  • 13
  • ...but this does not mean that this is the boot drive.E.g if you install your windows under `D:` it will be the system drive but boot.ini file or whatever is it analogue in VISTA/7/8.. will be on `C:` drive – npocmaka Jul 28 '14 at 10:48
  • The boot partition doesnt have a conventional name, it is a hidden partition. [Link](http://superuser.com/questions/351017/where-can-i-find-the-boot-ini-file-on-windows-7) – Badradish Jul 28 '14 at 11:00