【问题标题】:Powershell - Problem with Get-Childitem -includePowershell - Get-Childitem -include 的问题
【发布时间】:2020-03-28 14:52:34
【问题描述】:

我正在尝试编写一个 PowerShell 脚本,它允许用户传入一个参数,其中包含他们想要处理的多种类型的文件(即 *.txt 和 *.docx)。当我使用 -include 选项运行 Get-Childitem 命令并手动输入文件类型时,它可以工作文件。但是,当我尝试为 -include 掩码提供变量时,它不起作用(不返回任何内容)。

仅当我使用多种文件类型时才会出现此问题 $incFiles = “””.txt””`, “”.docx”””

如果我使用单一文件类型,它就可以正常工作。 $incFiles = “*.txt”

$incFiles = ""
$incFiles = """*.txt""`, ""*.docx"""
Write-Host "Manual Method:" '"*.txt", "*.docx"'
Write-Host "Calcul Method:" $incFiles

Write-Host "`nManual method works"
Get-ChildItem -path "C:\users\paul\Downloads" -Recurse -Include "*.txt", "*.docx"

Write-Host "This does not work"
Get-ChildItem -path "C:\users\paul\Downloads" -Recurse -Include $incFiles

Write-Host "End"

结果

Manual Method: "*.txt", "*.docx"
Calcul Method: "*.txt", "*.docx"

Manual method works

Directory: C:\users\paul\Downloads\Test2
Mode                LastWriteTime         Length Name                                                                                                                           
----                -------------         ------ ----                                                                                                                           
-a----        3/28/2020   9:54 AM              0 File1.txt                                                                                                                      
-a----        3/28/2020   9:55 AM              0 File2.docx                                                                                                                     

This does not work

End

【问题讨论】:

  • $incFiles = """*.txt"", ""*.docx"""` --> $incFiles = "*.txt", "*.docx"。你“过度引用”,甚至还有反引号。
  • 添加到 Theo 的评论中,您的“手动”方法是创建一个由两个单独的字符串值 *.txt*.docx 组成的数组,但由于您的“过度引用”,$incFiles 是一个字符串值"*.txt", "*.docx".

标签: powershell include


【解决方案1】:

Theomclayton 在 cmets 中提供了有关该问题的关键指针:

$incFiles = """*.txt""`, ""*.docx"""

创建一个单个字符串,其逐字逐句"*.txt", "*.docx"结尾。

顺便说一句:由于,"..."expandable string)中没有特殊含义,因此不需要反引号 (`),即 PowerShell 的转义字符。

虽然看起来是一个数组字面量,但你可以将它直接传递给一个接受字符串值数组的参数(例如-Include 在这种情况下),您传递的是一个 single string,它被解释为 single 通配符模式,因此不起作用。

您尝试做的事情需要 PowerShell 解析作为 PowerShell 源代码 传递的字符串的 内容 - 即,需要额外的一轮评估 -它(幸运的是)没有这样做。[1]

PowerShell 与其他 shell 不同,能够将任何类型的值作为直接参数传递给命令,无论是通过变量(在您的情况下为$incFiles)还是通过表达式/嵌套命令,使用(...);例如,Write-Host -Foreground Yellow ('-' * 10))

因此,将您的 $incFiles 变量构造为 字符串数组,并按原样传递该数组 strong> 到-Include 参数:

# Create a string array of wildcard expressions.
# (an [object[]] instance whose elements are of type [string]).
# Note the use of '...' rather than "...", which clearly signals that
# the enclosed text should be taken *verbatim* (rather than being subject to
# *expansion* (string interpolation).
$incFiles = '*.txt', '*.docx'

# Pass the array as-is to -Include
Get-ChildItem -path "C:\users\paul\Downloads" -Recurse -Include $incFiles

有关 PowerShell 如何解析传递给命令的不带引号的参数的全面概述,请参阅this answer


[1] 相比之下,类似 POSIX 的 shell(例如 bash)确实通过所谓的 shell 扩展在有限的范围内对未加引号的变量引用应用额外的评估 - 请参阅 @ 987654325@.

【讨论】:

    猜你喜欢
    • 2019-02-17
    • 1970-01-01
    • 1970-01-01
    • 2014-08-29
    • 1970-01-01
    • 2011-02-22
    • 2016-02-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多