Author Posts

August 22, 2016 at 4:27 am

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.

August 22, 2016 at 7:46 am

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.

  • This reply was modified 1 year, 12 months ago by  Anthony. Reason: Added note about PS version

August 23, 2016 at 5:02 am

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.