oldboy
oldboy

Reputation: 5954

Get Used/Free Disk Space for All Drives on All Platforms

I am creating an Electron app that helps me manage my disk space. However, I would like it to work on Linux/UNIX too.

I wrote the following code, which works great for Windows, but not Linux/UNIX systems.

window.onload = function(){
  const cp = require('child_process')
  cp.exec('wmic logicaldisk get size,freespace,caption', (error, stdout)=>{
    let drives = stdout.trim()split('\r\r\n')
      .map(value => value.trim().split(/\s{2,0}/))
      .slice(1)
  })
}

The output looks like this.

[
  ["560232439808",  "C:", "999526756352",  "System"  ]
  ["999369699328",  "D:", "999558213632",  "SSD"     ]
  ["1511570386944", "E:", "8001545039872", "Get"     ]
  ["4620751712256", "F:", "8001545039872", "BR"      ]
  ["788449492992",  "G:", "4000650883072", "Seen"    ]
  ["2296009408512", "H:", "4000768323584", "Seen 2"  ]
  ["3594248679424", "I:", "8001545039872", "2160"    ]
  ["3507750227968", "J:", "8001545039872", "1080"    ]
  ["945300619264",  "K:", "999625322496",  "Trailer" ]
]

Since I am unfamiliar with Linux/UNIX, I am wondering how I can achieve the same output for Linux/UNIX too?

Upvotes: 5

Views: 4074

Answers (1)

Joshua
Joshua

Reputation: 5322

There probably isn't a command that works across all platforms.

But what you can do is get the current platform with process.platform and run a different command on each platform.

For example:

const cp = require('child_process');

if (process.platform == 'win32') { // Run wmic for Windows.
    cp.exec('wmic logicaldisk get size,freespace,caption', (error, stdout)=>{
      let drives = stdout.trim()split('\r\r\n')
        .map(value => value.trim().split(/\s{2,0}/))
        .slice(1)
    });
} else if (process.platform == 'linux') { // Run df for Linux.
    cp.exec('df', (error, stdout)=>{
        // Do your magic here.
    });
} else {
    // Run something for a mac.
}

You can read about process.platform here.

Upvotes: 6

Related Questions