Home > Back-end >  Powershell - Get-ChildItems -recurse not working
Powershell - Get-ChildItems -recurse not working

Time:09-30

i am new to powershell and struggling a bit.

$SOURCE="\\server\folder\sub1\sub2\sub3\sub4"
$TYPE='*.csv'
$SUB="*subx*
Get-ChildItem -recurse -path $SOURCE -include $TYPE -filter $SUB

In my head this is to search a network path folder for folders below "sub4" that are all named "subx" if that makes sense.

Then want to return a list of files that are XX*.csv (csv's that start with XX) that are present in the subfolders that exist in sub folders below these "subx" folders.

so the paths may end up being

"\\server\folder\sub1\sub2\sub3\sub4\xxxxx\yyyyy\subx\zzzzzzz\XX*.csv"
"\\server\folder\sub1\sub2\sub3\sub4\aaaaa\bbbbb\subx\ccccccc\XX*.csv"

But it doesnt work, it only returns 1 file with subx in the name ending in .csv

I can run this and it returns the list of folders/files called "subx" but it doesnt recurse through the subfolders below subx

$SOURCE="\\server\folder\sub1\sub2\sub3\sub4"
$SUB="*subx*
Get-ChildItem -recurse -path $SOURCE -filter $SUB

I can run this and it returns all files called XX*.csv in the sub folders below the $SOURCE path

$SOURCE="\\server\folder\sub1\sub2\sub3\sub4"
$TYPE='*.csv'
Get-ChildItem -recurse -path $SOURCE -include $TYPE

any tips would be appreciated!!!

CodePudding user response:

Split the operation into 2 steps:

  • Discover the subx folders under sub4
  • Discover the *.csv files under each of those

Note: In the following example I'm deliberate avoiding -Include because it's terribly slow in combination with -Recurse.

Get-ChildItem -Path $SOURCE -Directory -Recurse -Filter subx |Get-ChildItem -File -Recurse -Filter *.csv
  • Related