I have a folder to which I have the path to, lets say C:/Users/Me/Project
Inside the Project folder there are 2 or more folders and one of them has a certain word like data1-latest. Only one folder inside Project has the word "latest" in it. The words before "latest" are unknown/random. How can I get the full path to data1-latest while only knowing it contains the word "latest" in it's name? I know I can use "cd C:/Users/Me/Project/*/specific folder that exists only here" but I would like to store the path to this folder inside a variable.
CodePudding user response:
Try this:
$fullPath = (Get-ChildItem -LiteralPath C:/Users/Me/Project -Filter *latest -Directory).FullName
- Using
-LiteralPath
so PowerShell doesn't try to interpret it as a wildcard path. When using-Path
instead, PowerShell applies its own extended wildcard syntax like[a-z]
, which sometimes comes as a surprise when a path contains literal[
and]
characters. -Directory
makes sure we only get directories-Filter
filters the directories using the wildcard syntax supported by the filesystem provider.FullName
gets a property from theFileSystemInfo
class, which is the common base class for bothDirectoryInfo
andFileInfo
, outputted byGet-ChildItem
.- To make sure you have found exactly one directory matching
*latest
, you may check theCount
property:if( $fullPath.Count -ne 1 ) { Write-Error 'Could not find exactly ONE directory matching *latest.' }