[英]monitor last modified time for a folder using batch file
我的工作站上有一个文件夹,每分钟都会添加文件。 我必须不时监视此文件夹,以查看是否正在添加新文件。 如果该文件夹中有5分钟没有新文件,我们将执行一个操作。
我们是否可以以这种方式使用批处理文件,如果最近5分钟未添加任何新文件,则会在窗口屏幕上弹出警报/弹出提示。 我也是Batch的新手。请让我知道步骤
您似乎不可能仅使用批处理文件来完成您想做的事情。
但是,您可以使用相对简单/较小的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块中的代码以完成所需的操作(将问题通知您)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.