Author Posts

September 8, 2016 at 5:22 am

Hi,
We have a requirement where files in a particular directory get processed the corresponding business users need to be intimated via email. All the Directories and Sub-directories have email associated and all this information is saved in a comma separated file.

$From = "myemail@domain.com"
$To = "businessuser@domain.com"
$Cc = "stakeholder@domain.com"
$Subject = "File process intimation"
$Body = "File processed"
$SMTPServer = "smtp.domain.com"
$SMTPPort = "1739"

$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 }

}

This is the ps script to send email. This works okay but when I have a file to process in "C:\Temp\Accounts\GL" and no included file in "C:\Temp\Accounts" it still sends out 2 different email to accounts@domain.com & accountsgl@domain.com.

Data in comma separated file:

Dir Email
Accounts accounts@domain.com
GL accountsgl@domain.com
AR accountsar@domain.com
AP accountsap@domain.com
Sales sales@domain.com
SalesOrder salesorder@domain.com

Thank you.

September 8, 2016 at 8:56 am

Does this work for you?

	
	$From = "myemail@domain.com"
	$To = "businessuser@domain.com"
	$Cc = "stakeholder@domain.com"
	$Subject = "File process intimation"
	$Body = "File processed"
	$SMTPServer = "smtp.domain.com"
	$SMTPPort = "1739"

	$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)\*.*" -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 $emailTo -Cc $Cc -Subject $Subject -Body "$Body $processedfiles" -SmtpServer $SMTPServer -port $SMTPPort 
		}
	}
  • This reply was modified 1 year, 11 months ago by  84rusty ..
  • This reply was modified 1 year, 11 months ago by  84rusty ..

September 8, 2016 at 2:24 pm

Hi, For some reason file process duplication happens. 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"

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) + " | ";
}

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.

Thank you.

September 8, 2016 at 8:29 pm

Hi venkatesh,

Check out the code I supplied in my previous post, it is your code just with the fix applied.

Specifically you mention the gci section as a problem, so remove the -Recurse, and add *.* onto the -path so it picks up the files, like so:
Get-ChildItem -Path "$($dir.FullName)\*.*" -include *.txt,*.xls,*.csv,*.xlsx

You don't need to recurse on this call as you recursed earlier ($orgFolder = Get-ChildItem -Path c:\temp -recurse -exclude *.*)

  • This reply was modified 1 year, 11 months ago by  84rusty ..