Windows批处理文件:最后一个进程的PID?

发布于 2024-10-11 11:09:00 字数 237 浏览 5 评论 0原文

我正在从批处理文件启动浏览器。

START "www.google.com"

我想知道启动的这个浏览器窗口的PID。

一台机器上可以启动多个浏览器窗口。我需要找到仅由我的批处理文件启动的进程的 PID。我尝试使用 WINDOWTITLE 过滤器。但这不是一个好主意,因为未来标题可能会发生变化。我正在使用 Windows XP/7

任何帮助将不胜感激。 谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

ゃ人海孤独症 2024-10-18 11:09:00

对于它的价值(问题已经超过 2 年了)这段代码可以解决问题,只需根据默认浏览器 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%

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%
嗳卜坏 2024-10-18 11:09:00

这只是一个想法,为了让你可能

有一个名为 Tasklist 的命令,

有一个名为 filter /FI 的开关,可以让你决定要输出哪些过滤器参数,例如 PID。将其输出到 a > 1.TXT

启动您的流程,

重新检查监视列表并输出到 2.TXT

然后您就必须发挥创意。比较1和2,
也许从 2.TXT 中删除 1 中的进程
剩下的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?

尽揽少女心 2024-10-18 11:09:00

如果您有一定的编程经验,则可以创建自己的控制台应用程序,该应用程序接受命令行参数并将它们传递给 Win32 API CreateProcess() 函数。其输出值之一是生成的进程 ID,您的应用程序随后可以返回该 ID。然后只需更新批处理文件即可调用您的应用,而不是直接使用 START

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.

我最亲爱的 2024-10-18 11:09:00

我正在尝试做同样的事情。虽然一定有某种方法可以做到这一点,但我所有的谷歌搜索都表明没有。

请访问 robvanderwoude.com 查看第三方工具和示例的列表。另请在此处查看 Sysinternal 进程实用程序的完整列表。

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.

娇纵 2024-10-18 11:09:00

我已经研究了大约 2 个小时,我认为有一种方法可以做到这一点,但它需要更多地了解 Windows 如何处理 PID 的 iexplore.exe...

我有一个批处理的工作版本我编写的文件将为您提供您想要的内容,但仅当它是第一个也是唯一一个打开的 Internet Explorer 窗口。

由于某种原因,当我打开新浏览器时,我无法更改 PID,但是如果没有打开窗口,我可以获得结果(显然是因为没有 PID)

无论如何,这就是我所拥有的......你应该是能够在您的系统上运行它,它会告诉您没有差异,如果您的默认浏览器是 Firefox 或 Chrome 或其他浏览器,它实际上可能会产生结果......只需对我提供的内容进行更改。

@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

让我知道这是否有帮助...

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文