powershell - 如果 *.tmp 存在于其中的任何位置,如何不对目录进行操作?
问题描述
此代码应遍历当前目录中的所有顶级目录,执行临时文件检查,如果不存在则移动目录。
$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
推荐阅读
- javascript - 在页面加载后通过小书签检索 XHR/GET 历史记录
- postgresql - 使用 pg 客户端连接到 postgres 的问题
- python - Alpine Linux 中缺少软件包,即使它已在软件包 repo 网站上列出
- node.js - 尝试使用 passport-jwt 到达受保护的路线时获得 401 状态
- css - 列填充:平衡在 Chrome 中不起作用?
- c# - 我可以在后台运行多个慢速进程,以便可以并行运行多个任务吗?
- amazon-cloudformation - Sophos UTM VPN 无法访问
- mongodb - 在 Scala Mongo DB 中使用嵌套案例类更新时出错
- systemd - 连接的systemd套接字服务?
- python - “validation_data 将覆盖validation_split”是什么意思。在 keras model.fit 文档中