获取启动分区驱动器名称

时间:2014-07-28 04:30:34

标签: windows batch-file disk-partitioning

我需要从bat脚本中获取启动分区的 DRIVE NAME

我使用reagentc.exe /info获取启动分区号,如下所示:

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

但我需要云端硬盘名称。怎么能从蝙蝠那里找回来?

2 个答案:

答案 0 :(得分:3)

这是我的解决方案。

首先使用以下内容创建drives.bat(它将列出逻辑驱动器及其磁盘分区):

@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] );
        }

    }   

}

然后在同一目录中使用以下内容在同一目录中创建re_d.bat(您可以随意调用它):

@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%:

看到第二个.bat的输出让你高兴。

答案 1 :(得分:0)

如果按驱动器名称表示卷标,则可以使用以下内容查找第5个字的名称将是驱动器名称。

vol %SystemDrive%

enter image description here