1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
| ' ----------------------------------------------------------
' Script de surveillance de création ou suppression de
' fichiers dans un répertoire
'
' JC BELLAMY © 2003
' ----------------------------------------------------------
Dim args, fso, dir
Dim listefic1(),listefic2(),nf(2)
Set fso = WScript.CreateObject("Scripting.FileSystemObject")
Set shell = WScript.CreateObject("WScript.Shell")
Set args = Wscript.Arguments
Const SW_SHOWNORMAL=1
If args.count=0 then
msg= "----------------------------------------------------------------------" & VBCRLF
msg=msg & "Surveillance de création ou suppression de fichiers dans un répertoire" & VBCRLF
msg=msg & "JCB © 2003" & VBCRLF
msg=msg & "----------------------------------------------------------------------" & VBCRLF
msg=msg & "Syntaxe :" & VBCRLF
msg=msg & " watchdir <répertoire> [<période> [<application>]]" & VBCRLF
msg=msg & "Paramètres :" & VBCRLF
msg=msg & " <répertoire> : Nom du répertoire à surveiller" & VBCRLF
msg=msg & " NB: à encadrer par des guillemets" & VBCRLF
msg=msg & " si le nom contient des espaces" & VBCRLF
msg=msg & " <période> : (facultatif) Période de scrutation en secondes" & VBCRLF
msg=msg & " (1 seconde par défaut)" & VBCRLF
msg=msg & " <application> : (facultatif) Application à lancer en cas" & VBCRLF
msg=msg & " de création ou suppression de fichier." & VBCRLF
msg=msg & " Elle reçoit en paramètres :" & VBCRLF
msg=msg & " - un indicateur de création (""C"") ou de suppression (""S"")" & VBCRLF
msg=msg & " - le nom complet du fichier créé ou supprimé" & VBCRLF
msg=msg & " L'application peut être un exécutable ou un script" & VBCRLF
msg=msg & " (fichier batch .BAT ou .CMD ou script VBS,...)" & VBCRLF
wscript.echo msg
wscript.quit
end if
TestHost true
dir=args(0)
If not fso.FolderExists(dir) Then
wscript.echo "Répertoire " & dir & " inexistant"
wscript.quit
End If
If right(dir,1)<>"\" Then dir=dir & "\"
periode=1
If args.count>1 Then
periode=args(1)
If not IsNumeric(periode) Then
wscript.echo "Valeur incorrecte de période"
wscript.quit
End If
End If
appli=""
If args.count>2 Then appli=args(2)
wscript.echo "Surveillance du répertoire " & dir & " toutes les " & periode & " seconde(s)"
wscript.echo "Taper CTRL-C pour stopper la surveilance"
periode=periode*1000
exploredir 1
While true
wscript.sleep periode
exploredir 2
compare 1,2
compare 2,1
actualise
Wend
Wscript.quit
'--------------------------------------------------------------------
Sub ExploreDir(i)
set D=fso.GetFolder(dir)
set collfic=D.Files
nf(i)=collfic.count
execute("redim listefic" & i & "(" & nf(i) & ")")
j=0
For each F in collfic
j=j+1
cmd="listefic" & i & "(" & j & ")=""" & F.name & """"
execute(cmd)
Next
End Sub
'--------------------------------------------------------------------
Sub Compare(m,n)
For i = 1 To nf(m)
curname1=eval("listefic" & m & "(" & i & ")")
exist=false
j=0
Do
j=j+1
curname2=eval("listefic" & n & "(" & j & ")")
If curname1=curname2 Then exist=true
Loop Until exist or j=nf(n)
If not Exist Then
If m=1 Then
action=" : suppression"
flag="S"
else
action=" : création"
flag="C"
end if
wscript.echo Time & " " & curname1 & action
' Création de l'email à envoyer
Set objMessage = CreateObject("CDO.Message")
' objMessage.To = "c.valentin@acr-distribution.fr"
objMessage.To = "fabien.bagliniere@aznetwork.eu;system@acr-distribution.fr"
objMessage.From = "edi@aznetwork.eu"
objMessage.Subject = "Commande EDI" & action
objMessage.TextBody = Time & " " & curname1 & action
' Protocole d’envoi du mail ici 2 correspond au SMTP
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
' Nom du serveur SMTP
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "zimbra.aznetwork.eu"
'Your UserID on the SMTP server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusername") = "edi"
' Port serveur SMTP
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
'mot de passe
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "abc123ABC!"
'Type of authentication, NONE, Basic (Base64 encoded), NTLM
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = Basic
objMessage.Configuration.Fields.Update
On Error Resume Next
' Envoi du mail
objMessage.Send
If appli<>"" Then
shell.run """" & appli & """ " & flag & " """ & dir & curname1 & """",SW_SHOWNORMAL,false
End If
End If
Next
End Sub
'--------------------------------------------------------------------
Sub actualise
redim listefic1(nf(2))
nf(1)=nf(2)
For i = 1 To nf(1)
listefic1(i)=listefic2(i)
Next
End Sub
'--------------------------------------------------------------------
'Sous-programme de test du moteur
'Vu les sorties générées, c'est CSCRIPT (et non pas WSCRIPT)
'qui doit être utilisé de préférence
Sub TestHost(force)
dim rep
strappli=lcase(Wscript.ScriptFullName)
strFullName =lcase(WScript.FullName)
i=InStr(1,strFullName,".exe",1)
j=InStrRev(strFullName,"\",i,1)
strCommand=Mid(strFullName,j+1,i-j-1)
if strCommand<>"cscript" then
If force then
Init="Ce script doit être lancé avec CSCRIPT"
Else
Init="Il est préférable de lancer ce script avec CSCRIPT"
End If
rep=MsgBox(Init & VBCRLF & _
"Cela peut être rendu permanent avec la commande" & VBCRLF & _
"cscript //H:CScript //S /Nologo" & VBCRLF & _
"Voulez-vous que ce soit fait automatiquement?", _
vbYesNo + vbQuestion,strappli)
if rep=vbYes then
nomcmd="setscript.bat"
Set ficcmd = fso.CreateTextFile(nomcmd)
ficcmd.writeline "@echo off"
ficcmd.writeline "cscript //H:CScript //S /Nologo"
ficcmd.writeline "pause"
params=""
For i = 0 To nbargs-1
params=params & " " & args(i)
next
ficcmd.writeline chr(34) & strappli & chr(34) & params
ficcmd.writeline "pause"
ficcmd.close
shell.Run nomcmd, SW_SHOWNORMAL,true
force=true
end if
If force then WScript.Quit
end if
end sub |
Partager