0

What I'd like to do is detect all of the top-level directories (i.e. C:\, A:\, X:\, et cetera) on Windows in light of the fact that This PC is seemingly inaccessible.

The following code works, but is obviously not ideal and has many drawbacks. For instance, I don't believe the namespace is even limited to A to Z, which would create major problems with such an approach.

const
  fs = require('fs'),
  ab = 'abcdefghijklmnopqrstuvwxyz'.split('')

ab.forEach(v => fs.existsSync(`${v}:\\`))

Is anybody aware of any other way to do this?

oldboy
  • 5,729
  • 6
  • 38
  • 86
  • 1
    Google gives me [this package](https://www.npmjs.com/package/windows-drive-letters). – Ken Y-N Dec 16 '19 at 00:17
  • 1
    https://www.npmjs.com/package/drivelist - OS agnostic – Jaromanda X Dec 16 '19 at 00:17
  • @KenY-N thanks ill check it out. – oldboy Dec 16 '19 at 00:18
  • @KenY-N - That google package executes `wmic logicaldisk get caption` in a child_process and uses those results. – jfriend00 Dec 16 '19 at 00:58
  • @jfriend00 i was looking at the source code on github and noticed that. is that not a good thing? all this stuff is new to me – oldboy Dec 16 '19 at 01:02
  • 1
    @JaromandaX - https://www.npmjs.com/package/drivelist supports multiple operating systems via native code, and thus requires compiling. It does appear to support mac, windows and linux. It's really more that it supports multiple OSes than it's OS agnostic. – jfriend00 Dec 16 '19 at 01:02
  • It's OK to call a child_process to execute a built-in app as long as you know that app is properly installed and in the path (or know where to find it). The only vulnerability if you're not passing any user-specified stuff into the child_process is if someone somehow messes with the OS and replaces the program `wmic` with something else. If they can do that, however, they could also be messing with your node.js app in more nefarious ways. – jfriend00 Dec 16 '19 at 01:06
  • @jfriend00 ok then are you aware of any safe and efficient ways to identify all of the root directories on a given PC? – oldboy Dec 16 '19 at 02:21
  • I am not aware of any ways other than the two already-posted options. FYI, in Windows, the proper term would not be "root directories", but more something like "mounted volumes" or in some cases just "drives", though there can be a top level mounted volume that does not have a drive letter assigned so it's not really a drive. – jfriend00 Dec 16 '19 at 02:53
  • All the solutions listed here [Enumerate system drives in nodejs](https://stackoverflow.com/questions/15878969/enumerate-system-drives-in-nodejs) and [List disk partitions in nodejs](https://stackoverflow.com/questions/12622758/list-partitions-in-nodejs) all use similar techniques to the ones already listed here. Lots of other command line choices here: [How to get list of drivers letters](https://serverfault.com/questions/62578/how-to-get-a-list-of-drive-letters-on-a-system-through-a-windows-shell-bat-cmd) and these would also all require child_process. – jfriend00 Dec 16 '19 at 02:55

1 Answers1

1

You could do something like this if you're okay with using child_process:

const child = require('child_process');

child.exec('wmic logicaldisk get name', (error, stdout) => {
    const drives = stdout.split('\r\r\n')
        .filter(value => /[A-Za-z]:/.test(value))
        .map(value => value.trim())

    //do stuff with drives
});

drives will be an array of available drives like ['C:', ...]

Michael Rodriguez
  • 2,142
  • 1
  • 9
  • 15
  • i will look into this. does using `child_process` introduce vulnerabilities or why would i not be okay using it? – oldboy Dec 16 '19 at 00:21
  • As long as you use it like this there is no real drawback. The main thing to keep in mind is that you don't want to allow command injection when using it. In this case, we've hardcoded the command, so it can't be exploited in any way. – Michael Rodriguez Dec 16 '19 at 00:25
  • ok cool. i should be getting some free time to test everything within the week. ill let u know how it goes. thanks again – oldboy Dec 16 '19 at 00:28
  • beautiful. what is all of that other info that it spits out in cmd? also, to see if i understand whats happening, it basically opens up a shell to execute the OS command, and then returns whatever, correct? – oldboy Dec 16 '19 at 04:10
  • For me stdout is just `Name\nC:\nD:\nE:`. Yeah it spawns a child process with a shell and executes the provided commands in that shell, then pipes back the output as stdout :) – Michael Rodriguez Dec 16 '19 at 04:14
  • yeah, but i forewent including the `filter` method to not only see what i was dealing with, but also to avoid maybe filtering out drives that do exist but dont exist in that given namespace – oldboy Dec 16 '19 at 04:15
  • yeah when i console.log(stdout) that is the unfiltered response from my shell – Michael Rodriguez Dec 16 '19 at 04:16
  • this may be a dumb question but this only works on windows because its a windows shell command correct? – oldboy Dec 16 '19 at 10:35
  • Actually child_process itself would work on any platform, this example in particular only works in windows because we are executing windows based commands “wmic” – Michael Rodriguez Dec 16 '19 at 14:15
  • yah ok thats what i meant/thought. thanks again mike – oldboy Dec 18 '19 at 03:28