3

Extension of the post Powershell - using wildcards to search for filename

Hi There!

Let's assume we have this files and folders configuration:

C:\temp\FolderLevel01\FolderLevel02\FolderLevel03\level03.txt
C:\temp\FolderLevel01\FolderLevel02\FolderLevel03\level03.yml
C:\temp\FolderLevel01\FolderLevel02\FolderLevel03\FolderLevel04\level04.txt
C:\temp\FolderLevel01\FolderLevel02\FolderLevel03\FolderLevel04\level04.yml
C:\temp\FolderLevel01\FolderLevel02\FolderLevel03\FolderLevel04\Contract\contract.txt
C:\temp\FolderLevel01\FolderLevel02\FolderLevel03\FolderLevel04\Contract\contract.yml

We can retrieve the contract.yml file if we have the correct folder tree structure in the "Path" argument:

Get-ChildItem "C:\temp\FolderLevel01\FolderLevel02\FolderLevel03\FolderLevel04\Contract\" -Include "*.yml" -Recurse
Get-ChildItem "C:\temp\FolderLevel01\*\*\*\Contract\" -Include "*.yml" -Recurse
Get-ChildItem "C:\temp\FolderLevel01\*\*\*\Contract\*.yml"

But how to retrieve the "Contract\*.yml" if

  • you don't know in advance the folder tree
  • the file name is unkwnown (except yml extension) The only info you have is
  • the file is in a specific folder (i.e. Contract)
  • the file as a secific extensioin (i.e. *.yml)

A command such as the following does not work:

Get-ChildItem "C:\temp\FolderLevel01\" -Include "Contract\*.yml" -Recurse

Regards

Only 4Info
  • 31
  • 2

1 Answers1

1

In two steps:

$folder=(Get-ChildItem .\FolderLevel01\\Contract -Recurse)
$file=(Get-ChildItem ($folder.FullName + '\*.yml'))
Paolo
  • 21,270
  • 6
  • 38
  • 69