Perl 获取父文件夹名称 [英] Perl Get Parent Folder Name
本文介绍了Perl 获取父文件夹名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用 File::Find 获取父目录名称的解决方案是什么.我知道如何仅获取文件名或仅获取目录路径,但我不知道如何为最后一个包含目录执行此操作.
What is the solution to get the name of the parent directory using File::Find. I know how to get only the filename or only the directory path but I don't know how to do this for the last containing directory.
例如,如果目录是 /dir_1/dir_2/dir_3/.../dir_n/*.txt
我需要获取 'dir_n
' 名称.
For example, if the directory is /dir_1/dir_2/dir_3/.../dir_n/*.txt
I need to get the 'dir_n
' name.
use strict;
use warnings;
use File::Find;
my $dir = "some_path";
find(\&file_handle, $dir);
sub file_handle {
/\.txt$/ or return;
my $fd = $File::Find::dir;
my $fn = $File::Find::name;
# ...
}
推荐答案
给定目录路径,然后应用 File::Basename(另一个核心模块)到获得目录最后部分的路径.
Given the directory path, you then apply File::Basename (another core module) to the path to obtain the last portion of the directory.
use strict;
use warnings;
use File::Find;
use File::Basename;
my $dir = "some_path";
find(\&file_handle, $dir);
sub file_handle {
/\.txt$/ or return;
my $fd = $File::Find::dir;
my $fn = $File::Find::name;
my $dir = basename($fd);
# ....
}
这篇关于Perl 获取父文件夹名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文