使用應用程式時每 x 分鐘運行 .cmd 檔案?

使用應用程式時每 x 分鐘運行 .cmd 檔案?

我正在嘗試找到一種每 15 分鐘增量備份一個資料夾的方法,但前提是自上次備份以來 VS Code 已被使用/處於焦點狀態。

有誰知道我該如何做到這一點?

答案1

在 AutoHotkey 中相對容易做到...如果您以前沒有使用過它,您可能還想獲取具有語法突出顯示功能的 SciTE4AutoHotkey。

以下是執行類似操作的粗略輪廓...您必須偵錯程式碼才能使視窗標題正常運作(使用托盤圖示中的 Window Spy 功能來取得適用於 VS Code 的適當 WinTitle,並參閱協助說明WinTitle)。您還必須調試運行語句以使其運行備份...您可以直接運行備份或作為批次文件運行,有時讓批次文件工作更容易。

; Global variable to store last active window time
Global gLastActive 

; This will call the Backup() function periodically..
SetTimer, Backup, % 15*60*1000 ; 15 minutes, in milliseconds

; This is the main loop, just sets a global if the window is active at any point
Loop {
    If WinActive("VS Code") {           ; this will need to be a valid WinTitle
        gLastActive := A_TickCount
        ; MsgBox % "Window detected..."     ; debug message for getting WinTitle correct
    }
    
    Sleep 1000
}

ExitApp         ; Denote logical end of program, will never execute


; Backup function will run periodically and only back things up
;    if VS Code was active since last time...
Backup() {
    Static lastTick
    
    ; If the window was active after the last backup, run a backup this time
    If (gLastActive>lastTick)
        Run, C:\Target.cmd, C:\WorkingDir, Hide ; this will need to be corrected for syntax
    
    lastTick := A_TickCount
}

筆記:這是完全未經測試的程式碼,只是為您提供了一個框架範例,供您嘗試開始。

相關內容