Viki@Stag has asked for the wisdom of the Perl Monks concerning the following question:

I have this script to check if a mapped drive is disconnected.
my @drives2check = ('S:/','T:/','X:/','D:/'); my @dis_drives = (); foreach my $drive (@drives2check) { print "Checking $drive\n"; opendir (DRIVE, $drive) or do { push @dis_drives, $drive; print "$drive : $!\n"; next; }; close DRIVE; } if (scalar @dis_drives) { # Mail the list of disconnected drives }
What ever drive I put at the end of list @drives2check the script hangs there. I dont understand why this is happening, any help ...

Replies are listed 'Best First'.
Re: Checking status of Network Mapped Drives
by AltBlue (Chaplain) on Aug 04, 2008 at 10:59 UTC

    If all you really want it's to *check* the connection status, I'd avoid using such an approach because it's slow and auto-vivifying (Windows tries to activate each connection you want to read from).

    IMHO a better way would be to query the Windows Management Instrumentation (WMI) infrastructure through DBD::WMI:

    use strict; use warnings; use DBI; my $dbh = DBI->connect('dbi:WMI:'); my $sth = $dbh->prepare(<<'WQL'); SELECT * FROM Win32_NetworkConnection WHERE ConnectionState != 'Connected' WQL $sth->execute() or die $dbh->errstr; while (my ($con) = $sth->fetchrow_array) { printf "%s\t%s\n", $con->ConnectionState, $con->Name; }
Re: Checking status of Network Mapped Drives
by pjotrik (Friar) on Aug 04, 2008 at 08:27 UTC
    Umm, shouldn't that be a closedir, instead of close?
      yup
      perl -e' opendir DIR, q!.!, or die $!;readdir DIR;close DIR or die $! +' Bad file descriptor at -e line 1.
Re: Checking status of Network Mapped Drives
by cdarke (Prior) on Aug 04, 2008 at 08:21 UTC
    Works for me (Windows XP perl 5.10). The handle is not always closed, but I can't see that causing a "hang". It most likely it is a problem in trying to connect with the drive itself.
Re: Checking status of Network Mapped Drives
by Bloodnok (Vicar) on Aug 04, 2008 at 14:31 UTC
    Updated
    Hi ,

    Have you tried letting the OS do the work and use (pun intended:-) the net use command (http://www.cae.wisc.edu/site/public/?title=nt4netuse) in backticks and parsing the output ?? It already 'knows' what drives should & shouldn't be mapped (c/w their status) and hence doesn't hang around...well not much by Windoze standards.

    Just a thought ...

    At last, a user level that overstates my experience :-))