I have a script which checks if there are new files in several folders and then it sends an e-mail with log file if there are new files.
$source = Get-Content "C:AAAsource.txt"
$logDate = get-date -format yyyy-MM-dd
$logtime = get-date -format HH:mm
$log = "C:AAA$logdate.log"
$mailkomu = "[email protected]"
$mailod = "[email protected]"
$subject = "New files in DDD"
$zprava = "Message"
ForEach($path in $source){
Test-path $path
Write-Output "$logtime - $path - ok" | Out-File $log -Append
If ((Get-ChildItem -Path $path -Force | Measure-Object).Count -eq 0) {
Write-Output "no files" | Out-File $log -Append
}
Else {
Write-Output "new files" | Out-File $log -Append
$data = Get-ChildItem -Path $path -Recurse | Select-Object DirectoryName, Name
$data | Out-File $log -Append
Send-MailMessage -From $mailod -To $mailkomu -Subject $subject -Body $zprava -SmtpServer $SMTPServer -Attachments $log
}
}
There are 4 different paths in source .txt but there can be more paths. The count does not matter.
Checking for new files works perfectly.
The problem is, if there is a new file on e.g. the second path, it sends email immediately and does not wait for the other paths to be checked.
Example:
first path - no file - sends no email
second path - new file - sends email with log only for the first and the second path
third path - new file - sends email with log only for the first, second and the third path
fourth path - no file - sends no email
Is there a way how to make it works that it will check all paths, and if there are new files, it will send only one e-mail with log after checking all paths?