Script - Sending Emails with a wildcard for an attachment


I have basic email script and I have only included the section about attaching an attachment

With objMessage
    .To       = cToEmail
    .From     = cFromEmail
    .Subject  = cSubject
    .TextBody = "**** REMINDER - Due ****" 
    .AddAttachment "C:\Inbox\WYIN*.pdf"	
End With

Open in new window

I understand using the .AddAttachment - you can only add a specific document using the exact file path.

I want to be able to use a wildcard in the attachment section - for example C:\Inbox\WYIN*.pdf"

Any ideas
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Though your original script is VB Script, you have PowerShell as one of the topics, so here's how easy it is in PS.
Come on over, PS is so much better than VB Script, it's not even funny anymore ...
$sendMailParams = @{
	To = ''
	From = ''
	Subject = 'Reminder'
	Body = '**** REMINDER - Due ****'
	SmtpServer = ''
	Attachments = (Get-ChildItem -Path 'C:\Inbox\WYIN*.pdf') | Select-Object -ExpandProperty FullName
Send-MailMessage @sendMailParams

Open in new window

Bill PrewIT / Software Engineering ConsultantCommented:
For a VBS approach, this should get you close.  Didn't test here, might need a tweak...

Set objFSO = CreateObject("Scripting.FileSystemObject")

With objMessage
    .To       = cToEmail
    .From     = cFromEmail
    .Subject  = cSubject
    .TextBody = "**** REMINDER - Due ****" 
    ' .AddAttachment "C:\Inbox\WYIN*.pdf"	
    For Each objFile In objFSO.GetFolder("C:\Inbox").Files
        If UCase(Left(objFSO.GetBaseName(objFile.Path), 4)) = "WYIN" And UCase(objFSO.GetExtensionName(objFile.Path)) = "PDF" Then
            .AddAttachment objFile.Path
        End If
End With

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.