批处理以获取 运行 所有进程的位置

Batch to get location for all the processes that are running

我想构建一个批处理来查看进程,对于它所在的每个进程 运行 获取位置并将其放入变量中,例如 例如:有进程 x1 运行 poof magic %var1% 有程序 x1 的位置 ... 但它应该为每个过程做.. 我所知道的是如何只为一个进程而不是所有进程做这件事,我必须知道进程的名称:

   wmic process where "name='aa.exe'" get ExecutablePath >dumpxkss.tmp
   more +1 dumpxkss.tmp >dumpxkss1.tmp
   for /f "delims=" %%x in (dumpxkss.tmp) do set Build=%%x
   set build1=%Build:~0,-8%

但在这种情况下,如果有更多 aa.exe 个进程,它将无法工作..

所以我想获取每个进程的路径,无论名称如何...... 请帮忙??

很明显:在您的 for 循环中,您只设置了一个变量 - 然后再次设置它并为文本文件的每一行再次设置它。所以你的 set build1 只能使用最后一个值,所有以前的值都被覆盖了。你只需要扩展你的 for 循环:

setlocal enabledelayedexpansion
wmic process where "name='aa.exe'" get ExecutablePath >dumpxkss.tmp
more +1 dumpxkss.tmp >dumpxkss1.tmp
for /f "delims=" %%x in (dumpxkss.tmp) do (
  set Build=%%x
  set build1=!Build:~0,-8!
  echo !build1!
)

另请参阅

您可以将代码缩短为:

setlocal enabledelayedexpansion
for /f "skip=1 tokens=*" %%x in ('wmic process where "name='aa.exe'" get ExecutablePath') do (
  set Build=%%x
  echo !Build:~0,-8!
)

试试这个批处理文件:

@echo off
set LogFile=Running_Process.txt
If Exist %LogFile% Del %LogFile%
WMIC /APPEND:%LogFile% PROCESS GET Caption,CommandLine>Nul
start "" %LogFile%