kennysarmy Posted March 28, 2018 Posted March 28, 2018 We use the Mdaemon email platform. I have a folder of emails, all replies to a question I sent out. I would like to create a text file of all the users who have replied. This is the format of a typical file: X-MDAV-Result: clean X-MDAV-Processed: school.domain.com, Tue, 27 Mar 2018 10:37:36 +0100 Return-path: Authentication-Results: school.domain.com; auth=pass (login) [email protected] Received: from WorldClient by school.domain.com (MDaemon PRO v17.5.2) with ESMTPA id md50007290673.msg; Tue, 27 Mar 2018 10:37:36 +0100 X-Spam-Processed: school.domain.com, Tue, 27 Mar 2018 10:37:36 +0100 (not processed: message from trusted or authenticated source) X-MDArrival-Date: Tue, 27 Mar 2018 10:37:36 +0100 X-Authenticated-Sender: [email protected] X-Rcpt-To: [email protected] X-MDRcpt-To: [email protected] X-Return-Path: [email protected] X-Envelope-From: [email protected] X-MDaemon-Deliver-To: [email protected] X-CAV-Result: clean Received: from [iP ADDRESS] by school.domain.com via WorldClient with HTTP; Tue, 27 Mar 2018 10:37:32 +0100 Date: Tue, 27 Mar 2018 10:37:32 +0100 From: "FistName Surname" To: "IT Manager" Subject: Re: [staff] Quick Question ! {01} MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Message-ID: X-Mailer: WorldClient 17.5.2 In-Reply-To: References: Yes please. Thanks I'd like to extract the line: From: "FistName Surname" from all the other email/files and dump in to one text file. Anyone have any scripting experience to help with this?
stevec_ Posted March 28, 2018 Posted March 28, 2018 Should do the trick, just change the $FolderPath and $OutputFile variables to the appropriate $FolderPath = "C:\Emails" $OutputFile = "C:\EmailsFrom.txt" $Emails = Get-ChildItem -Force $FolderPath -Recurse ForEach ($Email in $Emails) { $regex = [regex] "^From: (.*)$" Get-Content $Email.FullName | ForEach-Object { If ($_ -match $regex) { $_ | Out-File -FilePath $OutputFile -Append } } }
jmcdermott Posted March 28, 2018 Posted March 28, 2018 $filesFolder = "files" $outputFile = "output.txt" $files = Get-ChildItem $filesFolder for ($i=0; $i -lt $files.Count; $i++) { $currentFile = $filesFolder + "" + $files[$i] foreach ($text in get-content $currentFile){ if ($text.Length -gt 6){ if ($text.Substring(0,6) -eq "From: "){ Add-Content $outputFile $text } } } } 1
jmcdermott Posted March 28, 2018 Posted March 28, 2018 Sorry didn't refresh the page and see previous reply. 1
kennysarmy Posted March 28, 2018 Author Posted March 28, 2018 Should do the trick, just change the $FolderPath and $OutputFile variables to the appropriate $FolderPath = "C:\Emails" $OutputFile = "C:\EmailsFrom.txt" $Emails = Get-ChildItem -Force $FolderPath -Recurse ForEach ($Email in $Emails) { $regex = [regex] "^From: (.*)$" Get-Content $Email.FullName | ForEach-Object { If ($_ -match $regex) { $_ | Out-File -FilePath $OutputFile -Append } } } Works perfectly - many thanks...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now