list partitions in nodejs
Asked Answered
P

5

9

I would like to get the list of partitions in windows, using nodejs. fs.readdir works fine for any folder below or including C:, but I cant figure out what to give it to have the list of partitions like "C:", "D:" and so on.

Anyone know what I should use?

Petronella answered 27/9, 2012 at 13:39 Comment(0)
C
12

There is no api in node.js to list partitions. One workaround is to use child_process and execute wmic command (or any command which can list partitions).

var spawn = require('child_process').spawn,
    list  = spawn('cmd');

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

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

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

list.stdin.write('wmic logicaldisk get name\n');
list.stdin.end();
Candelariacandelario answered 28/9, 2012 at 1:11 Comment(0)
L
7

Not sure if it matches exactly what you're looking for, but we build a NodeJS module called drivelist that will return an array of connected drives with their respective mount points (e.g: mount letters in Windows):

[
    {
        device: '\\\\.\\PHYSICALDRIVE0',
        description: 'WDC WD10JPVX-75JC3T0',
        size: '1000 GB'
        mountpoint: 'C:',
        system: true
    },
    {
        device: '\\\\.\\PHYSICALDRIVE1',
        description: 'Generic STORAGE DEVICE USB Device',
        size: '15 GB'
        mountpoint: 'D:',
        system: false
    }
]

Non-removable drives are marked as system: false, you can filter by that property if that's what you're looking for.

The major advantage of this module is that is works in all major operating systems.

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

Loving answered 1/4, 2016 at 20:4 Comment(0)
B
6

My 2 cents:

Slightly enhanced - a function with callback for easy integration, returns an array of drives :

/**
 * Get windows drives
 * */
function get_win_drives(success_cb,error_cb){
    var stdout = '';
    var spawn = require('child_process').spawn,
            list  = spawn('cmd');

    list.stdout.on('data', function (data) {
        stdout += data;
    });

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

    list.on('exit', function (code) {
        if (code == 0) {
            console.log(stdout);
            var data = stdout.split('\r\n');
            data = data.splice(4,data.length - 7);
            data = data.map(Function.prototype.call, String.prototype.trim);
            success_cb(data);
        } else {
            console.log('child process exited with code ' + code);
            error_cb();
        }
    });
    list.stdin.write('wmic logicaldisk get caption\n');
    list.stdin.end();
}
Barmecidal answered 7/3, 2015 at 0:14 Comment(0)
R
2

a little simpler implementation:

const exec = require('child_process').exec;
exec('wmic logicaldisk get name', (error, stdout, stderr) => {
  if (error) {
    console.error(`exec error: ${error}`);
    return;
  }
  console.log('stdout ', stdout);
  console.log('stderr ', stderr);
});
Resplendent answered 29/9, 2017 at 7:34 Comment(1)
this is simpler and have cleaner output.Voelker
D
0
let arr = []
for(let i = 65; i < 91; i++) if (fs.existsSync(`${String.fromCharCode(i)}:`)) arr.push(String.fromCharCode(i))
console.log(arr)
Dyspnea answered 1/6, 2023 at 20:57 Comment(1)
Please don't inline everything like that... It's very hard to read. Also, it seems reliant on the drives starting with a single letter. If there is actual evidence that this works, please edit that into your answer.Scuffle

© 2022 - 2024 — McMap. All rights reserved.