0

可能重复:
给定文件的完整路径,我如何只获取没有文件名的路径?

我的完整路径可以有这些类型的模式。如何获取目录路径?

file=M:\ccadm01_IACB_DEV\Informatica_AVOB\IACB_src\parm\d_param_ZRK176E
file=M:\ccadm01_IACB_DEV\Informatica_AVOB\IACB_src\parm\p_param_HCB@002.txt
file=M:\ccadm01_IACB_DEV\Informatica_AVOB\IACB_src\parm\p_param_QCB@B006.1.txt

谢谢吉荣

4

2 回答 2

1

如前所述,您可以使用许多 CPAN 模块,这意味着您可以避免进行字符串操作。例如

use File::Basename 'fileparse';

my @files = qw(
    file=M:\ccadm01_IACB_DEV\Informatica_AVOB\IACB_src\parm\d_param_ZRK176E
    file=M:\ccadm01_IACB_DEV\Informatica_AVOB\IACB_src\parm\p_param_HCB@002.txt
    file=M:\ccadm01_IACB_DEV\Informatica_AVOB\IACB_src\parm\p_param_QCB@B006.1.txt
);

my @dirs = map { (fileparse($_))[1] } grep { s/^file=// } @files;
print join "\n", @dirs;
于 2012-12-19T03:43:10.420 回答
0

这能解决你的问题吗

my @list = qw(
  file=M:\ccadm01_IACB_DEV\Informatica_AVOB\IACB_src\parm\d_param_ZRK176E
  file=M:\ccadm01_IACB_DEV\Informatica_AVOB\IACB_src\parm\p_param_HCB@002.txt
  file=M:\ccadm01_IACB_DEV\Informatica_AVOB\IACB_src\parm\p_param_QCB@B006.1.txt
);

for my $line (@list) {
  my @split_path =  split(/\\/, $line);
  for my $i (1..$#split_path) {
    print @split_path[$i], "/";
  }
  print "\n";
}
于 2012-12-19T03:01:58.950 回答