【问题标题】:Getting -Filter or -Include values from .txt file从 .txt 文件中获取 -Filter 或 -Include 值
【发布时间】:2020-09-17 22:00:35
【问题描述】:

我有一个带有文件名的 txt 文件(即01234.tif),我想用它来过滤 Get-ChildItem cmdlet。我做了

$filenames = Get-Content filenames.txt

(也试过| Out-String

然后

Get-ChildItem . -Include $filenames | ForEach {if (!(Test-Path -Path ./jpeg/$_.Basename+".jpg")) {some imagemagick processing}}

但它什么也没做。有趣的部分是它适用于排除,因为

Get-ChildItem . -Exclude $filenames > exclude.txt

写入预期的行数。我在这里错过了什么?

Get-Content filenames.txt | ForEach (path test) {imagemagick}

运行但复制所有项目,因此路径检查或 Get-Content 未按预期工作。

【问题讨论】:

    标签: powershell


    【解决方案1】:

    也许令人惊讶的是,-Include(也称为-Exclude首先应用于(可能是通配符扩展的)输入路径的名称,并且- 仅在匹配的情况下 - then 到由文字路径定位的目录的 children
    如果还使用了-Recurse,则不会出现问题。
    GitHub issue #3304

    因此,使用Get-Item *Get-ChildItem 也可以,但不是必需的),以便 子项 的名称与 -Include 模式匹配:

    Get-Item * -Include (Get-Content filenames.txt)
    

    添加-Force 还包括隐藏项。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多