powershell-4.0 - 路径中的Powershell Get-ChildItem通配符?
问题描述
可以将通配符放在文件夹名称掩码中吗?或者是否需要运行Get-ChildItem
两次,一次用于目录,另一次用于路径?即使使用下面的代码,我也没有返回任何目录。
我认为这篇文章展示了如何使用旧语法来做到这一点:
$folderName = "c:\BizTalk\Vendors\*\AS2FilesReceived\"
$folderMask = "$folderName\*.*"
$dirs = Get-ChildItem -Path $folderName -Recurse -Directory
Write-Host "Number of Matching Directories = $($dirs.Count)"
$dirs
#$files = $dirs | %{ Get-ChildItem -Path $folderMask -Filter "*.*" -Exclude "*997*.*" -File} | Where-Object {$_.CreationTime -gt (Get-Date).AddDays(-6)} | Sort-Object LastWriteTime -Descending
解决方案
Get-ChildItem
支持路径中的通配符,但通配符仅适用于单个文件夹级别,即模式C:\foo\*\bar
将找到文件夹C:\foo\abc\bar
和C:\foo\xyz\bar
,但不会找到文件夹C:\foo\abc\xyz\bar
。对于后者,您需要这样的东西:
Get-ChildItem 'C:\foo' -Filter 'bar' -Directory -Recurse
如果您只有一个变量文件夹级别并且只想要给定路径中的文件(没有递归),您可以执行以下操作:
Get-ChildItem 'C:\foo\*\bar\*' -File
如果您还想要以下子文件夹中的所有文件,请bar
使用:
Get-ChildItem 'C:\foo\*\bar' -File -Recurse
推荐阅读
- python - Tortoise ORM for Python 没有实体的返回关系(Pyndantic,FastAPI)
- excel - VBA Excel,如何在特定列中提取最大值并在特定工作表中显示最大值
- database - Vb.Net 应用程序安装程序不更新访问数据库
- c - 在 C 代码中使用 asm 并在没有 -no-pie 选项的情况下进行编译
- r - GGplot不为折线图生成线性y轴
- spring-boot - Springboot+Java中的异步调用
- swiftui - SwiftUI 中结构继承的方法
- node.js - ReactNative - 在 socket.io 中发送图像
- php - 在 tcpdf 中添加动态内容
- androidx - 由于不兼容的 aar 问题,无法导入任何 androidx 库