开发者

Windows batch file : PID of last process?

开发者 https://www.devze.com 2023-02-04 23:10 出处:网络
I am launching a browser from batch file. START \"www.google.com\" I would like to know the PID of this browser window launched.

I am launching a browser from batch file.

START "www.google.com"

I would like to know the PID of this browser window launched.

There can be many browser windows launched on a single machine. I need to find the PID of the process which was launched by my batch file only. I tried with WINDOWTITLE filter. But开发者_开发知识库 its not a good idea as titles may change in future. I am using Windows XP/7

Any help would be appreciated. Thanks.


For what it worth (question is more than 2 years old) this code do the trick, just change variable according to default browser exe

set "browser=palemoon.exe"

tasklist /FI "imagename eq %browser%" /NH /FO csv > task-before.txt
start www.google.com
tasklist /FI "imagename eq %browser%" /NH /FO csv > task-after.txt

:: fc /L /LB1  test4-Before.txt test4-After.txt | find /I "%browser%"
for /f "delims=, tokens=2,*" %%A in ('"fc /L /LB1  task-before.txt task-after.txt | find /I "%browser%""') do set pid=%%A

SETLOCAL enabledelayedexpansion
pid=!pid:"=!
ENDLOCAL
echo pid is %pid%


This is just an idea, to get you maybe on the way

there is a command called Tasklist

there is a switch called filter /FI with lets you decide what filter parameters you want to output, f.e PID. Output this to a > 1.TXT

start your proces

recheck the watchlist and output to 2.TXT

Then you would have to get creative. COmpare 1 to 2, maybe remove the processes in 1 from the 2.TXT The remainig PID is what you wanted?


If you have some programming experience, you could create your own console application that accepts command-line parameters and passes them to the Win32 API CreateProcess() function. One of its output values is the spawned process ID, which your app could then return. Then just update your batch file to call your app instead of using START directly.


I'm trying to do the same thing. Though there must be some way of doing it, but all my Googling suggests not.

Check out robvanderwoude.com to see a list of 3rd party tools and examples. Also check out the full list of Sysinternal's process utilities here.


I've been looking at this for about 2 hours now and I think that there is a way to do this, but it requires some more insight on how windows handles iexplore.exe for PID...

I have a working version of a batch file I wrote that will get you what you want, BUT only if its the FIRST AND ONLY Internet Explorer Window open.

For some reason I can't get the PID to change when I open new browsers, but I can get results if there is no window open (obviously because there is no PID)

Anyhow, this is what I have... you should be able to run this on your system and it will tell you that there are no differences and it might actually produce results if your default browser is Firefox or Chrome or something... just need to make the changes to what I'm providing.

@echo off
IF EXIST c:\temp\pshell.txt del c:\temp\pshell.txt
IF EXIST C:\temp\PID1.txt del C:\temp\PID1.txt
IF EXIST C:\temp\PID2.txt del C:\temp\PID2.txt
IF EXIST C:\temp\PowerFormat.txt del C:\temp\PowerFormat.txt
powershell.exe Get-Process iexplore>C:\temp\pshell.txt
FOR /F "skip=3 tokens=7 delims= " %%1 IN ( c:\temp\pshell.txt ) DO @echo %%1>> C:\temp\PID1.txt
start "title" "www.google.com"
powershell.exe Get-Process iexplore>C:\temp\pshell.txt
FOR /F "skip=3 tokens=7 delims= " %%2 IN ( c:\temp\pshell.txt ) DO @echo %%2>> C:\temp\PID2.txt

FC /L c:\temp\pid1.txt c:\temp\pid2.txt> C:\temp\FileComparison.txt
FOR /F "tokens=7 delims=" %%3 IN (c:\temp\FileComparison.txt) DO @echo %%3>C:\temp\DiffPID.txt

FINDSTR "FC: no differences encountered" c:\temp\FileComparison.txt
IF '%ERRORLEVEL%'=='0' del C:\temp\FileComparison.txt & echo.No PID Found
IF NOT '%ERRORLEVEL%'=='0'  type c:\temp\FileComparison.txt

pause
exit

Let me know if this helps...

0

精彩评论

暂无评论...
验证码 换一张
取 消