powershell - * .tmp是否存在于目录中,如何不对目录进行操作?

标签 powershell powershell-4.0

该代码应通过当前目录中的所有顶级目录,并执行临时文件检查,如果不存在,它将移动目录。

$processDirectories = {
    foreach ($childDirectory in Get-ChildItem -Force -Directory) {
        test-path "$childDirectory\*.tmp"
        move-item -LiteralPath "$childDirectory" -Destination "d:\"
    }
}

我不知道如何停止为找到*.tmp的目录运行代码。另外,此方法仅检查每个子目录的根是否为*.tmp,而不是其中的整个树。

最佳答案

如果我了解您的限制,这里有两个变体,
计算子文件夹中*.tmp的数量。如果为零,则移动文件夹。

  • 迭代第一级文件夹
    foreach ($childDirectory in Get-ChildItem -Force -Directory) {
      if ((Get-ChildItem $childDirectory -recurse -Include *.tmp, *.!qb).Count -eq 0){
        Move-Item -LiteralPath "$childDirectory" -Destination "d:\" -WhatIf
      }
    }
    
  • 单个管道
    Get-ChildItem -Force -Directory | Where-Object {
      (Get-ChildItem $_ -recurse -Include *.tmp, *.!qb).Count -eq 0} |
        Move-Item -Destination "d:\" -WhatIf
    

  • 如果输出看起来正常,请删除结尾的-WhatIf

    关于powershell - * .tmp是否存在于目录中,如何不对目录进行操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56092908/

    相关文章:

    powershell - 所需的状态配置 HTTPS 不起作用

    powershell - 在PowerShell中组合变量时如何删除空间

    windows - 我可以让这个脚本更快吗?

    powershell - 如何判断XML元素何时在Powershell中是多个元素之一

    json - 从 powershell 中的 JSON 对象中删除一个属性

    powershell - Powershell根据名称移动目录

    powershell - 如何在SQL存储过程中调用的Powershell中传递参数

    powershell - 在文本文件的两列之一中查找重复值

    windows - 使用 Powershell 阻止文件

    Powershell 在模块内加载模块 [范围]