12

Is there a way to retrieve the drive name of all logical drives on a computer ?

I've looked at the fs api, but from there I can only enumerate the files and directories of a given directory.

hippietrail
  • 15,848
  • 18
  • 99
  • 158
foobarcode
  • 2,279
  • 1
  • 23
  • 24

5 Answers5

12

I'm not sure what you mean by "drive name". If you mean drives in the form of \\.\PhysicalDriveN, I faced the same problem and implemented this module that works in all major operating systems:

https://github.com/resin-io/drivelist

For Windows, you get information such as:

[
    {
        device: '\\\\.\\PHYSICALDRIVE0',
        description: 'WDC WD10JPVX-75JC3T0',
        size: '1000 GB'
    },
    {
        device: '\\\\.\\PHYSICALDRIVE1',
        description: 'Generic STORAGE DEVICE USB Device',
        size: '15 GB'
    }
]
jviotti
  • 17,881
  • 26
  • 89
  • 148
  • 1
    This is the correct and cross-platform solution! If you create an app in nodejs only for one OS, you're doing it wrong! – Dziad Borowy Dec 30 '15 at 12:17
  • If you are failing to build during `npm install drivelist`, just install an older version of this library. – Mohi Jul 07 '18 at 16:43
  • im getting this error when trying to retrieve the drives: `This version of Node.js requires NODE_MODULE_VERSION 75`. any idea what to do? ive already tried rebuilding and reinstalling – oldboy Jan 01 '20 at 03:35
  • Contains a lot of nice info, but not the drive label, which is how I am trying to identify a specific removable media... – Michael Oct 24 '22 at 01:34
8

If you targeting on Windows, you could try this:

This solution base upon the idea from this post.

I wrap it with promise.

var spawn = require("child_process").spawn

function listDrives(){
    const list  = spawn('cmd');

    return new Promise((resolve, reject) => {
        list.stdout.on('data', function (data) {
            // console.log('stdout: ' + String(data));
            const output =  String(data)
            const out = output.split("\r\n").map(e=>e.trim()).filter(e=>e!="")
            if (out[0]==="Name"){
                resolve(out.slice(1))
            }
            // console.log("stdoutput:", out)
        });

        list.stderr.on('data', function (data) {
            // console.log('stderr: ' + data);
        });

        list.on('exit', function (code) {
            console.log('child process exited with code ' + code);
            if (code !== 0){
                reject(code)
            }
        });

        list.stdin.write('wmic logicaldisk get name\n');
        list.stdin.end();
    })
}

listDrives().then((data) => console.log(data))

Test it, you will see the result like:

["c:", "d:"]
Community
  • 1
  • 1
Edwin Lee
  • 149
  • 1
  • 4
8

Based on Edwin Lees answer:

const child = require('child_process');

child.exec('wmic logicaldisk get name', (error, stdout) => {
    console.log(
        stdout.split('\r\r\n')
            .filter(value => /[A-Za-z]:/.test(value))
            .map(value => value.trim())
    );
});

Output: ['C:', 'D:'] etc.

Cedric
  • 532
  • 5
  • 7
2

How about using the DiskPart command? Does running diskpart list in the command line give you the output you need? If so you can execute this in node using child_process.exec

var exec = require('child_process').exec
var cmd = 'diskpart list'
exec(cmd, function(err, stdout, stderr) {
    if (err) {
        console.log('error running diskpart list command')
        console.log(err)
        return
    }
    console.log('stdout data')
    console.log(stdout)

    console.log('stderr data')
    console.log(stderr)
})
divibisan
  • 11,659
  • 11
  • 40
  • 58
Noah
  • 33,851
  • 5
  • 37
  • 32
  • 1
    That would work yes, but I'd like to avoid running a child process and parsing the command output. But that would be probably what I'll resort to if there's nothing in node's API... – foobarcode Apr 08 '13 at 14:10
  • 1
    I believe this is the best you are going to do. node.js doesn't have any way to do this natively. On OSX you could read the `/Volumes/` directory and on linux you could look at `/mnt` but Windows doesn't provide a directory of mounted drives for you. Also note http://www.computerhope.com/issues/ch000854.htm#2 and wmic logicaldisk get name – Noah Apr 08 '13 at 14:19
0

+1 for @Bagherani's downgrade suggestion!

I am using Electron React Boilerplate v4.0 and could not get drivelist to load. I downgraded to drivelist@5.2.12 and it works for my needs.

Ivan
  • 46
  • 4