2012-09-10 37 views
1

我的工作站上有一個文件夾,每分鐘添加文件。 我必須時不時監視此文件夾,以查看是否正在添加新文件。 如果這個文件夾中沒有新文件,例如5分鐘,我們執行一個動作。使用批處理文件監視文件夾的上次修改時間

我們可以使用批處理文件來達到這個目的嗎?如果最近5分鐘沒有添加新文件,窗口屏幕上會顯示警告/彈出窗口。 此外,我新批。請讓我知道的步驟

回答

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塊內的代碼以完成您需要完成的任務(通知您該問題)。

相關問題