Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Attach all files in directory to email?

20 views
Skip to first unread message

PAkerly

unread,
Dec 28, 2009, 4:39:09 PM12/28/09
to
Is it possible to use VBScript to attach all files with a particular
extension to an email?

How could this be done?

Pegasus [MVP]

unread,
Dec 28, 2009, 7:07:41 PM12/28/09
to
"PAkerly" <pak...@gmail.com> said this in news item
news:60dc06b6-6c7c-488a...@r14g2000vbc.googlegroups.com...

> Is it possible to use VBScript to attach all files with a particular
> extension to an email?
>
> How could this be done?

Use the File System Object to pick up the names of all files with the
desired extension, then use the script below to generate the mail message.
You need to inlcude one .AddAttachment line for each file to be attached.

const cdoBasic=1
schema = "http://schemas.microsoft.com/cdo/configuration/"
Set objEmail = CreateObject("CDO.Message")
With objEmail
.From = "Ja...@company.com"
.To = "J...@company.com"
.Subject = "Test Mail"
.Textbody = "The quick brown fox " & Chr(10) & "jumps over the lazy dog"
.AddAttachment "d:\Testfile.txt"
With .Configuration.Fields
.Item (schema & "sendusing") = 2
.Item (schema & "smtpserver") = "mail.company.com"
.Item (schema & "smtpserverport") = 25
.Item (schema & "smtpauthenticate") = cdoBasic
.Item (schema & "sendusername") = "Ja...@company.com"
.Item (schema & "smtpaccountname") = "Ja...@company.com"
.Item (schema & "sendpassword") = "SomePassword"
End With
.Configuration.Fields.Update
.Send
End With

PAkerly

unread,
Dec 29, 2009, 9:10:58 AM12/29/09
to
On Dec 28, 7:07 pm, "Pegasus [MVP]" <n...@microsoft.com> wrote:
> "PAkerly" <pake...@gmail.com> said this in news itemnews:60dc06b6-6c7c-488a...@r14g2000vbc.googlegroups.com...


OK Here is what I am doing, I thought that this would grab all files
but it is only getting and attaching one, multiple times...

'Grab all .txt files
strFolder = "C:\MYFiles"
strExt = "txt"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strFolder)

For Each objFile in objFolder.Files
strFileExt = objFSO.GetExtensionName(objFile.Path)

strFile = objFile.Path
next

'email the files, there are usually no more than 3 files.
Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = "Example CDO Message"
objMessage.From = "m...@HOST.COM"
objMessage.To = "m...@HOST.COM"
objMessage.TextBody = "This is some sample message text."
objMessage.AddAttachment strFile
objMessage.AddAttachment strFile
objMessage.AddAttachment strFile

'==This section provides the configuration information for the remote
SMTP server.
'==Normally you will only change the server name or IP.
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") =
"myserver.host.com"
'Server port (typically 25)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") =
25
objMessage.Configuration.Fields.Update
'==End remote SMTP server configuration section==
objMessage.Send

Any ideas how I can make this work?

Pegasus [MVP]

unread,
Dec 29, 2009, 10:24:06 AM12/29/09
to

"PAkerly" <pak...@gmail.com> said this in news item

news:af062eab-c31a-44e9...@h2g2000vbd.googlegroups.com...

When you get out of your file scanning loop then strFile is set to the name
of the *last* file found, which is not really what you want. To attach all
files, you must move the loop further down, e.g. as below. I also note that
you're not checking for the correct file extension.

objMessage.TextBody = "This is some sample message text."

For Each objFile In objFolder.Files
strFileExt = objFSO.GetExtensionName(objFile.Path)
objMessage.AddAttachment objFile.Path
Next

PAkerly

unread,
Dec 29, 2009, 10:35:57 AM12/29/09
to
On Dec 29, 10:24 am, "Pegasus [MVP]" <n...@microsoft.com> wrote:
> "PAkerly" <pake...@gmail.com> said this in news itemnews:af062eab-c31a-44e9...@h2g2000vbd.googlegroups.com...

Does your code only grab text files? or all files?

Pegasus [MVP]

unread,
Dec 29, 2009, 11:15:43 AM12/29/09
to


"PAkerly" <pak...@gmail.com> said this in news item

news:f0f484a8-78c9-46b0...@33g2000vbe.googlegroups.com...

The code

objMessage.TextBody = "This is some sample message text."
For Each objFile In objFolder.Files
strFileExt = objFSO.GetExtensionName(objFile.Path)
objMessage.AddAttachment objFile.Path
Next

is actually your own code, not mine, and it grabs all files, which you will
see immediately when you modify it like so:

objMessage.TextBody = "This is some sample message text."
For Each objFile In objFolder.Files
strFileExt = objFSO.GetExtensionName(objFile.Path)

wscript.echo objFile.Path
Next
wscript.quit

Remember to run the code with cscript.exe from a Command Prompt.

PAkerly

unread,
Dec 29, 2009, 12:46:16 PM12/29/09
to
On Dec 29, 11:15 am, "Pegasus [MVP]" <n...@microsoft.com> wrote:
> "PAkerly" <pake...@gmail.com> said this in news itemnews:f0f484a8-78c9-46b0...@33g2000vbe.googlegroups.com...

So I have this code working but it seems to take at least 3 or 4
minutes to actually send the email. I have put in a messagebox in the
For Next loop and it finds the files quickly, but I dont get the email
for about 4 minutes. I also dont get the Finished message for a while.

'Email the files


strFolder = "C:\MYFiles"
strExt = "txt"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strFolder)

Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = "Testing my files email"
objMessage.From = "m...@server.com"
objMessage.To = "m...@server.com"
objMessage.TextBody = "This is a test, files should be attached to
this email."


For Each objFile In objFolder.Files
strFileExt = objFSO.GetExtensionName(objFile.Path)
objMessage.AddAttachment objFile.Path

msgbox "Got file!"
Next
'Configuration Info


objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") =

"server.host.com"
'Server port (typically 25)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") =
25
objMessage.Configuration.Fields.Update
'==End remote SMTP server configuration section==
objMessage.Send

MsgBox "Finished! An email is being sent"


Any idea why it might be taking so long?

Pegasus [MVP]

unread,
Dec 29, 2009, 1:31:14 PM12/29/09
to

"PAkerly" <pak...@gmail.com> said this in news item

news:4ec8ef2d-21b8-4e00...@g12g2000vbl.googlegroups.com...

It would make more sense to put the line [msgbox "Got file!"] *after* the
[next] line so that you can see when all files are attached. The time to
transmit the message depends on a number of factors, e.g.
- The number of files
- The size of all attachments
- The speed of the link to your SMTP server
- How quickly the SMTP server processes your message
- The speed of the link from your POP3 server to your PC
- How often your mail client polls the POP3 server

To locate the bottle neck you need to perform some tests, e.g. vary the
number/size of the attachments and/or use some other application to send a
test mail, e.g. Outlook Express.

0 new messages