Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB Script to 'type' a file and send via email

Status
Not open for further replies.

jpotucek

Technical User
Jan 26, 2005
144
US
Let me preface by saying that I am not a VB Programmer.
a VB guy that used to work here setup a VB Script to run via windows job scheduler (his code below) to show the file attributes for 2 different files and then email the results.

What I would like to accomplish is to 'type' or display the contents of a small text file and then email the results. Can this be accomplished easily by modifying his script??? if so, can someone help me with the code?
___________________________________
'Script to get the last modified date for a file.

Dim fso, f1, f2

'Set the file attrribute to append
Const ForWriting = 8
Set fso = CreateObject("Scripting.FileSystemObject")


' Get a File object to query.

Set f1 = fso.GetFile("\\server\IT\file1.dmp")

set f2 = fso.GetFile("\\server\IT\file2.db")



Set objEmail = CreateObject("CDO.Message")
objEmail.From = "email_address@server_name.ourdomain.com"
objEmail.To = "Distribution_Group@ourdomain.com"
objEmail.Subject = "IT Operations Status Report-DB backup Status"
objEmail.Textbody = f1.name & " " & f1.DateLastModified & " " & vbcrlf & vbcrlf & _
f2.name & " " & f2.DateLastModified


'== remote smtp configuration


objEmail.Configuration.Fields.Item _
(" = 2

'Name or IP of Remote SMTP Server
objEmail.Configuration.Fields.Item _
(" = "000.000.0.0"

'Server port (typically 25)
objEmail.Configuration.Fields.Item _
(" = 25


objEmail.Configuration.Fields.Update

'==End remote SMTP server configuration section==


objEmail.Send
 
something like this ?
objEmail.Textbody = f1.name & " " & f1.DateLastModified & " " & vbcrlf & vbcrlf & f1.OpenAsTextStream(1).ReadAll

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
I copied the original .vbs file to a new file and modified and saved it. when I try and run it I get something about a compilation error line 22?
__________________________________________
'Script to get the last modified date for a file.



Dim fso, f1, f2

'Set the file attrribute to append
Const ForWriting = 8
Set fso = CreateObject("Scripting.FileSystemObject")


' Get a File object to query.

Set f1 = fso.GetFile("\\stm-xp03\n$\Program Files\Avails_Batch Avails DB\test_Avails_Batch.log")


Set objEmail = CreateObject("CDO.Message")
objEmail.From = "db_montor_srvr@ol-dbsrv-01.olntv.com"
objEmail.To = "jpotucek@olntv.com"
objEmail.Subject = "Avails Import Status for Oropa"
objEmail.Textbody = f1.name & " " & f1.DateLastModified & " " & vbcrlf & vbcrlf & f1.OpenAsTextStream
(1).ReadAll



'== remote smtp configuration


objEmail.Configuration.Fields.Item _
(" = 2

'Name or IP of Remote SMTP Server
objEmail.Configuration.Fields.Item _
(" = "192.168.0.50"

'Server port (typically 25)
objEmail.Configuration.Fields.Item _
(" = 25


objEmail.Configuration.Fields.Update

'==End remote SMTP server configuration section==


objEmail.Send
 

nevermind.. I found my syntax error. It worked like a champ!!! Thank you very much!!!!
 
One more question !!!!! I'd like to add a second file to the script and then have it display file info like DataLastModified AND the size of the file (# of bytes) here's what I've got so far:

Set f1 = fso.GetFile("\\stm-xp03\n$\Program Files\Avails_Batch Avails DB\test_Avails_Batch.log")

set f2 = fso.GetFile("\\stm-sim1\d$\Storer\OLNSC_s.dat")


Set objEmail = CreateObject("CDO.Message")
objEmail.From = "db_montor_srvr@ol-dbsrv-01.olntv.com"
objEmail.To = "jpotucek@olntv.com"
objEmail.Subject = "This is only a Test ** Avails Import Status for Oropa"

objEmail.Textbody = f1.name & " " & f1.DateLastModified & " " & vbcrlf & vbcrlf & f1.OpenAsTextStream (1).ReadAll _
f2.name & " " & f2.DateLastModified <---- IT ERRORS OUT ON THIS LINE

And... I can't figure out how to make it tell me the size of the file in bytes???????
 
objEmail.Textbody = f1.name & " " & f1.DateLastModified & " " & vbcrlf & vbcrlf & f1.OpenAsTextStream (1).ReadAll _
& vbCrLf & f2.name & " " & f2.DateLastModified

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 

worked like a champ!!!!!

THANK YOU SO MUCH!!!!!!!!!!!!!!!!!!!!!!!!!!
 

Is there a way to also display the FILESIZE on the 'f2' file in addition to the LastModifiedDate?

I guess I should really pick myself up a VB book ;)
 
f2.Size

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 

Once again... my savior!! This VB scripting stuff is turning out to be pretty cool. I have some serious reading to do!!!

Thanks again!
 

One more (final) question:

Here is my code to display the contents of one file and they just the file attributes of another:


objEmail.Textbody = f1.name & " " & f1.DateLastModified & " " & vbcrlf & vbcrlf & f1.OpenAsTextStream (1).ReadAll _
& vbCrLf & f2.name & " " & f2.DateLastModified & " " & vbcrlf & vbcrlf & f2.Size

This code works great, but I'd like to modify it further.
Is there a way to display only the last few lines of the 'f1' file in my email and not the WHOLE 'f1'file? Seems that the Programmer embedded some sensitive passwords in his executable and they show up in the 'f1' file that I'm emailing with the above code. :)
 
Then you can use the
f1.readline and f1.skipline methods


Please tell me if I'm wrong I like to learn from my mistakes...
_____________________________________
Feed a man a fish and feed him for a day.
Teach a man to fish and feed him for a lifetime...
 
Sounds like it will work... So how do I code skipping the first 25 lines of text?
 
Try this:

Code:
Do
    f1.skipline
Loop until f1.line = 25



Please tell me if I'm wrong I like to learn from my mistakes...
_____________________________________
Feed a man a fish and feed him for a day.
Teach a man to fish and feed him for a lifetime...
 
some clarification: .line returns the current line number...

Here you can get the Windows Script 5.6 Documentation

But feel free to pop any question you want...
We're here to help...

Please tell me if I'm wrong I like to learn from my mistakes...
_____________________________________
Feed a man a fish and feed him for a day.
Teach a man to fish and feed him for a lifetime...
 
I downloaded the Windows scripting documentation - Thank you Very much!!!!

It looks like your suggestion will work, I'm just having a little trouble with the syntax:

objEmail.Textbody = f1.name & " " & f1.DateLastModified & " " & vbcrlf & vbcrlf & f1.OpenAsTextStream (1).ReadAll _
& f1.skipline & Loop until f1.line = 20
 
myArr = Split(f1.OpenAsTextStream (1).ReadAll, vbCrLf)
myStr = ""
For i = 25 To UBound(myArr)
myStr = myStr = myArr(i) & vbCrLf
Next
objEmail.Textbody = f1.name & " " & f1.DateLastModified & " " & vbcrlf & vbcrlf & myStr _
& vbCrLf & f2.name & " " & f2.DateLastModified & " " & vbcrlf & vbcrlf & f2.Size

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 

Hi,

I used the above code and it stops on line 30 which is

objEmail.Textbody = f1.name & " " & f1.DateLastModified & " " & vbcrlf & vbcrlf & myStr _

error is as follows:
Line 30 Character 1 object required: "



 
Sorry for the typo in line 28:
myStr = myArr(i) & vbCrLf

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
i hate the _ line follow thing. always confuses me.

perhaps it is that? i doubt it, anyway

for testing chuck in

Msgbox IsObject(f1)
Msgbox IsObject(f2)
Msgbox f1.name
Msgbox f1.DateLastModified
MSgbox f2.name
Msgbox f2.DateLastModified
Msgbox myStr

is there a & straight after the _ ?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top