Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Prints NEW, DELETED and CHAGED files in a directory. Works on Windows WSH.
'
' Prints NEW, DELETED and CHAGED files in a directory.
'
' This script does not work with directorys have name include SPACE charactors.
'
' by HigeDice http://d.hatena.ne.jp/higedice/
'
Option Explicit
Const INTERVAL = "10" ' seconds
Const DEBUG_LEVEL = 0
' *** 入力エラーチェック ***
Dim strScriptHost
strScriptHost = LCase(Wscript.FullName)
If Right(strScriptHost, 11) <> "cscript.exe" Then
Call printErrorMsg("コマンドプロンプトから CScript.exe で実行してください。")
WScript.Quit 1
End If
Dim objWshShell
Set objWshShell = WScript.CreateObject("WScript.Shell")
Dim objWshUnnamed
Set objWshUnnamed = WScript.Arguments.Unnamed
If objWshUnnamed.Count <> 1 Then
Call printErrorMsg("")
Set objWshUnnamed = Nothing
WScript.Quit 2
End If
If Not checkTargetDir(objWshUnnamed.Item(0)) Then
Call printErrorMsg("指定のフォルダが存在しません。")
Set objWshUnnamed = Nothing
WScript.Quit 3
End If
' *** 実処理 ***
Dim strComputer
strComputer = getCompterNameFromPath(objWshUnnamed.Item(0))
'strComputer = "."
Dim targetDir
targetDir = Replace(objWshUnnamed.Item(0), ":\", ":\\")
targetDir = Replace(targetDir, "\", "\\")
If strComputer <> "." Then
targetDir = Replace(targetDir, "\\\\" & strComputer, "")
targetDir = "\\" & targetDir
End If
printDebug(targetDir)
Set objWshUnnamed = Nothing
Dim objWMIService
Dim colMonitoredEvents
Dim objLatestEvent
Dim targetFileName
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\cimv2")
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
("SELECT * FROM __InstanceOperationEvent WITHIN " & INTERVAL & " WHERE " _
& "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _
& "TargetInstance.GroupComponent= " _
& "'Win32_Directory.Name=""" & targetDir & """'")
Do
Set objLatestEvent = colMonitoredEvents.NextEvent ' WMIイベント待ち状態
targetFileName = formatPath(objLatestEvent.TargetInstance.PartComponent)
Select Case objLatestEvent.Path_.Class
Case "__InstanceCreationEvent"
createExec(targetFileName)
Case "__InstanceDeletionEvent"
delExec(targetFileName)
Case "__InstanceModificationEvent"
modExec(targetFileName)
End Select
Loop
Set objWshShell = Nothing
printDebug("END")
Function createExec(fileName)
Wscript.Echo (now & vbTab & "作成" & vbTab & fileName)
' objWshShell.Run "notepad.exe /p " & FileName , 4
End Function
Function delExec(fileName)
Wscript.Echo (now & vbTab & "削除" & vbTab & fileName)
End Function
Function modExec(fileName)
Wscript.Echo (now & vbTab & "変更" & vbTab & fileName)
End Function
Function formatPath (PathComponent)
Dim strNewFolder, arrNewFolder
arrNewFolder = Split(PathComponent, "=")
strNewFolder = arrNewFolder(1)
strNewFolder = Replace(strNewFolder, "\\", "\")
strNewFolder = Replace(strNewFolder, Chr(34), "")
formatPath = strNewFolder
End Function
Function printErrorMsg (str)
WScript.Echo str & vbNewLine _
& vbNewLine _
& "操作例:" & vbNewLine _
& "> cscript " & WScript.ScriptName & " [対象ディレクトリ]"
End Function
Sub printDebug(str)
If DEBUG_LEVEL > 0 Then
WScript.Echo str
End If
End Sub
Function checkTargetDir (path)
Dim objFSO
Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists(path) Then
checkTargetDir = True
Else
checkTargetDir = False
End If
End Function
Function getCompterNameFromPath(path)
Dim re, matches
Set re = New RegExp
re.Pattern = "^\\\\([^\\]+)"
Set matches = re.Execute(path)
If matches.Count > 0 Then
getCompterNameFromPath = matches.Item(0).SubMatches.Item(0)
Else
getCompterNameFromPath = "."
End If
printDebug "getCompterNameFromPath = " & getCompterNameFromPath
Set re = Nothing
Set matches = Nothing
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment