Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enumerate system drives in nodejs

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.

like image 842
foobarcode Avatar asked Apr 08 '13 12:04

foobarcode


3 Answers

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.

like image 184
Cedric Avatar answered Oct 21 '22 07:10

Cedric


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'
    }
]
like image 13
jviotti Avatar answered Oct 21 '22 08:10

jviotti


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:"]
like image 8
Edwin Lee Avatar answered Oct 21 '22 07:10

Edwin Lee