Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex with dir command directly into the powershell

I'm trying to list all the files with .rar or .zip file type.

I tried regular expressions like the ones you can find in the online regex helpers, but none of those seems to work.

Maybe the problem is trying to do this directly into the cmd/powershell instead of a script ?

dir *./(zip|rar)/

The expected result would be something like: file1.rar file2.rar file3.zip

like image 998
dmz Avatar asked Aug 14 '19 22:08

dmz


2 Answers

In Powershell, dir is an alias for Get-ChildItem. You cannot use a regex directly. Powershell is interpretting your regex as a wildcard.

As noted in the other answers and in the Get-ChildItem documentation, you can to use a comma to separate your search terms. dir *.zip,*.rar, which is an alias for

Get-ChildItem -path *.zip,*.rar

If you'd like to use a regex, you need to assign the result of Get-ChildItem to a variable and then use -match.

> $dir = Get-ChildItem
> $dir -match "\.(zip|rar)$"

Or use parentheses to turn it into a one-liner:

(Get-ChildItem) -match "\.(zip|rar)$"
like image 63
Codebling Avatar answered Nov 15 '22 05:11

Codebling


get-childitem -path takes an array. Not regex, but an array of wildcards that serve the same purpose.

dir *.zip,*.rar
like image 22
js2010 Avatar answered Nov 15 '22 03:11

js2010