tags:

views:

771

answers:

3

Hi there

I need to get substed drive letter in Perl. Could anyone kindly help me? $ENV{SYSTEMDRIVE} does not work; it gives me real logical drive letter, not the substed one.

thanxalot for your help

+3  A: 

Are you looking for Win32::FileOp?

brian d foy
"Can't locate Win32/FileOp.pm" and so on, but i am not sure it would help (anyway, thank you for effort)To be exact - I am using Windows XP.
Dungeo
You have to install the module first.
brian d foy
A: 

If you want to do it yourself, you could capture the output of the subst command and process it, since it outputs all current substituted drives.

SUBST [drive1: [drive2:]path]
SUBST drive1: /D
    drive1:        Specifies a virtual drive to which you want to assign a path.
    [drive2:]path  Specifies a physical drive and path you want to assign to
                   a virtual drive.
    /D             Deletes a substituted (virtual) drive.
Type SUBST with no parameters to display a list of current virtual drives.

C:\Documents and Settings\Administrator\My Documents>subst r: c:\bin

C:\Documents and Settings\Administrator\My Documents>subst
    R:\: => C:\bin

In order to do this, you need a function to return the subst'ed output, as follows:

sub get_drive {
    my $drv = shift;
    my $ln;
    $drv = substr($drv,0,1);
    open (IN, "subst |");
    while ($ln = <IN>) {
            chomp ($ln);
            if ((substr($ln,0,1) eq $drv) && (substr($ln,1,6) eq ":\\: =>")) {
                    close (IN);
                    return substr($ln,8);
            }
    }
    close (IN);
    return $drv . ":\\";
}

print get_drive ("R:") . "\n";
print get_drive ("S:") . "\n";

This outputs:

C:\bin
S:\

on my system which has only the one subst'ed drive.

paxdiablo
SUBST with no parameters could help, but I have many substed drives and it displays all of them - I want to get the only one, which I work on currently.
Dungeo
That's why you'd have to process the output to select which one you want. See edited contents.
paxdiablo
A: 

thanks to all, at last I solved it by much easier way - by using getcwd command to get current working directory and then I used first two letters from its output - so simple:-)

use Cwd;

my $driveletter = substr(getcwd, 0, 2);
Dungeo