简体   繁体   中英

monitor last modified time for a folder using batch file

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

It seems unlikely that you are going to accomplish what you want to do with purely a batch file.

However, you can do this in a relatively simple/small VB Script, that doesn't require any additional installation on the system.

'-------------------------------------------------------------
' 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

Execute this in WScript to make it not visible or CScript to show the console window.

Replace the code inside the IF block to do what you need done (notify you of the issue).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM