这段代码应该给出文件的扩展名和地址,但是它的表现并不是很清楚。
($FileName, $TPath, $suffix) = fileparse($SourceFiles[$Index], '\.[^\.]*');
答案 0 :(得分:1)
\.
与文字.
匹配,[^\.]*
匹配所有内容,直到.
出现。基本上,它将与扩展名匹配。
但正如@gaussblurinc所说,它本身也会匹配.
,尝试修改如下:
qr(\.[^\.]+) # regex operator, catch dot (.) and everything behind it but not dot ([^\.])
尝试试验here。我已经设置了一点。
来自文档(fileparse):
my($filename, $directories, $suffix) = fileparse($path);
my($filename, $directories, $suffix) = fileparse($path, @suffixes);
my $filename = fileparse($path, @suffixes);
#If @suffixes are given each element is a pattern (either a string or
#a "qr//") matched against the end of the $filename. The matching
#portion is removed and becomes the $suffix.
# On Unix returns ("baz", "/foo/bar/", ".txt")
fileparse("/foo/bar/baz.txt", qr/\.[^.]*/);
所以,我想搜索目录和输出或对图像执行某些操作(调整它们的大小,例如使用convert
)
use File::Spec;
use File::Basename;
sub searchAndResizeImages{
my ($searchDirectory) = shift;
# use find or whatever you want, I prefer to use glob, oh
# prepare images suffixes
# for example, I know about png, gif, jpg, whatever else?
my @suffixes = qw(png gif jpe?g);
# and also try this: different output, but it can be more useful in your task
# my @suffixes = map{".$_"}qw(png gif jpe?g);
for (glob(File::Spec->catfile($searchDirectory, "*"))){
if (!-d) { # if not directory
my ($file, $path, $suffix) = fileparse($_,@suffixes);
if ($suffix){
print "file : $file\n";
print "path : $path\n";
print "suffix : $suffix\n";
# do whatever you want here with images
}
}
}
}
searchAndResizeImages('.');
答案 1 :(得分:1)
fileparse
是来自File::Basename
my($filename, $directories, $suffix) = fileparse($path);
my($filename, $directories, $suffix) = fileparse($path, @suffixes);
my $filename = fileparse($path, @suffixes);
fileparse()
例程将文件路径划分为$ $目录,$ filename和(可选)文件名$ suffix。如果给出
@suffixes
,则每个元素都是一个模式(字符串或qr//
)与$filename
的末尾匹配。匹配部分将被删除并成为$suffix
。
在这种情况下,后缀定义为包括上一个句点.