我期待通過Perl來獲取路徑,而不在Windows中的文件名如何在使用Perl的Windows中獲得沒有文件名的路徑?
我有這樣的完整路徑:
c:\My_Designs\ipcore_script\test\src\IP_CORE\mux\sus.do
我只需要路徑和要剝離,如文件名:
c:\My_Designs\ipcore_script\test\src\IP_CORE\mux
我期待通過Perl來獲取路徑,而不在Windows中的文件名如何在使用Perl的Windows中獲得沒有文件名的路徑?
我有這樣的完整路徑:
c:\My_Designs\ipcore_script\test\src\IP_CORE\mux\sus.do
我只需要路徑和要剝離,如文件名:
c:\My_Designs\ipcore_script\test\src\IP_CORE\mux
> perl -MFile::Basename -wE "say dirname(qq(c:/perl/bin/perl.exe));"
c:/perl/bin
您還可以使用Path::Class:
#!/usr/bin/env perl
use strict;
use warnings;
use Path::Class;
my $file = file('c:\My_Designs\ipcore_script\test\src\IP_CORE\mux\sus.do');
print $file->parent, "\n";
輸出:
C:\My_Designs\ipcore_script\test\src\IP_CORE\mux
你也可以試試這個
$path='c:\My_Designs\ipcore_script\test\src\IP_CORE\mux\sus.do';
my $pathname=substr($path, 0, rindex($path,"\\"));
Result:
c:\My_Designs\ipcore_script\test\src\IP_CORE\mux
文件::規格是另一種選擇。 (幾年前,我從File :: Basename切換到File :: Spec;如果我記得爲什麼我這麼做,我會跟進)。 –
@KeithThompson'注意:File :: Basename的dirname()和basename()模擬同名的shell和C函數的行爲和怪癖。如果你的問題只是解析路徑,使用File :: Spec的splitpath()和splitdir()方法更安全。# – unixman83