We help IT Professionals succeed at work.

MSDOS or Pearl or VB Script or Jscriptt to check the file size and disk space and send the alert.

AnandSahoo
AnandSahoo asked
on
Some XML files are generate on one server  and comes to other server. The script and path is set in a different application log file. When they are coming to 2nd server, in case  the disk space of the server is less than the files size, the file is not getting saved properly.It shows 0 KB. Can you provide a tool or script which will evaluate the size of the file and space of the server .. Once the server space is less than the files size  it should send a alert. Alert by email is better.

Comment
Watch Question

Bill PrewTest your restores, not your backups...
Expert of the Year 2019
Top Expert 2016

Commented:
So, will the script have available to it the path of the file to copy, and the destination folder path?

~bp

Author

Commented:
Yes... It will have both source and destination path. We can save the filw n destination server and make it run automatically. Whenever it finds.the source file size is more than the destination drive path, it can alert.
Bill PrewTest your restores, not your backups...
Expert of the Year 2019
Top Expert 2016

Commented:
Okay, here's a VB script that should get you pretty close, but you will have to adjust as needed.  For the email sending, take a look at the following reference for different settings that may be needed depending on your environment.

http://www.paulsadowski.com/wsh/cdo.htm

' Constants needed
Const cdoSendUsingPickup = 1 'Send message using the local SMTP service pickup directory.
Const cdoSendUsingPort = 2 'Send the message using the network (SMTP over the network).
Const cdoAnonymous = 0 'Do not authenticate
Const cdoBasic = 1 'basic (clear-text) authentication
Const cdoNTLM = 2 'NTLM

' Specify file to be copied, and folder copying to (need trailing backslash!)
strCopyFile = "c:\ee\EE27482608\temp.txt"
strDestDir = "g:\ee\"

' Try to do the copy, and end
DoCopy strCopyFile, strDestDir
Wscript.Quit

Sub DoCopy(strFile, strDir)
   Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")

   ' Get the source file object
   Set objFile = objFSO.GetFile(strFile)

   ' Get the dest folder and drive objects
   Set objDir = objFSO.GetFolder(strDir)
   Set objDrive = objDir.Drive

   ' See if there is enough space to fit this file
   If objFile.Size > objDrive.FreeSpace Then
      ' Not enough space, send email
      SendEmail objFile.Path, objFile.Size, objDir.Path, objDrive.FreeSpace
   Else
       ' Enougn space, copy the file
       objFSO.CopyFile strFile, strDir
   End If

   ' Free objects
   Set objDrive = Nothing
   Set objDir = Nothing
   Set objFile = Nothing
   Set objFSO = Nothing
End Sub

Sub SendEmail(strFile, intSize, strDir, intFree)
   ' Create CDO object
   Set objEmail = CreateObject("CDO.Message")

   ' Specify settings needed for email and send it
   objEmail.From = "your.from@you.com"
   objEmail.To = "your.to@you.com"
   objEmail.Subject = "Insufficient Space"
   objEmail.Textbody = "Insufficient space to copy file." & vbCrLf & _
      "  File Path: " & strFile & vbCrLf & _
      "  File Size: " & intSize & vbCrLf & _
      "  Dest Path: " & strDir & vbCrLf & _
      "  Dest Free: " & intFree
   objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
   objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.you.com"
   objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
   objEmail.Configuration.Fields.Update
   objEmail.Send

   ' Free object
   Set objEmail = Nothing
End Sub

Open in new window

~bp

Author

Commented:
Let me try this. Can you please help me with the steps to set it in the pc. Also if we can scedule it to run automatically will be grateful.
Bill PrewTest your restores, not your backups...
Expert of the Year 2019
Top Expert 2016

Commented:
Save it as a VBS file.  Edit the two lines near the top to reference your file and folder.  Edit the needed lines in the email routine to your server and addresses, etc.

There are plenty of tutorials on using the windows task scheduler to schedule a program to run, here's one example (make sure you also view the "part 2" link).

http://www.windowsnetworking.com/articles_tutorials/Working-Windows-Server-2008-Task-Scheduler-Part1.html

~bp

Author

Commented:
M testing the above vb script n will get back by eod.
I also want to implement the similar thing in unix environment. Can i do it in pearl ?
Bill PrewTest your restores, not your backups...
Expert of the Year 2019
Top Expert 2016

Commented:
Sorry, I don't work with perl.

~bp

Author

Commented:
Its throwing me error as below
send using configuration value is invalid.
Cdo.message.1

Also can we add the email if the source file is not available or deleted?

Author

Commented:
I used the following Script. Since smtp is not available with me was trying to use gmail.

' Constants needed
Const cdoSendUsingPickup = 1 'Send message using the local SMTP service pickup directory.
Const cdoSendUsingPort = 2 'Send the message using the network (SMTP over the network).
Const cdoAnonymous = 0 'Do not authenticate
Const cdoBasic = 1 'basic (clear-text) authentication
Const cdoNTLM = 2 'NTLM

' Specify file to be copied, and folder copying to (need trailing backslash!)
strCopyFile = "c:\Program Files.zip"
strDestDir = "z:\"

' Try to do the copy, and end
DoCopy strCopyFile, strDestDir
Wscript.Quit

Sub DoCopy(strFile, strDir)
   Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")

   ' Get the source file object
   Set objFile = objFSO.GetFile(strFile)

   ' Get the dest folder and drive objects
   Set objDir = objFSO.GetFolder(strDir)
   Set objDrive = objDir.Drive

   ' See if there is enough space to fit this file
   If objFile.Size > objDrive.FreeSpace Then
      ' Not enough space, send email
       SendEmail objFile.Path, objFile.Size, objDir.Path, objDrive.FreeSpace
   Else
       ' Enougn space, copy the file
       objFSO.CopyFile strFile, strDir
   End If

   ' Free objects
   Set objDrive = Nothing
   Set objDir = Nothing
   Set objFile = Nothing
   Set objFSO = Nothing
End Sub
Sub SendEmail(strFile, intSize, strDir, intFree)

   ' Create CDO object
Set emailObj = CreateObject("CDO.Message")
emailObj.From = "sahooanand@gmail.com"
emailObj.To = "sahooanand@gmail.com"
emailObj.Subject = "Insufficient Space"
emailObj.TextBody = "Insufficient space to copy file." & vbCrLf & _
      "  File Path: " & strFile & vbCrLf & _
      "  File Size: " & intSize & vbCrLf & _
      "  Dest Path: " & strDir & vbCrLf & _
      "  Dest Free: " & intFree
        
Set emailConfig = emailObj.Configuration
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtppusessl") = true
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername") = "sahooanand@gmail.com"
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "Test123"
emailConfig.Fields.Update
emailObj.Send
Set emailObj = nothing
Set emialConfig = nothing
End Sub

This is the error I am getting "The transport failed to connect to the server"
Bill PrewTest your restores, not your backups...
Expert of the Year 2019
Top Expert 2016

Commented:
I did a test using gmail with the same code you have, but different account info, it send fine. Check your account info, and any firewalls on the computer or the network.

~bp

Author

Commented:
Ok, I think I got the idea now.

Can we just edit the script to check for the availability of the file before comparing with disk space.
If the file is not available or deleted send the email.
Test your restores, not your backups...
Expert of the Year 2019
Top Expert 2016
Commented:
Okay, here's a changed version that adds more robust error checking.  This should catch any problems in the copy process and report them, not just the ones you have mentioned.

' Let VBS handle errors
On Error GoTo 0

' Constants needed
Const cdoSendUsingPickup = 1  'Send message using the local SMTP service pickup directory.
Const cdoSendUsingPort = 2    'Send the message using the network (SMTP over the network).
Const cdoAnonymous = 0        'Do not authenticate
Const cdoBasic = 1            'basic (clear-text) authentication
Const cdoNTLM = 2             'NTLM

' Specify file to be copied, and folder copying to (need trailing backslash!)
strCopyFile = "c:\Program Files.zip"
strDestDir  = "z:\"

' Try to do the copy, and end
DoCopy strCopyFile, strDestDir
Wscript.Quit

Sub DoCopy(strFile, strDir)
    Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")

    ' Take over handling of errors
    On Error Resume Next

    ' Try to do the copy now
    objFSO.CopyFile strFile, strDir

    ' If any errors occur, send email with details
    If Err.Number <> 0 Then
        DoError strCopyFile, strDestDir, "Errors while copying file!"
    End If

    ' Let VBS handle errors
    On Error GoTo 0

    ' Free objects
    Set objFSO   = Nothing
End Sub


Sub DoError(strFrom, strTo, strSubject)
    ' If any errors occur, send email with details
    strBody = "  File Path: " & strFrom & vbCrLf & _
              "  Dest Path: " & strTo & vbCrLf & vbCrLf & _
              "  Error Source: " & Err.Source & vbCrLf & _
              "  Error Number: " & Err.Number & vbCrLf & _
              "  Error Description: " & Err.Description
    Err.Clear
    On Error GoTo 0
    SendEmail strSubject, strBody
    On Error Resume Next
End Sub

Sub SendEmail(strSubject, strBody)

    ' Create CDO object
    Set emailObj      = CreateObject("CDO.Message")
    emailObj.From     = "sahooanand@gmail.com"
    emailObj.To       = "sahooanand@gmail.com"
    emailObj.Subject  = strSubject
    emailObj.TextBody = strBody

    Set emailConfig   = emailObj.Configuration
    emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
    emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
    emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
    emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtppusessl") = True
    emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername") = "sahooanand@gmail.com"
    emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "Test123"
    emailConfig.Fields.Update
    emailObj.Send

    Set emailObj = Nothing
    Set emialConfig = Nothing
End Sub

Open in new window

Author

Commented:
Petfect ..im sure this will do. Thanks for the time n effort u have put for me.
Bill PrewTest your restores, not your backups...
Expert of the Year 2019
Top Expert 2016

Commented:
Welcome, glad to help.

~bp