Skip to content

Instantly share code, notes, and snippets.

@ibrezm1
Created July 7, 2025 00:08
Show Gist options
  • Save ibrezm1/2160b290baf62e3e9109808d7115f437 to your computer and use it in GitHub Desktop.
Save ibrezm1/2160b290baf62e3e9109808d7115f437 to your computer and use it in GitHub Desktop.
; This script finds the last modified folder matching "RunXXX" and triggers a PowerShell script within it
; Define the parent directory where the RunXXX folders are located
Local $sParentDir = "C:\Your\Parent\Directory" ; Replace with your actual parent directory
; Define the pattern for the folders
Local $sFolderPattern = "Run*"
; Define the name of the PowerShell script to trigger
Local $sPsScript = "watchdog.ps1"
; --- Function to find the last modified folder based on pattern ---
Func GetLastModifiedFolder($sDir, $sPattern)
Local $aFolders, $iCount, $sLastModifiedFolder = ""
Local $iLastModifiedTime = 0
; Get a list of folders matching the pattern in the specified directory
$aFolders = _FileListToArray($sDir, $sPattern, 2) ; 2 for folders only
If IsArray($aFolders) Then
For $i = 1 To $aFolders[0]
Local $sFolderPath = $sDir & "\" & $aFolders[$i]
Local $iModifiedTime = FileGetTime($sFolderPath, 0, 1) ; 0 for created date, 1 for UTC time
If $iModifiedTime > $iLastModifiedTime Then
$iLastModifiedTime = $iModifiedTime
$sLastModifiedFolder = $sFolderPath
EndIf
Next
EndIf
Return $sLastModifiedFolder
EndFunc
; --- Main script execution ---
; Find the last modified folder
Local $sTargetFolder = GetLastModifiedFolder($sParentDir, $sFolderPattern)
If $sTargetFolder <> "" Then
; Construct the full path to the PowerShell script
Local $sPsScriptPath = $sTargetFolder & "\" & $sPsScript
; Check if the PowerShell script exists in the target folder
If FileExists($sPsScriptPath) Then
; Build the command to run the PowerShell script
Local $sCommand = 'powershell.exe -noexit -ExecutionPolicy Bypass -File "' & $sPsScriptPath & '"'
; Run the PowerShell script
Run($sCommand)
MsgBox(64, "Success", "PowerShell script triggered in the last modified folder: " & $sTargetFolder)
Else
MsgBox(16, "Error", "PowerShell script not found in the last modified folder: " & $sTargetFolder)
EndIf
Else
MsgBox(16, "Error", "No folder matching the pattern '" & $sFolderPattern & "' found in the specified directory.")
EndIf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment