NealWalters
NealWalters

Reputation: 18187

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

Upvotes: 2

Views: 5587

Answers (3)

NealWalters
NealWalters

Reputation: 18187

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)"

Upvotes: 1

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200203

Get-ChildItem supports wildcards in the path, but the wildcard applies only to a single folder level, i.e. a pattern C:\foo\*\bar will find folders C:\foo\abc\bar and C:\foo\xyz\bar, but won't find a folder C:\foo\abc\xyz\bar. For the latter you need something like this:

Get-ChildItem 'C:\foo' -Filter 'bar' -Directory -Recurse

If you have just a single variable folder level and want only files from the given path (without recursion) you can do something like this:

Get-ChildItem 'C:\foo\*\bar\*' -File

If you also want all files in subfolders below bar use this:

Get-ChildItem 'C:\foo\*\bar' -File -Recurse

Upvotes: 4

Meeple
Meeple

Reputation: 181

Assuming that you want to find and return info about all files in all AS2FilesReceived directories for each folder under Vendors, then this might help you...

#Find all AS2FilesReceived directories
$dirs= Get-ChildItem -Path 'C:\BizTalk\Vendors\*\AS2FilesReceived\' -Directory

#Return number of directories
"$($dirs.Count) directories found"

#Return list of directory names
$dirs | % {$_.Parent.Name}

#Return the full name of every text file under each directory
$dirs | % {Get-ChildItem -Path $_ *.txt} | % {$_.FullName}

Which returns...

3 directories found
Vendor1
Vendor2
Vendor3
C:\BizTalk\Vendors\Vendor1\AS2FilesReceived\Vendor1AS2File.txt
C:\BizTalk\Vendors\Vendor2\AS2FilesReceived\Vendor2AS2File.txt
C:\BizTalk\Vendors\Vendor3\AS2FilesReceived\Vendor3AS2File.txt

Upvotes: 1

Related Questions