首页 > 解决方案 > 路径中的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

标签: powershell-4.0get-childitem

解决方案


Get-ChildItem支持路径中的通配符,但通配符仅适用于单个文件夹级别,即模式C:\foo\*\bar将找到文件夹C:\foo\abc\barC:\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

推荐阅读