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!
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 need the path of the directory, just use:
$MyVariable.FullName
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"
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
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 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With