如何查找名称可能比预期名称长的文件?

发布于 2024-11-07 07:02:11 字数 537 浏览 0 评论 0原文

我正在做一些测试,我有很多文件需要执行数据分析。我们的文件有命名约定,但有时有人会在文件名中添加更多内容。我正在寻找一种方法来查找名称的“核心”,然后保存整个文件名。

例如,我想查找 WIRA_Rabcd_RT,但有人可能将文件名保存为 RED_GREEN_BLUE_WIRA_Rabcd_RT.txt,所以我的文件夹将如下所示:

RED_GREEN_BLUE_WIRB_Rabcd_RT.txt
RED_GREEN_BLUE_WIRC_Rabcd_RT.txt
RED_GREEN_BLUE_WIRA_Rabcd_RT.txt ← I want to find this file, and open it.
RED_GREEN_BLUE_WIRF_Rabcd_RT.txt
RED_GREEN_BLUE_WIRG_Rabcd_RT.txt
RED_GREEN_BLUE_WIRT_Rabcd_RT.txt
RED_GREEN_BLUE_WIRW_Rabcd_RT.txt
RED_GREEN_BLUE_WIRQ_Rabcd_RT.txt

I am doing some testing where I have a lot of files that I need to perform data analysis on. We have a naming convention with our files, but sometime someone will add a little more to the file name. I'm looking for a way to look for the "core" of the name and then save the entire file name.

For example, I want to find WIRA_Rabcd_RT, but someone may have saved the file name as RED_GREEN_BLUE_WIRA_Rabcd_RT.txt, so my folder will look something like this:

RED_GREEN_BLUE_WIRB_Rabcd_RT.txt
RED_GREEN_BLUE_WIRC_Rabcd_RT.txt
RED_GREEN_BLUE_WIRA_Rabcd_RT.txt ← I want to find this file, and open it.
RED_GREEN_BLUE_WIRF_Rabcd_RT.txt
RED_GREEN_BLUE_WIRG_Rabcd_RT.txt
RED_GREEN_BLUE_WIRT_Rabcd_RT.txt
RED_GREEN_BLUE_WIRW_Rabcd_RT.txt
RED_GREEN_BLUE_WIRQ_Rabcd_RT.txt

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

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

发布评论

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

评论(2

眼趣 2024-11-14 07:02:11

glob 函数似乎可以解决问题:

my $dir = '/some/directory/';
my @files = glob($dir . '*WIRA_Rabcd_RT.txt');

# Make sure you get exactly one file, open it, etc.

The glob function seems like it would do the trick:

my $dir = '/some/directory/';
my @files = glob($dir . '*WIRA_Rabcd_RT.txt');

# Make sure you get exactly one file, open it, etc.
音盲 2024-11-14 07:02:11

在 Perl TIMTOWTDI 中,这是另一种方式:

#!/usr/bin/perl

use strict;
use warnings;

my $dir = 'dirname';
my $pattern = 'WIRA_Rabcd_RT';

my @files;

{ 
  opendir my $dir_handle, $dir;
  @files = grep { /$pattern/ } readdir $dir_handle;
} #end-of-block autocloses lexical $dir_handle 

@files 现在包含目录中与模式匹配的文件的名称。请注意,名称是相对于该目录的(存储为 name 而不是 dir/name)。然后我经常使用 File::chdir 模块将工作目录更改为 $dir 以处理这些文件。例如:

use File::chdir

# get @files in $dir as above...

{
  local $CWD = $dir;
  # work with files in @files
} 

# working directory restored to original here

In Perl TIMTOWTDI so here is another way:

#!/usr/bin/perl

use strict;
use warnings;

my $dir = 'dirname';
my $pattern = 'WIRA_Rabcd_RT';

my @files;

{ 
  opendir my $dir_handle, $dir;
  @files = grep { /$pattern/ } readdir $dir_handle;
} #end-of-block autocloses lexical $dir_handle 

@files now contains the names of the files in the directory that matches the pattern. Note that the names are relative to that directory (stored as name not dir/name). I often then use the File::chdir module to change the working directory to $dir to work on those files. For example:

use File::chdir

# get @files in $dir as above...

{
  local $CWD = $dir;
  # work with files in @files
} 

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