kirk123 has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Does anyone knows a way to list the drives(c:\,b:\,..) on a pc. I can't use adminmisc.pm. A script that list drives on windows would also be great. --thanks

Replies are listed 'Best First'.
Re: Getting list of drives on pc (Win32API::File)
by tye (Sage) on Apr 16, 2003 at 18:54 UTC
Re: Getting list of drives on pc
by BrowserUk (Patriarch) on Apr 16, 2003 at 19:10 UTC

    perl -MWin32::DriveInfo -e"print Win32::DriveInfo::DrivesInUse();" ACDEGT

    Examine what is said, not who speaks.
    1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
    2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
    3) Any sufficiently advanced technology is indistinguishable from magic.
    Arthur C. Clarke.
Re: Getting list of drives on pc
by Rex(Wrecks) (Curate) on Apr 16, 2003 at 18:25 UTC
    Well, do you want to dicern between network shares and actual partitions? What Windows version? 9x or NT/2K/XP?
    If it is the later (NT/2K/XP) look into parsing the output of net share (provided you have admin rights on the local machine). This still won't get you floppy drives or removable devices but it's a start.

    You have not provided very much info so I'm not exactly sure what you want. Oh, and why can't you use adminmisc?

    "Nothing is sure but death and taxes" I say combine the two and its death to all taxes!
Re: Getting list of drives on pc
by Coruscate (Sexton) on Apr 16, 2003 at 20:49 UTC

    I once had the same need and didn't even think of asking about a module, nor did I look at the Win32:: set of modules. So I did my own little snippet, that went something like this. I think this was it, I don't have a windows box to test it on anymore, so you know... it might not be right.

    my @drives; for my $drive ('A' .. 'Z') { opendir DRIVE, "$drive:/" and push @drives, $drive; } print "Drives: ", (join ', ', @drives), "\n";


    If the above content is missing any vital points or you feel that any of the information is misleading, incorrect or irrelevant, please feel free to downvote the post. At the same time, please reply to this node or /msg me to inform me as to what is wrong with the post, so that I may update the node to the best of my ability.

      I do have a Windows box to test it on and it looks like this largely works. A couple of notes though. My floppy drive is A:, and it brought up a message (twice! and I cancelled both times) indicating that there was no disk in the A: drive. This would be awkward for a non-interactive user. Second thing, my CD drive is D:, and it completely skipped over that when the drive was empty, not recognizing it at all. No error message, no nothing. However, with a CD in the drive, it recognized it the same was as for all the fixed drives. So, caveat emptor with removable-media drives.
        Many Windows installations have A:\ and B:\ aliased to the
        floppy drive, which is probably why you got the message 2x.

        blyman
        setenv EXINIT 'set noai ts=2'

        Ah yes, I could have told you about the not recognizing the empty drive. It will only return a list of drives that currently have a filesystem mounted on it. It simply tries to open the root directory of the drive, and if executed successfully, adds it to the list. As for the alert telling you about the empty floppy drive, this I could have expected but did not see in my use of that snippet. I believe it's because I used it in a CGI script ;)


        If the above content is missing any vital points or you feel that any of the information is misleading, incorrect or irrelevant, please feel free to downvote the post. At the same time, please reply to this node or /msg me to inform me as to what is wrong with the post, so that I may update the node to the best of my ability.

        You can control whether trying to access an empty drive prompts the user. See Win32API::File's SetErrorMode() for details.

                        - tye
      I've seen Randy Kobes do that. Besides the CD issue, sometimes (never seen it in practice) you may need to
      for my $drive ( 'A' .. 'Z', 'AA' .. 'ZZ' ) {
      in case the logical drives number is really high ;)


      MJD says you can't just make shit up and expect the computer to know what you mean, retardo!
      I run a Win32 PPM repository for perl 5.6x+5.8x. I take requests.
      ** The Third rule of perl club is a statement of fact: pod is sexy.

Re: Getting list of drives on pc
by draper7 (Scribe) on Apr 17, 2003 at 14:19 UTC
     Here's my way of doing it! I not sure why you can't use Win32::AdminMisc though? Can you use Win32::Lanman? Note that this only gets LOCAL Disks.
    #!/usr/bin/perl -w # # requires win32::lanman # get it from http://jenda.krynicky.cz/ # or install from ppm (set repository Jenda http://Jenda.Krynicky.cz/p +erl) # or on ppm 3.0.1 (repository add Jenda http://Jenda.Krynicky.cz/perl) # use strict; use Win32::Lanman; my @drives; my @computerlist = ("computer_a","computer_b"); foreach my $computer (@computerlist) { chomp $computer; Win32::Lanman::NetServerDiskEnum("\\\\$computer",\@drives); } foreach my $drive (@drives) { print "$drive\n"; }


    --JD