18

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!

Matt
  • 45,022
  • 8
  • 78
  • 119
Dewi Jones
  • 785
  • 1
  • 6
  • 18

5 Answers5

34

Try something like this:

$BaseDir = "C:\Dir1"
$NameToFind = "\DirA"

$MyVariable = Get-ChildItem $BaseDir -Recurse | Where-Object { $_.PSIsContainer -and $_.Name.EndsWith($NameToFind)}

This should go through all the directories in the tree, starting at C:\Dir1 and return the directory object DirA back to you.

If you only want results one level deep remove the -Recurse.

If you only need the path of the directory, just use:

$MyVariable.FullName
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
Mark Oxley
  • 380
  • 3
  • 7
  • You Sir are amazing! i changed it a little because it was more likely to find the start so "$_.Name.EndsWith" and removed the backslash from $NameToFind – Dewi Jones Mar 08 '16 at 15:00
10

You don't need to post process this with Where-Object. -Filter can already get this for you. If you have at least PowerShell 3.0 then you can remove Where-object in its entirety.

(Get-ChildItem -Path D:\Data\Dir1 -Filter "*DirA*" -Recurse -Directory).Fullname

That will return all directories under the Path that have the exact name of DirA. If you need partial matches then just use simple wildcards: -Filter "Dir*some"

If running PowerShell 2.0 then you can do the following.

Get-ChildItem -Path D:\Data\Dir1 -Filter "*DirA*" -Recurse | Where-Object {$_.PSIsContainer} | Select-Object -ExpandProperty Fullname
Matt
  • 45,022
  • 8
  • 78
  • 119
4

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
       }

    } 


}
Patrick
  • 1,717
  • 7
  • 21
  • 28
3

You can use Get-ChildItem and recurse like you are doing, throw in a where-object to grab directories only, and then also use a name filter. Using your example of trying to find DirA (but it may have letters after it) that will always be somewhere inside Dir1, this query should work:

$MyVariable = Get-Childitem D:\Data\Dir1 -recurse | Where-Object {$_.PSIsContainer -and $_.name -like "*DirA*" | Select-Object -expandproperty fullname
dfundako
  • 8,022
  • 3
  • 18
  • 34
2

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"

crisc2000
  • 1,082
  • 13
  • 19