Link to home
Start Free TrialLog in
Avatar of Bambi
Bambi

asked on

VB script for Emailing attachment using a wild card:

Hello Friends, I'm trying to create a VBS script to attached all the files in specific direcotry label with todays date and emailed them.
however the file names are changing every day  (X_file1_20170111.txt,A_file2_20170111.txt, A_file3_20170111.txt, etc.)so I only want to attache only the lates files"label with todays dates"

Im sorry im but I dont know much about vbs or any other programing laguage, Currently Im trying to use a wild card but it only attached one file to the imail regarless the # of files in the directory. to me this was the most easy way but I would preffer to attache all files label with todays date.Any help would be apreciated.

================= See my code below  ===========================

'Initialization 
Option Explicit
Dim objEmail
Dim mbAnswer
Dim attFName

'=====================
' MainSettings
'=====================

' Create the message object using CDO
Set objEmail = CreateObject("CDO.Message")

' Assign message properties (To, From, Subject, etc.)
objEmail.From = "Psal@test.com"
objEmail.To = "Psal@test.com"
objEmail.Subject = "test script"
' Assign Email server Properties
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.mycompany.com"	'Smtp server
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 2525				'smtp port
objEmail.Configuration.Fields.Update

' Add file attachments if desired
Dim objFSO, strFolder, objFile, strFileName
Set objFSO = CreateObject("Scripting.FileSystemObject")
strFolder = "C:\test"
strFileName = ""
For Each objFile In objFSO.GetFolder(strFolder).Files
	If Right(LCase(objFile.Name), 4) = ".txt" Then strFileName = objFile.Path
Next
If strFileName <> "" Then objEmail.AddAttachment strFileName

objEmail.Send

Open in new window

(Edit: Added CODE tags)
ASKER CERTIFIED SOLUTION
Avatar of Rgonzo1971
Rgonzo1971

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Rgonzo1971
Rgonzo1971

Solution