【问题标题】:Powershell - Copy folders with names matching arrayPowershell - 复制名称匹配数组的文件夹
【发布时间】:2021-06-20 15:50:14
【问题描述】:

这就是我想要完成的事情

  1. 运行sql查询-结果输出到txt文件
  2. txt 文件到数组的结果
  3. 如果文件夹名称与数组中的值匹配,则获取目录中的子项-文件夹和文件
  4. 将步骤 3 中返回的项目复制到另一个目录

我遇到的问题是根据数组的值过滤目录中的项目

gci $sourcepath -recurse | where {$_.name -like $idlist} | Copy-Item -Destination $DestinationHistoryPath

【问题讨论】:

  • 什么是$idlist?请提供minimal reproducible example
  • 我会附和@zett42 所说的话。我会更进一步,问您是否检查过-in 比较?

标签: sql powershell filter


【解决方案1】:

我假设您已经正确加载了数组。对于此特定场景,您不希望使用 -like 运算符。最好使用-in-Contains。像下面这样的东西可能会起作用:

Get-ChildItem $sourcepath -Recurse | 
Where-Object {$_.Name -in $idlist} | 
Copy-Item -Destination $DestinationHistoryPath

如果你使用包含,你需要像这样切换操作数:

Get-ChildItem $sourcepath -recurse | 
Where-Object { $idlist -contains $_.Name } | 
Copy-Item -Destination $DestinationHistoryPath

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-22
    • 2021-02-11
    • 2019-11-11
    • 1970-01-01
    相关资源
    最近更新 更多