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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find Total Folder & Files then E-Mail using CDO

Status
Not open for further replies.

Sphinxguy

IS-IT--Management
Jul 27, 2004
28
US
Hey Everyone,


I am working on a current script that calculates the total number of files ONLY then e-mails them to me. I am needing to change the script and add the total number of folders. Example would be this ...

"C:\GS"
"C:\GS\1"
"C:\GS\2"
.... etc

and this e-mail would just tell me 3 folders and X number of files. Below is the VBScript I'm working with to at least get the total number of files.





Dim FSO, vPath, FileCnt
Set FSO = CreateObject("scripting.filesystemobject")
vPath = "C:\gs\"
FileCnt = 0

ReturnFileCountUsingFSO vPath, FileCnt
If FileCnt > 0 Then
SendMailCDO "The total number of files in '" & vPath & "' is " & FileCnt, "MY E-MAIL SERVER"
End If
Set FSO = Nothing
Set vPath = Nothing
Set FileCnt = Nothing

Function ReturnFileCountUsingFSO(vPath, FileCnt)
Dim f, fld
On Error Resume Next 'in case no permission for folder
Set fld = FSO.GetFolder(vPath)
FileCnt = FileCnt + fld.Files.Count
For Each f In fld.SubFolders
ReturnFileCountUsingFSO f.path, FileCnt
Next
On Error GoTo 0
Set f = Nothing
Set fld = Nothing
End Function

Function SendMailCDO(strBody, SmtpServer)
Dim objCDO
Set objCDO = Nothing
On Error Resume Next
Set objCDO = CreateObject("CDO.Message")
On Error GoTo 0
If objCDO Is Nothing Then 'cdo must not be installed on the machine
SendMailCDO = True
Exit Function
End If
With objCDO
.Subject = "File Count"
.From = "THEIR E-MAIL ADDRESS" 'address does not have to exist
.To = "MY E-MAIL ADDRESS"
.TextBody = strBody
With .Configuration.Fields
.Item(" = 2
.Item(" = SmtpServer
.Item(" = 25
.Update
End With
.Send
End With
Set objCDO = Nothing
SendMailCDO = True
End Function




So bottom line is I would like it to calculate the number of folders and files in the C:\GS folder and have it in the e-mail. Again currently all I see in the e-mail is "The Total Number of Files in C:\GS is XXX".

Any help would be honestly appreciated it!
 
[0] What I'm not impressed the most is that collision of global variable names and the function argument names (defaulted to be passed the reference). This is a big confusion. If you want to rely fully on the global variable declared outside, push it through thoroughly, like your use of fso. Otherwise, keep the names distinct.

[0.1] I do not do that for you hereinafter.

[1] The simplest is to add a FolderCnt say. Like this.
[tt]
Dim FSO, vPath, FileCnt[blue], FolderCnt[/blue]
Set FSO = CreateObject("scripting.filesystemobject")
vPath = "C:\gs\"
FileCnt = 0
[blue]FolderCnt=0[/blue]
ReturnFileCountUsingFSO vPath, FileCnt[blue], FolderCnt[/blue]
If FileCnt > 0 Then [blue]'or some more elaborated condition[/blue]
SendMailCDO "The total number of files in '" & vPath & "' is " & FileCnt [blue]& vbcrlf & "The total number of subfolders in '" & vPath & "' is " & FolderCnt[/blue], "MY E-MAIL SERVER"
End If

'etc etc

Function ReturnFileCountUsingFSO(vPath, FileCnt[blue],FolderCnt[/blue])
Dim f, fld
On Error Resume Next 'in case no permission for folder
Set fld = FSO.GetFolder(vPath)
FileCnt = FileCnt + fld.Files.Count
[blue]FolderCnt=FolderCnt+fld.SubFolders.Count[/blue]
For Each f In fld.SubFolders
ReturnFileCountUsingFSO f.path, FileCnt[blue], FolderCnt[/blue]
Next
'etc etc
End Function

'etc etc[/tt]
 
i have to admit Tsuji i am guilty of deliberately doing that. i mean reusing the variable names from the scope which passes the parameters to the function/sub which uses them (not updates necessarily).
I agree with you that its not best practice and i must get out of the habit. however, for me, it stops me getting confused. i think i just need to adopt a naming convention for the one which is passed, something like prefixing with an 'l'? i avoid global variables altogether so the 'g' prefix for them isnt relevant for me


ReturnFileCountUsingFSO vPath, FileCnt


Function ReturnFileCountUsingFSO(ByVal lvPath, ByVal lFileCnt)
 
I think what is important to a scripter/coder is the type, the scope (and the life-cycle, ...) of a variable. And that a function/sub, its functionality. Names are of no importance. But, living creature's memory (short and long)is something pathetic. That's why the saying of devising name to reflect ... or whatever for a larger project. (I don't think op would appreciate that.)

A variable name of 5-key-stroke is an invitation of 26^5 (figuratively) possible physical errors. It is an increase of I-don't-know-how-many loads of brain waves to read it. For a developer, if they cannot free themselves of that fetter to let their logical deduction free, it won't go very far.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top