我认为我原来的问题过于宽泛。在思考了上周如何实现我的目标后,我相信我需要实现一个多维动态数组并使用redim preserve来保存数据。 我的第一个数组是“strProgramList”,它是包含程序名称的完整路径。我拆分每条路径并得到它的上限以获得程序的名称。我现在需要将它组合成一个具有两列的单维多维数组,一列具有完整路径,另一列具有程序名称。我知道你只能在改变最后一个维度时保留,但我无法弄清楚如何绕过这个限制。我已经尝试了很多方法,我很难过。我已经更新了代码,但它不起作用。我想我需要一个嵌套for else循环,我已经尝试但无法工作。
'Option Explicit
' ---------------------------------------------------------------------------------
'---Declare Globals Variables
Dim objProcess, objWMIService, colProcesses, Process, strComputer
'---Declare Program Variables
Dim strProgram1, strProgram2, strProgram3, strProgramList, strProgramPath, strPathComponents, strProgramName
ReDim strProgramNameList(1, -1)
Dim strProgram, pid, objShell, strCommand, i, program1StartTime, boolNeedsRestart, currentProgramStartTime, removalIndex, x, objFso, strProgramRestartList
Set objFso = CreateObject("Scripting.FileSystemObject")
'---Create Program Variables
strProgram1 = "%SystemRoot%\notepad.exe"
strProgram2 = "C:\Program Files\Microsoft Office\root\Office16\OUTLOOK.EXE"
strProgram3 = "C:\Program Files\Microsoft Office\root\Office16\ONENOTE.EXE"
'---Add Program Path Variables to an Array
strProgramList = Array(strProgram1,strProgram2,strProgram3)
'strProgramNameList = Array()
strProgramRestartList = Array()
boolNeedsRestart = false
'---Iterating using For each loop to get program name.
For Each strProgramPath In strProgramList
strPathComponents = Split(strProgramPath, "\")
strProgramName = strPathComponents(Ubound(strPathComponents))
ReDim Preserve strProgramNameList(1, UBound(strProgramNameList) + 1)
ReDim Preserve strProgramNameList(2, UBound(strProgramNameList) + 1)
strProgramNameList(1, UBound(strProgramNameList)) = strProgramName
strProgramNameList(2, UBound(strProgramNameList)) = strProgramPath
Next
msgbox Ubound(strProgramNameList,5)
'---Program launching logic.
For i = 1 To uBound(strProgramNameList)
If isrunning(strProgramNameList(i)) Then
If isrunning(strProgramNameList(0)) then
program1StartTime = getProcessStartTime(strProgramNameList(0))
currentProgramStartTime = getProcessStartTime(strProgramNameList(i))
If WMIDateStringToDate(program1StartTime) > WMIDateStringToDate(currentProgramStartTime) Then
boolNeedsRestart = true
ReDim Preserve strProgramRestartList(UBound(strProgramRestartList) + 1)
strProgramRestartList(UBound(strProgramRestartList)) = strProgramNameList(i)
Else
boolNeedsRestart = false
End If
Else
boolNeedsRestart = true
ReDim Preserve strProgramRestartList(UBound(strProgramRestartList) + 1)
strProgramRestartList(UBound(strProgramRestartList)) = strProgramNameList(i)
End if
Else
If objFso.FileExists(strProgramList(i)) Then
boolNeedsRestart = true
ReDim Preserve strProgramRestartList(UBound(strProgramRestartList) + 1)
strProgramRestartList(UBound(strProgramRestartList)) = strProgramNameList(i)
'Else
'MsgBox(strProgramList(i))
End If
End If
Next
If boolNeedsRestart = true Then
killprocess(strProgramNameList(0))
For i = 0 To uBound(strProgramRestartList)
killprocess(strProgramRestartList(i))
Next
restartprocess strProgramNameList(0)
For i = 0 To uBound(strProgramRestartList)
restartprocess strProgramRestartList(i)
Next
End If
Sub RestartProcess(strProgramPath)
Set objShell = WScript.CreateObject("WScript.Shell")
strCommand = chr(34)& strProgramPath &chr(34)
objShell.Run strCommand,1,False
pid = WaitForProcess(strProgramPath, 5)
If pid > 0 Then
objShell.AppActivate pid
End If
Set objShell = Nothing
End Sub
' ---------------------------------------------------------------------------------
Function KillProcess(strProcessKill)
Dim aProcessRunning
aProcessRunning = false
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer& "\root\cimv2")
Set colProcesses = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name='" & strProcessKill & "'")
For Each objProcess in colProcesses
If LCase( strProcessKill )= LCase( objProcess.Name ) Then
objProcess.Terminate()
aProcessRunning = True
End If
Next
If aProcessRunning Then
' Wait and make sure the process is terminated.
Do Until Not aProcessRunning
Set colProcesses = GetObject( _
"winmgmts:{impersonationLevel=impersonate}" _
).ExecQuery( "Select * From Win32_Process Where Name = '" _
& strProcessKill & "'" )
WScript.Sleep 100 'Wait for 100 MilliSeconds
If colProcesses.Count = 0 Then 'If no more processes are running, exit loop
aProcessRunning = False
End If
Loop
End If
Set colProcesses = Nothing
Set objWMIService = Nothing
KillProcess = aProcessRunning
End Function
' ---------------------------------------------------------------------------------
Function IsRunning(strProgram)
Dim bProcessRunning
bProcessRunning = false
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer& "\root\cimv2")
Set colProcesses = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name='" & strProgram & "'")
For Each objProcess in colProcesses
If objProcess.Name = strProgram Then
bProcessRunning = True
End If
Next
Set colProcesses = Nothing
Set objWMIService = Nothing
IsRunning = bProcessRunning
End Function
' ---------------------------------------------------------------------------------
Function WaitForProcess(imageName, tries)
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer& "\root\cimv2")
Set colProcesses = objWMIService.ExecQuery("SELECT ProcessId FROM Win32_Process WHERE Name='" & imageName & "'")
WaitForProcess = 0
While tries > 0 And WaitForProcess = 0
For Each process In colProcesses
WaitForProcess = process.ProcessId
Next
If WaitForProcess = 0 Then
WScript.Sleep 1000
tries = tries - 1
End If
'MsgBox(imageName)
Wend
Set colProcesses = Nothing
Set objWMIService = Nothing
End Function
' ---------------------------------------------------------------------------------
Function getProcessStartTime(programName)
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer& "\root\cimv2")
Set colProcesses = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name='" & programName & "'")
For Each objProcess in colProcesses
getProcessStartTime = objProcess.CreationDate
Next
Set colProcesses = Nothing
Set objWMIService = Nothing
End Function
' ---------------------------------------------------------------------------------
Function WMIDateStringToDate(dtmStart)
WMIDateStringToDate = CDate(Mid(dtmStart, 5, 2) & "/" & _
Mid(dtmStart, 7, 2) & "/" & Left(dtmStart, 4) _
& " " & Mid (dtmStart, 9, 2) & ":" & _
Mid(dtmStart, 11, 2) & ":" & Mid(dtmStart, _
13, 2))
End Function