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

Counting script 2

Status
Not open for further replies.

patrick118

Technical User
Jan 14, 2004
315
NL
Maybe someone can help me with this.

I am new to writing vb scripts so sorry for stupid questions

I want a scripts that checks a folder for the amount of files and the take an action.

Example.

If there are more then 5 files in it do nothing. If there are more then 5 files in it send an email to pietje@pietje.nl

I got this but is is completely wrong

Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists("c:\test") Then
Set objFolder = objFSO.GetFile("C:\test")
Else
Set objEmail = CreateObject("CDO.Message")
objEmail.From = "pietje@pietje.nl"
objEmail.To = "klaasje@pietje.nl"
objEmail.Subject = "Check the directory because it is filling"
objEmail.Textbody = "Check the server"
objEmail.Send
End If


Hope someone can help

Patrick
 
Hello patrick118,

You can do something like this.
Code:
folderspec="d:\test"
bAction=false
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists(folderspec) Then
    Set objFolder = objFSO.GetFolder(folderspec)
    if objFolder.files.count>5 then bAction=true
End If

if bAction then
    Set objEmail = CreateObject("CDO.Message")
    objEmail.From = "pietje@pietje.nl"
    objEmail.To = "klaasje@pietje.nl"
    objEmail.Subject = "Check the directory because it is filling" 
    objEmail.Textbody = "Check the server"
    objEmail.Send
    set objEmail = nothing
end if
set objFOlder = nothing
set objFSO = nothing
The "action" being that if folder found and with files in it more than 5 then send an email, else do nothing. You may want some different criteria. Just for illustration.

regards - tsuji
 
It is working great but i now run into a nother problem.

I wanted the script to run every 10 minutes and thought i could do that with scheduler but i can't.

Do you have any idea?

Patrick
 
patrick118,

What scheduler? Are you using at? cdo which you are using would do better than outlook here. What is the exact problem? I'm off the day. May look at it tomorrow. Also, hope also some other memebers will step in in the meantime.

- tsuji
 
Just a sugestion but why not loop indefinetly and put a delay in



Do Until 1=0

::Your code here


wait-30000
Loop

Regards

Max


 
The loop thing is a great idea. Thanks.

Could it be possible to make is a little different because it isn't working as i thought.

We use the same script as above

folderspec="d:\test"
bAction=false
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists(folderspec) Then
Set objFolder = objFSO.GetFolder(folderspec)
if objFolder.files.count>5 then bAction=true
End If

if bAction then
Set objEmail = CreateObject("CDO.Message")
objEmail.From = "pietje@pietje.nl"
objEmail.To = "klaasje@pietje.nl"
objEmail.Subject = "Check the directory because it is filling"
objEmail.Textbody = "Check the server"
objEmail.Send
set objEmail = nothing
end if
set objFOlder = nothing
set objFSO = nothing

Here comes the difference i would like.

I want it to check the folder if there are any files in it. If no write 1 to a file.

Do this every 5 minutes.

If there are 5 times no files there should be 5 times 1 in the file. then send an email.

If there are files in the folder then delete the file.

It's a challange i know

Patrick
 
why do you need to write to a file? the 1's i mean?
 
if you are using the do loop then you should be able to do something like this

Set objFSO = CreateObject("Scripting.FileSystemObject")
folderspec="d:\test"
intA = 0
Do
If objFSO.FolderExists(folderspec) Then
Set objFolder = objFSO.GetFolder(folderspec)
If objFolder.Files.Count = O Then
intA = intA + 1
End If
If intA = 5 Then
Msgbox "5 times with no files do something"
intA = 0
End If
Set objFolder = Nothing
End If
Loop
 
Ok I'll tell you the entire story.

I would like to have a script that does the following.

Check a folder every 5 minutes. If there are 5 times in a row no files in there then send me an email. If there are files in there forget the last check and start counting from zero to five again.

Is that what i can do with the above script?

 
Set objFSO = CreateObject("Scripting.FileSystemObject")
folderspec="d:\test"
intA = 0
Do
If objFSO.FolderExists(folderspec) Then
Set objFolder = objFSO.GetFolder(folderspec)
If objFolder.Files.Count = O Then
intA = intA + 1
End If
If objFolder.Files.Count <> O Then
intA = 0
End If
If intA = 5 Then
Call sendEmail()
intA = 0
End If
Set objFolder = Nothing
End If
Wscript.Sleep 60000
Wscript.Sleep 60000
Wscript.Sleep 60000
Wscript.Sleep 60000
Wscript.Sleep 60000
Loop

Sub sendEmail()

Set objEmail = CreateObject("CDO.Message")
objEmail.From = "pietje@pietje.nl"
objEmail.To = "klaasje@pietje.nl"
objEmail.Subject = "Check the directory because it is filling"
objEmail.Textbody = "Check the server"
objEmail.Send
set objEmail = nothing

End Sub
 
perhaps this bit looks better

Set objFSO = CreateObject("Scripting.FileSystemObject")
folderspec="d:\test"
intA = 0
Do
If objFSO.FolderExists(folderspec) Then
Set objFolder = objFSO.GetFolder(folderspec)
If objFolder.Files.Count = O Then
intA = intA + 1
Else
Call sendEmail("folder has files in it")
intA = 0
If objFolder.Files.Count > 5 Then
Call sendEmail("folder has files in it, will try and delete")
For Each aFile In objFolder.Files
aFile.Delete
Next
End If
End If
If intA = 5 Then
Call sendEmail("5x with no files in it")
intA = 0
End If
Set objFolder = Nothing
End If
Wscript.Sleep 60000
Wscript.Sleep 60000
Wscript.Sleep 60000
Wscript.Sleep 60000
Wscript.Sleep 60000
Loop

Sub sendEmail(strMessage)

Set objEmail = CreateObject("CDO.Message")
objEmail.From = "pietje@pietje.nl"
objEmail.To = "klaasje@pietje.nl"
objEmail.Subject = "directory"
objEmail.Textbody = strMessage
objEmail.Send
set objEmail = nothing

End Sub
 
Thank you so much. I'll be testing this and i really wanna thank you for your help

Patrick
 
patrick118,

If you are on xp or up, you can consider using schtasks.exe to set it up running every x minutes.

- tsuji
 
I'm still working on the script but a few tthings are wrong i think

- it shouldn't delete the files

- it keeps counting but if there files in it it should start at 0 again.

I hope to post another version later in the day.

Patrick
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top