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.
Asked
Active
Viewed 3,666 times
4 Answers
3
Are you looking for Win32::FileOp?

brian d foy
- 129,424
- 31
- 207
- 592
-
"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 Dec 15 '08 at 08:10
-
You have to install the module first. – brian d foy Dec 15 '08 at 08:20
0
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
- 854,327
- 234
- 1,573
- 1,953
-
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 Dec 15 '08 at 08:11
-
That's why you'd have to process the output to select which one you want. See edited contents. – paxdiablo Dec 15 '08 at 08:44
0
(Really late response, I know) but just today I need something like this and Win32::FileOp would not compile on my system. So I invoked subst and substituted virtual drives with "real"; snippet follows:
use strict;
use Data::Dumper;
use feature 'say';
my $DB=1;
$Data::Dumper::Indent = 1;
$Data::Dumper::Terse = 1;
my %Virt;
exit main();
sub main
{
my $rtn;
my (@args) = @_;
open CMD,"subst|" or die "can't run subst command";
while (<CMD>) {
chomp;
my ($drv, $path) = split(/:\\: => /);
$Virt{$drv} = $path;
}
my %rset; # result set
while (my ($d,$p) = each %Virt) {
$rset{$d} = expand($p);
}
#D say Dumper rset => \%rset;
return $rtn;
}
# recursive call if expanded path has another 'virtual' drive
sub expand
{
my ($loc) = @_;
my $rtn = undef;
my ($drv, $path) = split(/:\\/, $loc);
if ($a = $Virt{$drv}) {
#D say "$a $path";
$rtn = "$a\\$path";
$rtn = expand($rtn);
} else {
#D say "$drv $path";
$rtn = "$drv:\\$path";
}
return $rtn;
}
Notes: I use #D for quickNdirty debug statements
I tested this to three levels i.e. w: subst to x:, x: subst to y: and y: subst to c:

davidc
- 71
- 2