簡體   English   中英

使用批處理文件監視文件夾的上次修改時間

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM