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
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"
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"
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.