Need help writing a vb script to check a file size

I need some help writing a vb script to check a file size and if it doesn't equal a certain size range then send a page or sms/email.  I appreciate it very much!

Roy
LVL 1
WeberTechAsked:
Who is Participating?
 
sirbountyCommented:
Not much, other than a couple of display prompts.
Perhaps it's marked as spam/junk?
Const iSize = 47104
Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.GetFile("C:\Test\Test.txt")
If objFile.Size < iSize Then 
  wscript.echo "Calling SendMail routine..."
  SendMail (objFile.Name)
End If
wscript.quit

Sub SendMail(strFile)
  YourSMTPServer = "smtp.domain.com"
  Recipient = "Admin@Domain.Com"
  Sender = "Someone@Somewhere.com"

  set iMsg = CreateObject("CDO.Message")
  set iConf = CreateObject("CDO.Configuration")
  Set Flds = iConf.Fields

  With Flds
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = YourSMTPServer
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 10
    .Update
  End With

  strSubject = "Alert" 
  strBody = "The file, " & strFile & " is not above the preset limit of " & iSize & "." 

  With iMsg
    Set .Configuration = iConf
    If Not IsEmpty (Recipient) Then .To = Recipient
    If Not IsEmpty (Sender) Then .From = Sender
    If Not IsEmpty (strSubject) Then .Subject = strSubj
    If Not IsEmpty (strBody) Then .TextBody = strBody
    .Send 
    wscript.echo "Mail sent."
  End With
End Sub

Open in new window

0
 
sirbountyCommented:
This should work...depending on where your file is...
Const iSize = 10000
Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.GetFile("C:\Test\Test.txt")
If objFile.Size > iSize Then SendMail (objFile.Name)
wscript.quit

Sub SendMail(strFile)
  YourSMTPServer = "smtp.domain.com"
  Recipient = "Admin@Domain.Com"
  Sender = "Someone@Somewhere.com"

  set iMsg = CreateObject("CDO.Message")
  set iConf = CreateObject("CDO.Configuration")
  Set Flds = iConf.Fields

  With Flds
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = YourSMTPServer
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 10
    .Update
  End With

  strSubject = "Alert" 
  strBody = "The file, " & strFile & " exceeds the preset size of " & iSize & "." 

  With iMsg
    Set .Configuration = iConf
    If Not IsEmpty (Recipient) Then .To = Recipient
    If Not IsEmpty (Sender) Then .From = Sender
    If Not IsEmpty (strSubject) Then .Subject = strSubj
    If Not IsEmpty (strBody) Then .TextBody = strBody
    .Send 
  End With
End Sub

Open in new window

0
 
WeberTechAuthor Commented:
Thank you SirBounty for you help!  If I want to to only send a message out if it is below the file size that I preset, like if the file size is less than 46MB, then send a message.  This way I know a job didn't finish.

Roy
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
sirbountyCommented:
If I remember correctly, the fso file size is in bytes, meaning you'd have to set the limit at 47104.

Then simply change the logic to only send, if less than...
Const iSize = 47104
Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.GetFile("C:\Test\Test.txt")
If objFile.Size < iSize Then SendMail (objFile.Name)
wscript.quit

Sub SendMail(strFile)
  YourSMTPServer = "smtp.domain.com"
  Recipient = "Admin@Domain.Com"
  Sender = "Someone@Somewhere.com"

  set iMsg = CreateObject("CDO.Message")
  set iConf = CreateObject("CDO.Configuration")
  Set Flds = iConf.Fields

  With Flds
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = YourSMTPServer
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 10
    .Update
  End With

  strSubject = "Alert" 
  strBody = "The file, " & strFile & " is not above the preset limit of " & iSize & "." 

  With iMsg
    Set .Configuration = iConf
    If Not IsEmpty (Recipient) Then .To = Recipient
    If Not IsEmpty (Sender) Then .From = Sender
    If Not IsEmpty (strSubject) Then .Subject = strSubj
    If Not IsEmpty (strBody) Then .TextBody = strBody
    .Send 
  End With
End Sub

Open in new window

0
 
WeberTechAuthor Commented:
I tried this and it doesn't seem to be working.  Any ideas?
0
 
sirbountyCommented:
There's a few components needed from your end.
First of all - the file, it's path and name.
That gets replaced on this line:
  Set objFile = objFSO.GetFile("C:\Test\Test.txt")

Next, an smtp server that you can authenticate against.
That's declared on this line:
  YourSMTPServer = "smtp.domain.com"

There may be more configuration needed for your smtp server...depends on what you're using.

You'll also need to change the email address for sender/receiver:
  Recipient = "Admin@Domain.Com"
  Sender = "Someone@Somewhere.com"

0
 
WeberTechAuthor Commented:
Yes, I have changed those fields.  Is there anyway to add logging to this?  I'm not sure where its failing.
0
 
RobSampsonCommented:
46MB in bytes would be 48234496, so change
Const iSize = 47104

to
Const iSize = 48234496

Rob.
0
 
WeberTechAuthor Commented:
Rob,
Thank you!  It is working now.  The only issue I have left is the subject line isn't populating.  Any solution to that?
Roy
0
 
sirbountyCommented:
Yes, type-o...

If Not IsEmpty (strSubject) Then .Subject = strSubj
should read

If Not IsEmpty (strSubject) Then .Subject = strSubject
0
 
WeberTechAuthor Commented:
Perfect!  It works flawlessly!  Thank you all for your help.
Roy
0
 
WeberTechAuthor Commented:
Thank you for your help in this!  I really appreciate it!
0
 
WeberTechAuthor Commented:
Is there a way to put in a if statement to say, if it is above the size to send an email that the job ran successfuly?
0
 
WeberTechAuthor Commented:
I meant an else statement.
0
 
RobSampsonCommented:
Try this.  It will send an email whether its over or under the size.

Regards,

Rob.
Const iSize = 48234496
Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.GetFile("C:\Test\Test.txt")
If objFile.Size < iSize Then 
  wscript.echo "Calling SendMail routine..."
  SendMail "The file, " & objFile.Name & " is not above the preset limit of " & iSize & "."
Else
  SendMail "The file, " & objFile.Name & " is above the preset limit of " & iSize & ". The job ran successfully."
End If
wscript.quit

Sub SendMail(strBody)
  YourSMTPServer = "smtp.domain.com"
  Recipient = "Admin@Domain.Com"
  Sender = "Someone@Somewhere.com"

  set iMsg = CreateObject("CDO.Message")
  set iConf = CreateObject("CDO.Configuration")
  Set Flds = iConf.Fields

  With Flds
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = YourSMTPServer
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 10
    .Update
  End With

  strSubject = "Alert" 

  With iMsg
    Set .Configuration = iConf
    If Not IsEmpty (Recipient) Then .To = Recipient
    If Not IsEmpty (Sender) Then .From = Sender
    If Not IsEmpty (strSubject) Then .Subject = strSubj
    If Not IsEmpty (strBody) Then .TextBody = strBody
    .Send 
    wscript.echo "Mail sent."
  End With
End Sub

Open in new window

0
 
WeberTechAuthor Commented:
Is it possible instead of putting an Alert in the subject line to make it, if it wasn't successful, to put The job failed, and if it was size is above the size limit, the subject line would say the Job Completed?
0
 
WeberTechAuthor Commented:
I tried adding a strSubject under the send mail on each statement but it keeps sending an email with no subject line.
0
 
sirbountyCommented:
There are several ways of accomplishing that...using Rob's last post, here's one method:
(and the subject should be corrected now as well)

You can make the subject whatever you want - see the result logic
  If result = "False" Then
    strSubject = "Below size" 'adjust as needed
  Else
    strSubject = "Above size" 'adjust as needed
  End If
Const iSize = 48234496
Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.GetFile("C:\Test\Test.txt")
If objFile.Size < iSize Then 
  wscript.echo "Calling SendMail routine..."
  SendMail "The file, " & objFile.Name & " is not above the preset limit of " & iSize & ".", "False"
Else
  SendMail "The file, " & objFile.Name & " is above the preset limit of " & iSize & ". The job ran successfully.", "True"
End If
wscript.quit

Sub SendMail(strBody, result)
  YourSMTPServer = "smtp.domain.com"
  Recipient = "Admin@Domain.Com"
  Sender = "Someone@Somewhere.com"

  If result = "False" Then
    strSubject = "Below size" 'adjust as needed
  Else
    strSubject = "Above size" 'adjust as needed
  End If

  set iMsg = CreateObject("CDO.Message")
  set iConf = CreateObject("CDO.Configuration")
  Set Flds = iConf.Fields

  With Flds
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = YourSMTPServer
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 10
    .Update
  End With


  With iMsg
    Set .Configuration = iConf
    If Not IsEmpty (Recipient) Then .To = Recipient
    If Not IsEmpty (Sender) Then .From = Sender
    If Not IsEmpty (strSubject) Then .Subject = strSubject
    If Not IsEmpty (strBody) Then .TextBody = strBody
    .Send 
    wscript.echo "Mail sent."
  End With
End Sub

Open in new window

0
 
WeberTechAuthor Commented:
Ah!  Ok, thank you very much!
0
 
WeberTechAuthor Commented:
Ok guys, this script runs great.  Now I have a new problem.  I found out that the log file size is fluctuating.  Is it possible to just have the script scan the file for a phrase like Rebuild of dynsum completed?
0
 
RobSampsonCommented:
That would require a rather different solution, so please post a new question.  You can click the "ask a related question" link and post a new one.

FYI, it would take a long time to scan a 45MB log file for text.....

Rob.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.