如果进程在其路径中具有特定的文件夹名称,如何终止进程?

时间:2018-04-17 14:02:55

标签: process path directory powerbuilder terminate

我从PowerBuilder运行应用程序并在我不需要它时终止它。但是应用程序在运行时会执行特定文件夹中的其他进程。

如果从特定文件夹运行,我想杀死所有进程。

如何获取路径中具有特定文件夹名称的所有进程的进程句柄?

2 个答案:

答案 0 :(得分:0)

这是一个示例,说明如何终止所有正在运行的Notepad.exe实例

OleObject wsh
integer  li_rc

wsh = CREATE OleObject
wsh.ConnectToNewObject( "MSScriptControl.ScriptControl" )
wsh.language = "vbscript"
wsh.AddCode('function terminatenotepad() ~n ' + &
 'strComputer = "." ~n ' + &
 'Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") ~n ' + &
 'Set colItems = objWMIService.ExecQuery("Select * from Win32_Process where name = ~'notepad.exe~'") ~n ' + &
 'For Each objItem in colItems ~n ' + &
 '     objItem.Terminate ~n ' + &
 'Next ~n ' + &
 'end function ~n ' )
wsh.executestatement('terminatenotepad()')
wsh.DisconnectObject()
DESTROY wsh

尝试更改where子句

where name = ~'notepad.exe~'" ...

代表

where commandline = ~'" + ls_commandline + "~'"  ...

其中ls_commandline是用于启动进程的命令行。

有关更多信息,请参阅"Win32_Process class"

答案 1 :(得分:0)

/// This is simple code example Terminate a Running Process if here is specific
/// folder name in the path of the running program (\EagleGet\)
/// Two variable SelectThis and TheWhere can be changed to get different 
/// results. Many things are not  dynamic in this script and certainly not the
/// best way of writing code. but its just ok for a little quick work.`

OleObject wsh
Integer  li_rc

String LineFeed = ' ~r~n ' 
String TheComputer = "." //local computer
String TheCode = "no need to set it here"
String FunctionName = "Whatever()" //any name you like
String SelectThis = "?" //only the columns/expressions
String TheWhere = "?" //only the where clause without keyword WHERE
String DoWhat = "?" //the action to perform for example 'Terminate' without quotes
String TheQuery = "no need to set here"
String WMIClass = "Win32_Process" /// The WMI class of running processes
String TheFolderName = "The folder name from which the creapy process is running (path) " 

/// You just set DoWhat, SelectThis and TheWhere. Rest of the variables, you dont need to set here
/// SelectThis = is the columns or expressions you want returned by the Query
SelectThis = "*"

/// TheFolderName = set it to the name of the folder that exist in the path 
///of the ruuning process you want to terminate
TheFolderName = "EagleGet"                                      

/// TheWhere is the WHERE clause expressions
TheWhere = "ExecutablePath  LIKE ~'%\\" + TheFolderName + "\\%~' "

/// DoWhat is the final function call of the WMI Class
DoWhat = "Terminate"

/// There is no need to chage anything from this point onward.
/// without double quotes, and you dont have to change TheQuery here 
TheQuery = " SELECT " + SelectThis + " FROM " + WMIClass + " WHERE " + TheWhere

TheCode = "Function " + FunctionName + LineFeed + &
"strComputer = ~"" + TheComputer  + "~"" + LineFeed + &
"Set objWMIService = GetObject(~"winmgmts:\\~" & strComputer & ~"\root\cimv2~")" + LineFeed + &
"Set colItems = objWMIService.ExecQuery(~"" + Trim(TheQuery) + "~" )" + LineFeed + &
   "For Each objItem in colItems" + LineFeed + &
      "objItem." + Trim(DoWhat) + LineFeed + &
   "Next " + LineFeed + &
"END Function " + LineFeed 

wsh = CREATE OleObject
wsh.ConnectToNewObject("MSScriptControl.ScriptControl")
wsh.Language = "VBScript"
wsh.AddCode(TheCode)
TRY
    wsh.ExecuteStatement(FunctionName)
CATCH (RunTimeError Re01)
    MessageBox("Query Error", "Following code has some problems.~r~n~r~n" + TheCode, StopSign!)
END TRY
wsh.DisconnectObject()

DESTROY wsh