1

i would like to build a batch that will look over the processes, for every process that it is running to get the location and put it in a variable like ex : there is process x1 running poof magic %var1% has the location of program x1 ... But it should do it for every process.. what i know is how to do it just for one process not all of them and i have to know the name of the process :

   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%

but in this case if there are more aa.exe processes it won t work..

so i want to get the path for every process no matter the name ... please help ??

wkup
  • 79
  • 1
  • 1
  • 8

2 Answers2

0

quite obvious: in your for loop you only set a variable - and set it again and set it again for every line of the textfile. So your set build1 can only work with the last value, all the previous values got overwritten. You just have to expand your for loop:

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!
)

Also read about delayed expansion

You can shorten your code to:

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!
)
Community
  • 1
  • 1
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • nononono, you see that didn't solve the problem .. i want this command to run for every existing process not only for aa.exe – wkup Jul 17 '16 at 18:02
  • then just delete `where "name='aa.exe'"` (although I'm not sure, what `%Build:~0,-8%` is supposed to mean...) – Stephan Jul 17 '16 at 20:27
  • yea , if i remove where "name='aa.exe'" and run it as administrator it work's just fine. thanks a lot !!! – wkup Jul 18 '16 at 07:11
0

Give a try for this batch file :

@echo off
set LogFile=Running_Process.txt
If Exist %LogFile% Del %LogFile%
WMIC /APPEND:%LogFile% PROCESS GET Caption,CommandLine>Nul
start "" %LogFile%
Hackoo
  • 18,337
  • 3
  • 40
  • 70
  • well thanks for the work but i don't wanna log them .. i want to get every location of every running process in a variable – wkup Jul 18 '16 at 07:07