# Windows powershell file process duplication

**URL:** <https://forums.powershell.org/t/windows-powershell-file-process-duplication/7036>\
**Category:** PowerShell Help\
**Created:** [August 21, 2016, 10:57pm UTC](https://forums.powershell.org/t/windows-powershell-file-process-duplication/7036 "2016-08-21T22:57:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![venkatesh-sql](https://avatars.discourse-cdn.com/v4/letter/v/a9adbd/32.png) [@venkatesh-sql](https://forums.powershell.org/u/venkatesh-sql)\
**Post date:** [August 21, 2016, 10:57pm UTC](https://forums.powershell.org/t/windows-powershell-file-process-duplication/7036/1 "2016-08-21T22:57:28Z")

</div>

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]$_.Directory.Name  
$processedfiles += [string][System.IO.Path]::GetFileName($\_.FullName) + " | ";  
}

Thank you.

---

<div class="post-metadata">

**Author:** ![theonlyway](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/theonlyway/32/395_2.png) [@theonlyway](https://forums.powershell.org/u/theonlyway)\
**Post date:** [August 22, 2016, 2:16am UTC](https://forums.powershell.org/t/windows-powershell-file-process-duplication/7036/2 "2016-08-22T02:16:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![venkatesh-sql](https://avatars.discourse-cdn.com/v4/letter/v/a9adbd/32.png) [@venkatesh-sql](https://forums.powershell.org/u/venkatesh-sql)\
**Post date:** [August 22, 2016, 11:32pm UTC](https://forums.powershell.org/t/windows-powershell-file-process-duplication/7036/3 "2016-08-22T23:32:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:40pm UTC](https://forums.powershell.org/t/windows-powershell-file-process-duplication/7036/4 "2024-05-16T20:40:42Z")

</div>

Closed on May 16, 2024, 8:40pm UTC


