Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing directory to a folder matching a regex in powershell

I want to CD into a folder that begins with the word "Patch", followed by several other digits. All I care about is that it begins with the word "Patch"

How can I change my directory using a regex in powershell?

This is what I have at the moment and it doesn't work. Am I on the right track though?

$FolderPath = "^Patch[0-9]+$"

cd "C:\Test\" + $FolderPath
like image 277
molly Avatar asked Nov 25 '25 03:11

molly


2 Answers

To put you in the right direction:

Use Get-ChildItem and only get the directories matching your regex, something like:

$matchingItem = Get-ChildItem "C:\Test" -Directory | ?{ $_.Name -match $FolderPath } | select -First 1

Now you can cd to the matching directory.

like image 66
manojlds Avatar answered Nov 28 '25 06:11

manojlds


You don't really need the regex. Wildcard blobbing will handle that:

cd c:\test\patch[0-9]*
like image 20
mjolinor Avatar answered Nov 28 '25 06:11

mjolinor