1

I have a folder on my workstation where files are added every minute. I have to monitor this folder every now and then, to see if new files are being added. In case there is no new file in this folder for say 5 min, we perform an action.

Can we use batch file for this purpose in such a way that if there is no new file added for last 5 min, an alert /pop up apears on window screen. Also I m new to Batch .Please let me know the steps

4

1 に答える 1

0

やりたいことを純粋にバッチ ファイルで達成できる可能性は低いと思われます。

ただし、これは、システムに追加のインストールを必要としない、比較的単純な/小さな VB スクリプトで行うことができます。

'-------------------------------------------------------------
' Monitors a folder for new files and warns if they 
' are not being created within a certain time period.
'-------------------------------------------------------------
Dim intMinutes:      intMinutes = 5            ' minute threshold for warning of no new files
Dim strDrive:        strDrive = "c:"           ' drive to monitor
Dim strPath:         strPath = "\\temp\\"      ' path to monitor. remember to double slashes

Dim intTimer:        intTimer = "2"
Dim strComputer:     strComputer = "."
Dim objWMIService:   Set objWMIService = GetObject( "winmgmts:" &  "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2" )
Dim strQuery:        strQuery = "Select * From __InstanceOperationEvent" & " Within " & intTimer & " Where Targetinstance Isa 'CIM_DataFile'" & " And TargetInstance.Drive='" & strDrive & "'" & " And TargetInstance.Path='" & strPath & "'"
Dim colEvents:       Set colEvents = objWMIService. ExecNotificationQuery (strQuery)
Dim LastNew:         LastNew = Now
WScript.Echo "Monitoring new file creation... Press [Ctrl]-[C] to exit"
Do
    Set objEvent = colEvents.NextEvent()
    Set objTargetInst = objEvent.TargetInstance
    Select Case objEvent.Path_.Class
        Case "__InstanceCreationEvent"
            LastNew = Now
    End Select
    if DateDiff("n",LastNew,Now) >= intMinutes then 
        ' put whatever actions you want in here... the following two lines are for demo purposes only
        msgbox "The last new file was " & DateDiff("n",LastNew,Now) & " minute ago!",0,"No New Files"
        exit do
    end if
Loop

これをWScriptで実行して非表示にするか、CScriptでコンソール ウィンドウを表示します。

IF ブロック内のコードを置き換えて、必要なことを実行します (問題を通知します)。

于 2012-10-14T01:02:06.480 に答える