?
Solved

Need help writing a vb script to check a file size

Posted on 2011-10-26
21
Medium Priority
?
375 Views
Last Modified: 2012-06-21
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
0
Comment
Question by:WeberTech
  • 12
  • 6
  • 3
21 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 37032192
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
 
LVL 1

Author Comment

by:WeberTech
ID: 37032468
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
 
LVL 67

Expert Comment

by:sirbounty
ID: 37032538
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 1

Author Comment

by:WeberTech
ID: 37033339
I tried this and it doesn't seem to be working.  Any ideas?
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 37033911
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
 
LVL 1

Author Comment

by:WeberTech
ID: 37033996
Yes, I have changed those fields.  Is there anyway to add logging to this?  I'm not sure where its failing.
0
 
LVL 67

Accepted Solution

by:
sirbounty earned 1332 total points
ID: 37034333
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
 
LVL 65

Assisted Solution

by:RobSampson
RobSampson earned 668 total points
ID: 37035466
46MB in bytes would be 48234496, so change
Const iSize = 47104

to
Const iSize = 48234496

Rob.
0
 
LVL 1

Author Comment

by:WeberTech
ID: 37037890
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
 
LVL 67

Assisted Solution

by:sirbounty
sirbounty earned 1332 total points
ID: 37038124
Yes, type-o...

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

If Not IsEmpty (strSubject) Then .Subject = strSubject
0
 
LVL 1

Author Comment

by:WeberTech
ID: 37038207
Perfect!  It works flawlessly!  Thank you all for your help.
Roy
0
 
LVL 1

Author Closing Comment

by:WeberTech
ID: 37038220
Thank you for your help in this!  I really appreciate it!
0
 
LVL 1

Author Comment

by:WeberTech
ID: 37042213
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
 
LVL 1

Author Comment

by:WeberTech
ID: 37042224
I meant an else statement.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 37042226
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
 
LVL 1

Author Comment

by:WeberTech
ID: 37044399
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
 
LVL 1

Author Comment

by:WeberTech
ID: 37044542
I tried adding a strSubject under the send mail on each statement but it keeps sending an email with no subject line.
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 37044574
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
 
LVL 1

Author Comment

by:WeberTech
ID: 37044607
Ah!  Ok, thank you very much!
0
 
LVL 1

Author Comment

by:WeberTech
ID: 37063001
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
 
LVL 65

Expert Comment

by:RobSampson
ID: 37066357
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month16 days, 1 hour left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question