Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 了解批处理文件在多少父进程下运行_Windows_Batch File_Cmd_Process_Pid - Fatal编程技术网

Windows 了解批处理文件在多少父进程下运行

Windows 了解批处理文件在多少父进程下运行,windows,batch-file,cmd,process,pid,Windows,Batch File,Cmd,Process,Pid,我有一个批处理文件,它在多个CMD会话中调用自己,例如: @echo off if "%var%"=="set" goto :begin set var=set call cmd /c %0 :begin echo Inside CMD session! Executed under x processes pause 我试图完成的是获取从中调用批处理文件的进程数 正常批处理文件: | Batch-file | Batch script | Batch-file | CMD

我有一个批处理文件,它在多个
CMD
会话中调用自己,例如:

@echo off
if "%var%"=="set" goto :begin
set var=set
call cmd /c %0

:begin
echo Inside CMD session! Executed under x processes
pause
我试图完成的是获取从中调用批处理文件的进程数

正常批处理文件:

| Batch-file
   | Batch script
| Batch-file
   | CMD
      | Batch-file
         | Batch script
当从根进程调用批处理文件时,将返回
1

多进程批处理文件:

| Batch-file
   | Batch script
| Batch-file
   | CMD
      | Batch-file
         | Batch script
当从另一个批处理文件调用批处理文件时,将返回
2

一个可能的解决方案是获取根进程标识符编号(PID),然后分析该进程的等待链,我可以在Task Manager中轻松完成这一点:

总结: 如何返回批处理文件正在执行的进程数,有无任何第三方实用程序?

@echo off
setlocal

if "%var%"=="3" goto :begin
set /A var+=1
cmd /C "%~F0"
goto :EOF

:begin
echo Inside CMD session!

wmic process where "name='cmd.exe'" get ExecutablePath,ParentProcessId,ProcessId > wmic.txt
for /F "skip=1 tokens=1-3" %%a in ('type wmic.txt') do (
   ECHO %%a - Parent: %%b - PID: %%c
   set "Parent[%%c]=%%b"
   set "This=%%c"
)

set X=0
:nextParent
   set /A X+=1
   call set "This=%%Parent[%This%]%%"
if defined Parent[%This%] goto nextParent
echo Executed under %X% processes
输出示例:

Inside CMD session!
C:\Windows\system32\cmd.exe - Parent: 3792 - PID: 4416
C:\Windows\system32\cmd.exe - Parent: 4416 - PID: 3220
C:\Windows\system32\cmd.exe - Parent: 3220 - PID: 1728
C:\Windows\system32\cmd.exe - Parent: 1728 - PID: 3560
Executed under 4 processes
输出示例:

Inside CMD session!
C:\Windows\system32\cmd.exe - Parent: 3792 - PID: 4416
C:\Windows\system32\cmd.exe - Parent: 4416 - PID: 3220
C:\Windows\system32\cmd.exe - Parent: 3220 - PID: 1728
C:\Windows\system32\cmd.exe - Parent: 1728 - PID: 3560
Executed under 4 processes

我涉猎了Aacini的代码以使树更直观

Inside CMD session
C:\WINDOWS\system32\cmd.exe - 1004:12424
C:\WINDOWS\system32\cmd.exe -      12424:12016
C:\WINDOWS\system32\cmd.exe -           12016:10592
C:\WINDOWS\system32\cmd.exe -                10592:11392
C:\WINDOWS\system32\cmd.exe -                     11392:5616
Executed under 5 processes
改变了中间部分

Set "Space="
wmic process get ExecutablePath,ParentProcessId,ProcessId > wmic.txt
for /F "tokens=1-3" %%a in ('type wmic.txt') do (
   if /I "%%a" equ "%ComSpec%" (
      Call ECHO %%a - %%Space%%%%b:%%c
      CAll Set "Space=%%Space%%     "
      set "Parent[%%c]=%%b"
      set "This=%%c"
   )
)

我涉猎了Aacini的代码以使树更直观

Inside CMD session
C:\WINDOWS\system32\cmd.exe - 1004:12424
C:\WINDOWS\system32\cmd.exe -      12424:12016
C:\WINDOWS\system32\cmd.exe -           12016:10592
C:\WINDOWS\system32\cmd.exe -                10592:11392
C:\WINDOWS\system32\cmd.exe -                     11392:5616
Executed under 5 processes
改变了中间部分

Set "Space="
wmic process get ExecutablePath,ParentProcessId,ProcessId > wmic.txt
for /F "tokens=1-3" %%a in ('type wmic.txt') do (
   if /I "%%a" equ "%ComSpec%" (
      Call ECHO %%a - %%Space%%%%b:%%c
      CAll Set "Space=%%Space%%     "
      set "Parent[%%c]=%%b"
      set "This=%%c"
   )
)