At 08:42 AM 5/2/2001, you wrote:
>         I am working on automating some file imports from a file that are 
> saved on a server to a database on a local machine.  It gets a bit ugly 
> because we need to run the text file through a conversion program from a 
> vendor before it gets imported into the DB, and this conversion utility 
> requires the file to be on a mapped drive.  I want to set this up with 
> the most flexibility I can, so I would rather not just map a specific 
> drive to the sever permanently.  I need to make my program look for an 
> open drive letter, then map the server directory to that drive.  I was 
> thinking of putting a text file that consisted of just one word (like 
> "verify") in the server directory, and then using an until loop to try to 
> map the directory, open the file, and set $verify = the first line of a 
> file.  The until criteria would be something like $verify =~ /verify/ 
> .  Is there a more elegant way to test to see what drives are available 
> to map, and if not, will this method work?  Thanks you in advance for any 
> help that you can offer.

I'm not totally sure I understand your question. I'm guessing that you:

A. Know the share that you will be pulling this file off of. (ie 
\\MyServerName\MySharedDir\myfile.txt )

B. You *just* need to figure out what drive letters are available.

If this is the case ( and correct me if it is not ) your task is relatively 
simple.

Use the Win32API::File module.  There's a function in there called 
'getLogicalDrives' ( among MANY others ) and it returns all of the 'root' 
drive paths.  For instance, on my Win2000 system:

#drives.pl
use strict;
use Win32API::File qw( :Func );

my @drives = getLogicalDrives;

print join "\n", @drives;

produces:

C:\
D:\
E:\

To get just the letter, use a regex to get the drive letter off the front, 
something like:

my ($lastdrive) =  pop (@drives) =~ /^([A-Z])/;

pop gets the last element off the list (sort the original one if you're 
paranoid) and the pattern gets just the letter into $lastdrive.  In the 
case of my system, it would be 'E'.  Then just use an increment operator on 
it, and append the ':\' back on to it, if need be. ( Perl's is nice enough 
to give you the next character in sequence, except 'Z'.  After that it will 
return 'AA', and in this case that would be bad.  You would have to test to 
make sure that $lastdrive isn't 'Z'.)

$lastdrive = ++$lastdrive . ':\\';

And I think that's it.

Thank you for your time,

Sean.

Reply via email to