Windows powershell file process duplication

My folder structure is “C:\Temp\SalesOrder\20160809\cc.txt” when its looking for all the files that need to be processed as soon as the recursion gets to folder “C:\Temp\SalesOrder” & “C:\Temp\SalesOrder\20160809” its processing cc.txt file. How do I rewrite the gci to not to traverse subfolders and only process files “C:\Temp\SalesOrder\20160809”

$flatFilePath = "C:\Temp\Email.csv"

$flatFile = Import-Csv -Path $flatFilePath -Header Dir,Email
$emailID = @{}
foreach ($record in $flatFile) { $emailID[$record.Dir]=$record.Email }

organization folder

$orgFolder = Get-ChildItem -Path “C:\Temp” -Recurse -Exclude .

Send email

foreach ($dir in $orgFolder) {
$processedfiles = $null
$emailTo = $null

Get-ChildItem -Path $dir.FullName -Recurse -Include *.txt,*.xls,*.csv,*.xlsx |  ? { $_.FullName } |  
    Foreach-Object { 
       [string]$internalOrg = [System.IO.DirectoryInfo]$_.Directory.Name
       $processedfiles += [string][System.IO.Path]::GetFileName($_.FullName) + " | ";  

$emailTo = $emailID.Get_Item($dir.Name) -split ";"   

if ($processedfiles) { Send-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject -Body "$Body $processedfiles" -SmtpServer $SMTPServer -port $SMTPPort } 


Problem area:

Get-ChildItem -Path $dir.FullName -Recurse -Include .txt,.xls,.csv,.xlsx | ? { $.FullName } |
Foreach-Object {
[string]$internalOrg = [System.IO.DirectoryInfo]$
$processedfiles += [string][System.IO.Path]::GetFileName($_.FullName) + " | ";

Thank you.

So basically you just want it to process files and not folders? Or do you mean you only want it to go down a certain depth?

Get-childitem -File will only return files
Get-ChildItem -Recurse -Depth 2 will recurse but will only go a maximum of 2 folders deep

Note: You need PS 3.0+ for -file I believe.

Hi Anthony, I want to process files only when the file that meets the criteria exists. For some reason even though I have just 1 file to process in all the folders put together it does process more than once and cannot figure out why? I have no hidden files either.