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

I want to test if I can write to a mapped drive. The code below does this by simply:
1 seeing if –d test means the drive can be seen;
2 trying to open a ‘test’ file and write one word.
use strict "vars"; my ($map_letter, $en_num, $en_mes, $net_file, $res_open); $map_letter = "H:\\"; if(-d $map_letter) { $net_file = ">" . $map_letter . "netfile.txt"; $res_open = open(NETOP, $net_file); if($res_open == 1) { print NETOP "Hello\n"; close(NETOP); print "netfile print OK\n"; } else { $en_num = Win32::GetLastError(); $en_mes = Win32::FormatMessage($en_num); print "netfile print failed number <$en_num>\nmessage <$en_mes +>\n"; } } else { $en_num = Win32::GetLastError(); $en_mes = Win32::FormatMessage($en_num); print "could not find mapped drive <$map_letter>\nnumber <$en_num> +\nmessage <$en_mes>\n"; }

I wondered if there was a more elegant way:
1 where I could find out what the mapped drive was set to;
2 to tell if the drive was shared in a way that I could write the file.

Replies are listed 'Best First'.
Re: Test File Writing to Mapped Drive
by Crackers2 (Parson) on Oct 23, 2005 at 23:15 UTC

    Not an answer to your questions, but a word of caution: write permissions (unless you're on an ancient windows version) are file- and directory-based, not drive-based. So while your code does check whether you can create a file in the root directory of that drive, this does not necessarily mean that you can read from or write to existing files in that directory, or that you can even access subdirectories on that drive.

Re: Test File Writing to Mapped Drive
by sgifford (Prior) on Oct 24, 2005 at 05:15 UTC
    The -w file test operator is supposed to tell you if you can write to a file or directory; not sure how it works with Windows and its ACL-based permissions.
Re: Test File Writing to Mapped Drive
by swampyankee (Parson) on Oct 24, 2005 at 15:41 UTC

    Just out of idle curiousity, have you tried Win32::FileSecurity?

    emc