stackUser2000
stackUser2000

Reputation: 1695

Find the most recent file in a folder/dir and send that file in a email using shell script in windows cmd or powershell

I need to find the most recent file in a folder/dir and send that file as an attachment in a email, so far i have this code that find the most recent file in my windows SO, but i need to specify a route a find the most recent file there and then send that file in a email so far i have this:

EDIT 1:

So far i have this:

This part gives me the last file created in a dir/folder:

$dir = "D:\Users\myUser\Desktop\dirTest"
$latest = Get-ChildItem -Path $dir | Sort-Object LastAccessTime -Descending | Select-Object -First 1
$latest.Fullname

$attachment = $latest.Fullname

And this send the email (i'm using yahoo accounts):

$emailSmtpServer = "smtp.mail.yahoo.com"
$emailSmtpServerPort = "587"
$emailSmtpUser = "[email protected]"
$emailSmtpPass = "passForThisquestion"

$emailMessage = New-Object System.Net.Mail.MailMessage
$emailMessage.From = "[email protected]"
$emailMessage.To.Add( "[email protected]" )
$emailMessage.Subject = "Testing e-mail"
$emailMessage.Body = "email from power shell"

$emailMessage.Attachments.Add( $attachment )  <---- this part gives me problems

$SMTPClient = New-Object Net.Mail.SmtpClient($emailSmtpServer, $emailSmtpServerPort)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential($emailSmtpUser, $emailSmtpPass);
$SMTPClient.Send($emailMessage)

Upvotes: 0

Views: 5425

Answers (3)

Ramnath Susendran
Ramnath Susendran

Reputation: 1

Please check this powershell command.

  $Body = “get the information here to show the data with attachement”  
    $dir = "Path\of\the\folder(C:\..\..)"
    $latest = Get-ChildItem -Path $dir | Sort-Object LastAccessTime -Descending | Select-Object -First 1
    $latest.Fullname
    $file = $latest.Fullname
    $EmailFrom = “[email protected]”
    $EmailTo = “[email protected]”
    $SMTPServer = “smtp.gmail.com”
    $EmailSubject = “Enter Your Subject”
    $att = new-object Net.Mail.Attachment($file)
    $mailmessage = New-Object system.net.mail.mailmessage
    $mailmessage.from = ($EmailFrom)
    $mailmessage.To.add($EmailTo)
    $mailmessage.Subject = $EmailSubject
    $mailmessage.Body = $Body
    $mailmessage.IsBodyHTML = $true
    $mailmessage.Attachments.Add($att)
    $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
    $SMTPClient.EnableSsl = $true
    $SMTPClient.Credentials = New-Object System.Net.NetworkCredential(“sender_username”, “sender_password”);
    $SMTPClient.Send($mailmessage)
    $att.Dispose()

Upvotes: 0

stackUser2000
stackUser2000

Reputation: 1695

It works now, this is my final script.

This script search the most recent file created in a Dir and it sends that file created to a email account.

Here is my script it works for me but it takes a few minutes to send the email, thanks for the help

This script do what i wanted, it find the most recent file and send tha file in a email.

$dir = "d:\Users\myUser\Desktop\testDir"
$latest = Get-ChildItem -Path $dir | Sort-Object LastAccessTime -Descending | Select-Object -First 1
$latest.Fullname

$attachment = $latest.Fullname


$emailSmtpServer = "smtp.mail.yahoo.com"
$emailSmtpServerPort = "587"
$emailSmtpUser = "[email protected]"
$emailSmtpPass = "MyPassword"

$emailMessage = New-Object System.Net.Mail.MailMessage
$emailMessage.From = "[email protected]"
$emailMessage.To.Add( "[email protected]" )
$emailMessage.Subject = "My Subject"
$emailMessage.Body = "My body message"

$emailMessage.Attachments.Add($attachment)

$SMTPClient = New-Object Net.Mail.SmtpClient($emailSmtpServer, $emailSmtpServerPort)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential($emailSmtpUser, $emailSmtpPass);
$SMTPClient.Send($emailMessage)

Upvotes: 1

James Ruskin
James Ruskin

Reputation: 990

With PowerShell, something like this should do alright:

function Send-RecentFile {
    param(
        [ValidateScript({Test-Path $_ })]
        [String] $Path
        )

    $file = Get-ChildItem -Path $Path -File | Sort CreationTime | Select -Last 1
    Write-Output "The most recently created file is $($file.Name)"


    $messageParameters = @{
        From        = "[email protected]"
        To          = "[email protected]"
        Subject     = "title"
        Body        = "message body"
        SMTPServer  = "mail.mydomain.com"
        Attachments = $file.FullName
    }

    Send-MailMessage @messageParameters -Credential (Get-Credential "[email protected]")
}

If you do want to store the credentials in the file, you might have to do something slightly different with it.

Upvotes: 0

Related Questions