Jump to content

File creation monitoring

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
12 replies to this topic

#1
Spencer Elliott

Spencer Elliott

    Newbie

  • Members
  • PipPip
  • 24 posts
I am trying to write some code that monitors a folder, and all of it's subfolders and subfolders within subfolders with subfodlers etc etc etc.
I want to monitor this folder for file creation, and on file creation send myself an e-mail.
However, I only want to have it email me and tell me about the file if it's of a specific file extension. i.e only email me if the file extension of the newly created file is .doc.

I have successfully created code that gives a little pop-up when a file is created in a folder, then moved on to make it email me. However, I cannot seem to go on to the next steps of making it watch for file creation in all subfolders. I am certain I can add the code myself for checking the file extension...

Here is my code so far. Can anyone spare some time to help me please?


sComp = "."  


sFolder="c:\\\\test"  


Set oWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & sComp & "\root\cimv2")  






Dim Queue

Queue = sFolder & ";"


     While Queue <> ""

            Dim Path

			Path = Split(Queue,";")(0)

			

			

			Queue = Right(Queue,Len(Queue) - Len(Path)-1)



     Dim oFSO, oFolder, oFiles, oFile, OSubFolders

        set oFSO = CreateObject("Scripting.FileSystemObject")

        set oFolder = oFSO.GetFolder(Path)

        set oFiles = oFolder.Files

		set oSubFolders = oFolder.SubFolders

		For Each oSubFolder in OSubFolders



Set oEvents = oWMI.ExecNotificationQuery("SELECT * FROM __InstanceCreationEvent WITHIN 2 WHERE " & "Targetinstance ISA 'CIM_DirectoryContainsFile' AND " & "TargetInstance.GroupComponent= " & "'Win32_Directory.Name=""" & oSubFolder & """'")  

  

Do  

  

Set oEvent = oEvents.NextEvent  

  

sNewFileName=oEvent.TargetInstance.PartComponent  

  

Wscript.Echo sNewFileName 


Set objMessage = CreateObject("CDO.Message") 


objMessage.Configuration.Fields.Item _

("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 


'Name or IP of Remote SMTP Server

objMessage.Configuration.Fields.Item _

("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "removed"


'Type of authentication, NONE, Basic (Base64 encoded), NTLM

objMessage.Configuration.Fields.Item _

("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = removed

'Server port (typically 25)

objMessage.Configuration.Fields.Item _

("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 


'Connection Timeout in seconds (the maximum time CDO will try to establish a connection to the SMTP server)

objMessage.Configuration.Fields.Item _

("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60


objMessage.Configuration.Fields.Update



objMessage.Subject = "Unwanted file created" 

objMessage.From = "removed@removed.removed.removed.removed" 

objMessage.To = "removed@removed.com" 

objMessage.TextBody = "The following file has been created: " &  sNewFileName

'objMessage.Send  

  

Loop  




		Next


Wend




#2
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
I'll try :D

#3
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
I get an error on line 30:

Set oEvents = oWMI.ExecNotificationQuery("SELECT * FROM __InstanceCreationEvent WITHIN 2 WHERE " & "Targetinstance ISA 'CIM_DirectoryContainsFile' AND " & "TargetInstance.GroupComponent= " & "'Win32_Directory.Name=""" & oSubFolder & """'") 


The error message is: Unparsable query.

Do you also get it?

#4
Spencer Elliott

Spencer Elliott

    Newbie

  • Members
  • PipPip
  • 24 posts
No error for me o.0

I'm not sure if it's even possible really to get the code to do what I want it to. Although it would be very nice to get working.

I really appreciate you trying to help me (again) man, thanks allot! :-D

#5
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
What you want is it to check the subfolders? :confused:

#6
Spencer Elliott

Spencer Elliott

    Newbie

  • Members
  • PipPip
  • 24 posts

Vswe said:

What you want is it to check the subfolders? :confused:

I want to be able to watch a folder for when a file is created. Then email myself a notification. I am certain I can write the e-mail support, and even the support for just the specific type of file I want to watch for.
I can do this for a folder, but at the moment there is a loop-hole. Poeple can create a folder in the folder I am watching, and add the file to there newly created folder. :-(

#7
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
Can't you do like you did with the last problem you had?

#8
Spencer Elliott

Spencer Elliott

    Newbie

  • Members
  • PipPip
  • 24 posts
I have tried, but it seems I am not quite there with my source that I posted. I'm just really not used to vbscript at all. :-(

#9
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
Add this :

Queue = Queue & oSubFolder & ";"

after this line:

For Each oSubFolder in OSubFolders


#10
Spencer Elliott

Spencer Elliott

    Newbie

  • Members
  • PipPip
  • 24 posts

Vswe said:

I get an error on line 30:

Set oEvents = oWMI.ExecNotificationQuery("SELECT * FROM __InstanceCreationEvent WITHIN 2 WHERE " & "Targetinstance ISA 'CIM_DirectoryContainsFile' AND " & "TargetInstance.GroupComponent= " & "'Win32_Directory.Name=""" & oSubFolder & """'") 


The error message is: Unparsable query.

Do you also get it?


I am now getting this message. I cannot remember getting it before, but then when I comment out the line you just said to add, I still get the message. Bummer! :-(

Thankyou for giving your time to help again fella.

#11
Spencer Elliott

Spencer Elliott

    Newbie

  • Members
  • PipPip
  • 24 posts
If anyone can help, I'll subscribe to your life long fan club. :-)

#12
adamslader

adamslader

    Newbie

  • Members
  • Pip
  • 1 posts
Been working on this myself a bit recently, so thought I'd sign up and help out.
The problem with the WMI query on line 30 is it needs '\\\\' instead of '\', so you need to run oSubFolder through a replace function - I found someone at
nntpnews . net / f3893 / server2003-possible-8529102 who was doing this so have appropriated from them, as it also puts the strange end quote marks in. Code
fragment is as follows (add the 'DQ =' and 'oSubFolder =' lines in, change the 'Set oEvents =' line, above and below that are for reference):

For Each oSubFolder in OSubFolders

DQ = """"

oSubFolder = "'Win32_Directory.Name=" & DQ & Replace(oSubFolder , "\", "\\\\") & DQ & "'"

Set oEvents = oWMI.ExecNotificationQuery("SELECT * FROM __InstanceCreationEvent WITHIN 2 WHERE " & "Targetinstance ISA 'CIM_DirectoryContainsFile' AND " &
"TargetInstance.GroupComponent= " & oSubFolder)

Do

Set oEvent = oEvents.NextEvent