1

OK.. almost there.. the output.txt file is still somewhat incorrect in that it was incomplete...also it doesn't send email. Again, I'm sure it's something stupid. Any help appreciated

On Error Resume Next

dim dosendmail
set dosendmail= false

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objoutFile = objFSO.CreateTextFile("output.txt", True)
Set objServerlist = objFSO.OpenTextFile("servers.txt", 1)

Do Until objServerList.AtEndOfStream
strComputer = objServerList.ReadLine


strOldestFile = ""
dtmOldestDate = Now


Set objFolder = objFSO.Getfolder(strcomputer)

intFolderSize = Int((objFolder.Size / 1024) / 1024)

If intFolderSize > 1 Then

    strOldestFile = ""
    dtmOldestDate = Now


    intFolderSize = Int((objFolder.Size / 1024) / 1024)

     If intFolderSize > 1 and doSendmail = False Then
        doSendMail=true
           Set colFiles = objFolder.Files
           For Each objFile in colFiles
                strFile = objFile.Path
                dtmFileDate = objFile.DateCreated
                If dtmFileDate < dtmOldestDate Then
                     dtmOldestDate = dtmFileDate
                     strOldestFile = strFile

                End If

           Next
     End If  


    objoutfile.writeline strcomputer & stroldestfile  & ":  " & dtmoldestdate

    If DoSEndMail = True Then

        Set myobj = CreateObject("Scripting.FileSystemObject")

        const ForReading = 1
           Set f = myobj.OpenTextFile("output.txt", ForReading)
           ReadAllText =   f.ReadAll

        Dim objEmail
        strEmailFrom = "from@email.com"
        strEmailTo ="me@email.com"
        strSubject ="TEST"
        strMessage =strMessage & ReadAllText

          Set objEmail = CreateObject("CDO.Message")

          objEmail.From = strEmailFrom
          objEmail.To= strEmailTo
          objEmail.Subject = strSubject
          objEmail.HTMLBody = strMessage

        objEmail.Configuration.Fields.Item _
            ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
        objEmail.Configuration.Fields.Item _
            ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _
                "smtpaddress"
        objEmail.Configuration.Fields.Item _
            ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
        objEmail.Configuration.Fields.Update 
    End If    

End If        

Loop

I feel like I'm close.. Finally.

dbutts
  • 17
  • 7
  • See the answer in [this](http://stackoverflow.com/questions/4388879/vbscript-output-to-console) StackOverflow post. It all depends on how you are running your script: cscript or wscript. – codechurn Nov 17 '14 at 17:56
  • At first glance it looks like your `For Each` objFile variable overwrites your initially declared output file. – Filburt Nov 17 '14 at 17:57
  • I should've specified. I'm using cscript. – dbutts Nov 17 '14 at 17:57
  • Thanks Filburt..that was it! D'oh!!! OK.. now, more interesting question, and I'm out of my depth here: how can I get this to conditionally send email IF & only IF the value of INTFOLDERSIZE > 1 ? – dbutts Nov 17 '14 at 18:03
  • This is working, btw: – dbutts Nov 17 '14 at 18:05

1 Answers1

1

Before you go on get rid of the re-initialization of objFSO inside the Do...Loop, same as objFolder and the rest of the variables you copied.

Concerning sending the email: The simplest way would be to declare a variable doSendMail at top level, initialize it to false and inside your Loop set it to true

If intFolderSize > 1 AND doSendMail = False Then
    doSendMail = True
End If

Finally check doSendMail after your Do...Loop and act accordingly.

Filburt
  • 17,626
  • 12
  • 64
  • 115
  • OK.. Filburt, great work.. I'm almost home.. see new code above.. It's not sending email; again, I'm sure it's somethign stupid – dbutts Nov 17 '14 at 19:35
  • You placed your email sending code inside the Do...Loop - this is surely not what you intend if you attach the output.txt file containing your collected results. – Filburt Nov 17 '14 at 19:49