list partitions in nodejs

前端 未结 4 1282
说谎
说谎 2020-12-30 11:20

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 hav

4条回答
  •  太阳男子
    2020-12-30 11:43

    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();
    }
    

提交回复
热议问题