Locating all subdirectories matching a string or partial string

Viewed 48720

I basically need to set a variable to a folder path without knowing the full path.

My issue is I need to find a directory called "DirA" for example. But this directory could either be located in "DirB\" or "DirB\DirC" and sometimes the directory names that they could be contained in are not the same.

I know the first portion of the directory path will be the same regardless so i thought of using a -recurse filter to the folder name but sometimes the directory i'm looking for isn't named quite like the one that is usually expected, sometimes an extra letter on the end.

Would it be possible to do something like...

$MyVariable = Get-ChildItem D:\Data\Dir1 -Recurse | Where-Object {$_.name -like "name"} -Directory |
% { $_.fullname }

Any help is greatly appreciated!

5 Answers

Hi find the below script it should work correct me in case if it's wrong,

$rootFolder = "D:\Data" ##Mention the root folder name here###

$folderItems = (Get-ChildItem $rootFolder) 

$subfolderslist = (Get-ChildItem $rootFolder -recurse | Where-Object {$_.PSIsContainer -eq $True -and $_.Name -like "*2018-08*"} | Sort-Object)

foreach ($curfolder in $subfolderslist)
{

    #Write-Host 'Folder Name '$curfolder  yyyy-mm-dd
    $subFolderItems = (Get-ChildItem $curfolder.FullName) 

    foreach($item in $subFolderItems)
    {

      $currentfile=$rootFolder+"\"+$curfolder+"\"+$item
      #Write-Host 'file path '$mfilepath
      Write-Host " "

     if ((Get-Item $currentfile ).length -gt 3mb)
      { 
        Write-Host "File size greater than 3 MB hence "$true
      }
      else 
       {
         Write-Host "File size less than 3 MB hence "$false
       }

    } 


}

You could do it in this way in Powershell 3.0+

$MyVariable = (dir -r -Filter "DirA*" -Path "D:\Data\Dir1").FullName

"dir -r" it's an alias of "Get-ChildItem -Directory -Recurse"

Related