How do I print file for a specific parent/child using Get-ChildItem?

252 views Asked by At

I'm able to recursively search a directory using Get-ChildItem -Recurse. But not getting output as expected. Tried -Depth also. But no luck.

Folder structure is like below:

C:\Users\Documents\Azure\repo\Templates\directory*

directory1-uat --> childdir1 --> childdir2 --> a.parameters.json

directory2-dev --> childdir1 --> childdir2 --> b.parameters.json

directory3-nonprod --> childdir1 --> childdir2 --> c.parameters.json

directory4-test --> childdir1 --> childdir2 --> d.parameters.json

directory5-uat --> childdir1 --> childdir2 --> e.parameters.json

I've tried the following:


    $repo = 'C:\Users\Documents\Azure'
    $subDirs = Get-ChildItem -Path "$repo\Templates\*\" -Depth 0 -Directory | Where-Object fullname -notlike "*_Templates*"
    $subDirs
    ForEach ($subDir in $subDirs) {
        $subDir
        $envTag = $subDir.Name.split('-')[1]
        $envTag
        $subParamFiles = Get-ChildItem -Path $subDir.PSParentPath -Recurse -File -Include "*parameters.json"
        $subParamFiles
    }

Output is:


    d-----         02-Jan-20  8:48 PM                directory1-uat
    uat
    -a----         02-Jan-20  8:48 PM                a.parameters.json
    -a----         02-Jan-20  8:48 PM                b.parameters.json
    -a----         02-Jan-20  8:48 PM                c.parameters.json
    -a----         02-Jan-20  8:48 PM                d.parameters.json
    -a----         02-Jan-20  8:48 PM                e.parameters.json
    d-----         02-Jan-20  8:48 PM                directory2-dev
    dev
    -a----         02-Jan-20  8:48 PM                a.parameters.json
    -a----         02-Jan-20  8:48 PM                b.parameters.json
    -a----         02-Jan-20  8:48 PM                c.parameters.json
    -a----         02-Jan-20  8:48 PM                d.parameters.json
    -a----         02-Jan-20  8:48 PM                e.parameters.json
    d-----         02-Jan-20  8:48 PM                directory3-nonprod
    nonprod
    -a----         02-Jan-20  8:48 PM                a.parameters.json
    -a----         02-Jan-20  8:48 PM                b.parameters.json
    -a----         02-Jan-20  8:48 PM                c.parameters.json
    -a----         02-Jan-20  8:48 PM                d.parameters.json
    -a----         02-Jan-20  8:48 PM                e.parameters.json
    d-----         02-Jan-20  8:48 PM                directory4-test
    test
    -a----         02-Jan-20  8:48 PM                a.parameters.json
    -a----         02-Jan-20  8:48 PM                b.parameters.json
    -a----         02-Jan-20  8:48 PM                c.parameters.json
    -a----         02-Jan-20  8:48 PM                d.parameters.json
    -a----         02-Jan-20  8:48 PM                e.parameters.json
    d-----         02-Jan-20  8:48 PM                directory5-uat
    uat
    -a----         02-Jan-20  8:48 PM                a.parameters.json
    -a----         02-Jan-20  8:48 PM                b.parameters.json
    -a----         02-Jan-20  8:48 PM                c.parameters.json
    -a----         02-Jan-20  8:48 PM                d.parameters.json
    -a----         02-Jan-20  8:48 PM                e.parameters.json

Expected Output:


    d-----         02-Jan-20  8:48 PM                directory1-uat
    uat
    -a----         02-Jan-20  8:48 PM                a.parameters.json
    d-----         02-Jan-20  8:48 PM                directory2-dev
    dev
    -a----         02-Jan-20  8:48 PM                b.parameters.json
    d-----         02-Jan-20  8:48 PM                directory3-nonprod
    nonprod
    -a----         02-Jan-20  8:48 PM                c.parameters.json
    d-----         02-Jan-20  8:48 PM                directory4-test
    test
    -a----         02-Jan-20  8:48 PM                d.parameters.json
    d-----         02-Jan-20  8:48 PM                directory5-uat
    uat
    -a----         02-Jan-20  8:48 PM                e.parameters.json

2

There are 2 answers

0
Wasif On

You should give the current path instead of parent path:

$subDirs = dir "C:\Users\Documents\Azure\Templates\*\" -Directory | ? {$_.fullname -notlike "*_Templates*"}
    $subDirs
    $subDirs | % {
        $_
        $_.Name.split('-')[1]
        dir $_ -Recurse -File -Filter "*parameters.json"
    }
0
Lee_Dailey On

you are printing the $subDir - in the 1st case that is directory1-uat. then you are getting the file list of the parent directory of $subDir. you want the files in $subDir, not in the entire parent tree. [grin]

try replacing $subDir.PSParentPath with just $subDir. this line ...

$subParamFiles = Get-ChildItem -Path $subDir.PSParentPath -Recurse -File -Include "*parameters.json"

... would become this ...

$subParamFiles = Get-ChildItem -Path $subDir -Recurse -File -Include "*parameters.json"