Ahmed Kamal
Ahmed Kamal

Reputation: 3000

How to use curly braces path name in windows?

I used to use paths like this ls {path1,path2}/* in Linux but in Windows PowerShell, I got the following error

At line:1 char:10
+ ls {path1,path2}/*
+          ~
Missing argument in parameter list.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingArgument

I tried dir {path1,path2}/* instead of ls {path1,path2}/* but the same error thrown!!

Upvotes: 2

Views: 1701

Answers (2)

user6811411
user6811411

Reputation:

You should use the proper cmdlet/alias for the environment you are working in (consider on linux PowerShell Core ls refers to the system ls not the PoSh gci)

PowerShell allows wildcards *?(and ranges[0-2]) on multple levels, so this should do:

Get-ChildItem -Path some\verbose[12]\dir\* -Include *.ext1,*.ext2

In case the part of the path differs completely you'll have to supply an array:

Get-ChildItem -Path some\this\dir\*,some\that\dir\* -Include *.ext1,*.ext2

Upvotes: 1

AdminOfThings
AdminOfThings

Reputation: 25001

I think what you want is this:

ls path1,path2 -recurse

Path1 and Path2 will either need to be variables or quoted Strings.

Here is an example

PS C:\> ls "c:\temp\test1","c:\temp\test2" -recurse


    Directory: C:\temp\test1


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----        2/14/2019  10:29 AM                test1subdir


    Directory: C:\temp\test1\test1subdir


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        2/14/2019  10:29 AM             13 test1file.txt


    Directory: C:\temp\test2


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        2/14/2019  10:30 AM              6 test2file.log

I know this may be crazy, but you can create a function that will nearly replicate what you are trying to do:

function new-ls {

param([Parameter(Position=0)]
[string]$Path,
[Parameter(Position=1)]
[string[]]$Includes)


$StrMatch = ($Path | sls -pattern "^(.*?){(.*?),(.*?)}(.*?)$" -allmatches).matches.groups.value
$NewPaths = @(($StrMatch[1] + $StrMatch[2] + $StrMatch[4]), ($StrMatch[1] + $StrMatch[3] + $StrMatch[4]))
ls $NewPaths -include $Includes -recurse

}

# Running the code

PS C:\temp> new-ls "c:\temp\{test1,test2}\*"


    Directory: C:\temp\test1\test1subdir


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        2/14/2019  10:29 AM             13 test1file.txt


    Directory: C:\temp\test2


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        2/14/2019  10:30 AM              6 test2file.log


PS C:\temp> new-ls "c:\temp\{test1,test2}\*" "*.txt"


    Directory: C:\temp\test1\test1subdir


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        2/14/2019  10:29 AM             13 test1file.txt

PS C:\temp> new-ls "c:\temp\{test1,test2}\*" "*.txt","*.log"


    Directory: C:\temp\test1\test1subdir


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        2/14/2019  10:29 AM             13 test1file.txt


    Directory: C:\temp\test2


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        2/14/2019  10:30 AM              6 test2file.log

Upvotes: 1

Related Questions