Recently, users have a need to monitor the number of files in the folder. If the threshold is exceeded, send a mail warning. I found some VB script code on the Internet and modified it myself.
The source code is as follows:
Strmessage = "file number more than10, please check"
Strto = "sample@home.cn"
Strfrom = "sample@home.cn"
Strsubject = "file number more than 10, please check"
Strsmtpserver = "smtp.home.cn" '-- SMTP address
Strcomputer = "."
Set ob1_miservice = GetObject ("winmgmts: \" & strcomputer & "\ Root \ cimv2 ")
Do While true
Set colfilelist = obw.miservice. execquery _
("Associators of {win32_directory.name = 'd: \ sample \ '} Where "_
& "Resultclass = cim_datafile ")
If colfilelist. Count> = 10 then
Sendmail strfrom, strto, strsubject, strmessage, strsmtpserver
Exit do
End if
Wscript. Sleep 600000 '-- one hour
Loop
'Use the SMTP server to send emails
Function Sendmail (strfrom, strsendto, strsubject, strmessage, strsmtp)
Setoemail = Createobject ("CDO. Message ")
'Configuremessage
Withoemail. configuration. Fields
. Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
. Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
. Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = strsmtp
. Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 0' do not perform verification
. Update
Endwith
'Build message
Withoemail
. From = strfrom
. To = strsendto
. Subject = strsubject
. Textbody = strmessage
Endwith
'Send message
Onerror resume next
Oemail. Send
Iferr then
Wscript. Echo "Sendmail failed:" & err. Description
Endif
End Function