如何在 Perl 中获取替换的驱动器号?

发布于 2024-07-09 22:43:11 字数 91 浏览 13 评论 0原文

我需要在 Perl 中获取替换的驱动器号。 有人可以帮助我吗? $ENV{SYSTEMDRIVE} 不起作用; 它给了我真正的逻辑驱动器号,而不是替换的驱动器号。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

鲜肉鲜肉永远不皱 2024-07-16 22:43:11

您是否在寻找 Win32::FileOp

Are you looking for Win32::FileOp?

九公里浅绿 2024-07-16 22:43:11
 perl -e 'use Cwd; print( substr(getcwd(),10,1 )) ' # prints 10th char.
 perl -e 'use Cwd; print( substr(getcwd(),10,1 )) ' # prints 10th char.
嘴硬脾气大 2024-07-16 22:43:11

如果您想自己执行此操作,则可以捕获 subst 命令的输出并对其进行处理,因为它会输出所有当前替换的驱动器。

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

为此,您需要一个函数来返回替换后的输出,如下所示:

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";

此输出:

C:\bin
S:\

在我的系统上,该系统只有一个替换后的驱动器。

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.

狂之美人 2024-07-16 22:43:11

(我知道回复确实很晚)但就在今天我需要这样的东西,而 Win32::FileOp 无法在我的系统上编译。 所以我调用了 subst 并将虚拟驱动器替换为“real”; 代码片段如下:

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;
}

注意:我使用#D 进行quickNdirty 调试语句,

我对此进行了三个级别的测试,即 w: subst 到 x:、x: subst 到 y: 和 y: subst 到 c:

(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:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文