1

我的工作站上有一个文件夹,每分钟都会添加文件。我必须时不时地监视这个文件夹,看看是否有新文件被添加。如果 5 分钟内此文件夹中没有新文件,我们将执行一项操作。

我们是否可以为此目的使用批处理文件,如果在过去 5 分钟内没有添加新文件,则会在窗口屏幕上显示警报/弹出窗口。我也是批处理的新手。请让我知道步骤

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 回答