List sections in nodejs

I would like to get a list of partitions on Windows using nodejs. fs.readdir works fine for any folder below or, including C :, but I can't figure out what to give it a list of partitions like "C:", "D:", etc.

Does anyone know what I should use?

+6
source share
4 answers

Node.js does not have an api for enumerating sections. One way is to use the child_process command and execute the wmic command (or any command that 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(); 
+8
source

My 2 cents:

A bit advanced - a callback function for easy integration, returns an array of disks:

 /** * 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(); } 
+3
source

Not sure if it matches exactly what you are looking for, but we create a NodeJS module called drivelist that returns an array of mapped drives with their corresponding mount points (for example: installation letters on 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 this property if this is what you are looking for.

The main advantage of this module is that it works on all major operating systems.

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

+3
source

slightly 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); }); 
0
source

Source: https://habr.com/ru/post/926411/


All Articles