Search code examples
powershell-4.0get-childitem

Powershell Get-ChildItem wildcard in path?


Can a wildcard be put in a folder name mask? Or is it necessary to run Get-ChildItem twice, one for the direcdtories and a second for the path? Even with the code below, I'm getting no directories returned.

I think this post shows how to do it with the older syntax:

$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

Solution

  • Ultimately, I just wanted the files, not sure if I had a typo or what, but now this works. It's possible I swithced from $folderMask to $folderName, or didn't have the *.* on the end of the $folderMask.

    $folderMask = "c:\BizTalk\Vendors\*\AS2FilesReceived\*.*"
    $files = Get-ChildItem -Path $folderMask -File  
    Write-Host "Number of Matching Files = $($files.Count)"