如何在 File::Find::Rule 中的“or”替代中使用 mindepth 和 maxdepth?

2023-11-29

我有以下文件夹结构(作为一个最小的示例):

dir
├── a
│   ├── b
│   │   ├── c.txt
│   │   └── d.txt
│   └── c
│       └── q.txt
├── b
│   ├── bb.txt
│   └── d
│       └── dd.txt
└── q.txt

我想找到所有.txt文件,但排除其中的所有内容dir/b从搜索中使用File::Find::Rule(我知道我可以使用轻松地做到这一点File::Find但这需要对我的代码进行大量重构)。

我尝试了以下方法:

use feature qw(say);
use strict;
use warnings;
use File::Find::Rule;

my $dir = 'dir';
my @files = File::Find::Rule->or(
    File::Find::Rule->directory->mindepth(1)->maxdepth(1)
                    ->name( 'b' )->prune->discard,
    File::Find::Rule->name('*.txt'),
)->in( $dir );
say for @files;

Output:

dir/q.txt
dir/a/c/q.txt

预期产出:

dir/q.txt
dir/a/b/d.txt
dir/a/b/c.txt
dir/a/c/q.txt

似乎maxdepth() and mindepth()不在里面工作or()功能。


就像find命令行工具,maxdepth不限制匹配发生的地点;它限制了实际的遍历。

maxdepth( $level )

最多下降$level(非负整数)起始点以下的目录级别。

就像find命令行工具,mindepth防止在特定深度之前执行所有测试。

mindepth( $level )

不要在低于以下的级别进行任何测试$level(非负整数)。

Given what they do, they affect the entire search. As such, it's no surprise the mindepth and maxdepth from the outer rule object is the one used and the others are ignored.[1]


相同的解决方案适用于find这里可以使用命令行工具。

find:

$ find dir -wholename dir/b -prune -o -name '*.txt' -print
dir/a/b/c.txt
dir/a/b/d.txt
dir/a/c/q.txt
dir/q.txt

文件::查找::规则:

$ perl -MFile::Find::Rule -e'
   my ($dir) = @ARGV;
   CORE::say for
      File::Find::Rule
         ->or(
            File::Find::Rule->exec(sub { $_[2] eq "$dir/b" })->prune->discard,
            File::Find::Rule->name("*.txt"),
         )
         ->in($dir);
' dir
dir/q.txt
dir/a/b/c.txt
dir/a/b/d.txt
dir/a/c/q.txt

另一种方法是使用 File::Find::Rule 构建要搜索的目录列表,然后使用 File::Find::Rule 的其他用途搜索这些目录。 (Perl 相当于find ... -print0 | xargs -0 -I{} find {} ....)


  1. The find命令行实用程序以不同的方式处理错误的输入。

    $ find dir -type d -mindepth 1 -maxdepth 1 -name b -prune -o -name '*.txt' -print
    find: warning: you have specified the -mindepth option after a non-option argument (, but options are not positional (-mindepth affects tests specified before it as well as those specified after it).  Please specify options before other arguments.
    
    find: warning: you have specified the -maxdepth option after a non-option argument (, but options are not positional (-maxdepth affects tests specified before it as well as those specified after it).  Please specify options before other arguments.
    
    dir/q.txt
    
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 File::Find::Rule 中的“or”替代中使用 mindepth 和 maxdepth? 的相关文章

随机推荐

  • iOS 库到 BitCode

    我最近下载了 Xcode 7 beta Xcode 抱怨我的一些 C 库没有编译成 BitCode 我该如何告诉 Clang 生成与 iOS 兼容的 BitCode 我在 stackoverflow 上看到过类似的答案 但我不知道它们是否适
  • 如何将元组数据提取为单元素格式

    我从以下内容中得到了良好的结果 但是如何从元组中提取该数据 换句话说 如何清理数据 这是数据库里的数据 我跑出来了 gt gt gt policy id 2309L 118L 94L gt gt gt for i in policy id
  • Visual Studio代码EPERM操作不允许

    每次我尝试在 vsc 上安装新扩展时 我都会得到 Error while loading extensions EPERM operation not permitted 接下来它告诉我打开一个 obsolete 文件 但它告诉我的文件路径
  • 在没有 Java EE 应用服务器的情况下使用 Web 服务在 C# 和 Java 之间进行互操作?

    我的处境很困难 我们有一个公开基于 Java 的 API 的第三方企业系统 然而 我们是一个100 Net 导向的开发团队 本质上 我需要用 C 代码可以调用的东西来包装 Java API Web 服务固然很棒 但我们的基础设施上唯一支持的
  • 从网址中删除 web/app_dev.php/

    我已经在 symfony 2 中完成了我的应用程序 现在我想从网址中删除 web app dev php 我读到了这一点 并在这样做之后 php app console cache clear env prod no debug 并添加 h
  • 创建 libcurl http post 表单

    我如何创建一个curl form 例如在stackoverflow上发帖 如果我查看问题表单页面的来源 我会看到
  • 有没有办法获取队列中的最后一个元素?

    我知道堆栈是最好也是最简单的方法 但是是否有可能获得队列中的最后一个元素而无需将任何内容出列 您可以简单地执行以下操作 Assumes T is a reference type if it s a value type then you
  • 删除文本文件中的特定行

    我正在研究一个选项 如果用户输入确切的标题和作者 该选项将能够删除指定的行 但是我无法让它发挥作用 我的功能内容如下所示 fnRemoveBook echo Title read Title echo Author read Author
  • 如何在java中从tcp流播放声音

    还有另一个应用程序在此套接字上写入原始 wav 文件 客户端启动并开始收听当前正在播放的歌曲 Socket clientSocket new Socket localhost 9595 AudioInputStream stream Aud
  • TypeScript 错误 TS2339:“EventTarget”类型上不存在属性“matches”

    我收到一个我无法从 TypeScript 中理解的错误 我正在使用一段完全有效的 JavaScript 但它在我的 IDE 中以及通过 Gulp 进行预处理期间都标记了错误 我已将其剥离回其核心 但仍然收到错误 即使这是完全有效的 JS d
  • 将段落的每一行包裹在一个跨度中

    我有一个 div 元素 它将显示一个没有换行符的段落 如示例中所示 div Lorem Ipsum is simply dummy text of the printing and typesetting industry Lorem Ip
  • 无法同时满足约束 - 没有适当的约束

    我已经检查并删除了每个用户限制 但仍然收到以下错误ONLY旋转设备后 我完全不知道为什么 有人有什么想法吗 2013 01 14 21 30 31 363 myApp 35869 c07 Unable to simultaneously s
  • 声纳添加新项目

    我正在尝试添加一个新项目到sonar 运行声纳跑步者时 我收到以下错误 任何人都可以帮助我解决这个问题 sonar runner Runner configuration file opt lampp htdocs typo3 sonar
  • 如何在不使用 SQLAlchemy 引擎的情况下将数据帧写入 Postgres 表?

    我有一个数据框 我想写入Postgres数据库 此功能需要成为Flask app 现在 我通过创建一个单独的脚本来运行此插入部分SQLAlchemy 引擎并将其传递给df to sql 将数据框写入数据库表 但是当我将此功能集成到 Flas
  • 将不同大小的圆形打包成矩形 - d3.js

    我试图打包圈子不同尺寸放入一个长方形容器中 不包装在圆形容器中d3 js捆绑在一起 在下面d3 layout pack 这是我想要实现的布局 我找到了这张纸在这个问题上 但我不是数学家 无法彻底理解这篇文章并将其转换为代码 任何人都可以建议
  • Django 简单标签在 if 条件下不起作用

    我想通过使用审核工具添加块来自定义 django admin 的视频对象更改表单 当我在 if 条件下使用自定义 simpletags 时 它不起作用 模型 py class Video models Model class Meta db
  • Spring RestController - 找不到 java.lang.Integer 类型的返回值的转换器

    我目前正在尝试创建我的第一个带弹簧的休息服务 我不想让他回来XML结果 基于JAXB 在一个非常基本的休息控制器上 import org springframework web bind annotation import java uti
  • 如何获取未过时的位置?

    启用 GPS 后 GPS 需要一段时间才能准备就绪 因此 使用此代码可能会获得已过时的位置 位置 locationManager getLastKnownLocation LocationManager GPS PROVIDER 我无法使用
  • Safari set-cookie 不适用于第一方 cookie

    当我登录时 我返回到浏览器 Overview URL https subdomain domain de 8444 api auth login Status 200 Source Network Adresse xxx xxx x xx
  • 如何在 File::Find::Rule 中的“or”替代中使用 mindepth 和 maxdepth?

    我有以下文件夹结构 作为一个最小的示例 dir a b c txt d txt c q txt b bb txt d dd txt q txt 我想找到所有 txt文件 但排除其中的所有内容dir b从搜索中使用File Find Rule