获取当前CMD 的 PID By Yu2n
注意,当一个BAT文件被同时多次开启时,可能撞车……- @echo off
- :: @set "DebugOff= ' "
- @call :_CmdPid "%~0"
- :: ↑ 此行不能写在标签内,建议写在脚本开头
- echo CMD PID: %CmdPid%
- pause
-
- goto :eof
- :_CmdPid
- (echo For Each objItem In GetObject^("winmgmts:\\.\root\cimv2:win32_process"^).Instances_
- echo If LCase^(objItem.Name^)=LCase^("cmd.exe"^) And InStr^(1,objItem.CommandLine,"%~1",vbTextCompare^)^>0 Then _
- echo CurPID=objItem.ProcessID %DebugOff%: WScript.Echo CurPID,objItem.Name,objItem.CommandLine
- echo Next
- echo WScript.Quit^(CurPID^))>"%tmp%\CmdPid.vbs"
- CScript //NoLogo "%tmp%\CmdPid.vbs"
- set CmdPid=%ErrorLevel%
- del /q "%tmp%\CmdPid.vbs"
- goto :eof
复制代码
|