3

I have a batch file that runs to open a webpage in Chrome that runs a specific function.

start http://www.example.com/cgi/myprogram.exe

This process runs quickly, then I want to automatically close the browser window. I don't want to use taskkill /IM chrome.exe because Chrome has many services running under "chrome.exe" and I only want to kill the one that shows on the applications tag of task manager, not the processes tag.

Is that possible?

Hackoo
  • 18,337
  • 3
  • 40
  • 70
Shawn
  • 3,031
  • 4
  • 26
  • 53

1 Answers1

1

To just kill the new tab, you can use this:

@echo off
setlocal EnableDelayedExpansion
set "newPIDlist="
set "oldPIDlist=p"
::find old PIDs
for /f "TOKENS=1" %%a in ('wmic PROCESS where "Name='chrome.exe'" get ProcessID ^| findstr [0-9]') do (set "oldPIDlist=!oldPIDlist!%%ap")
::start your site here
start http://www.example.com/cgi/myprogram.exe
::find new PIDs
for /f "TOKENS=1" %%a in ('wmic PROCESS where "Name='chrome.exe'" get ProcessID ^| findstr [0-9]') do (
if "!oldPIDlist:p%%ap=zz!"=="%oldPIDlist%" (set "newPIDlist=/PID %%a !newPIDlist!")
)
echo %newPIDlist%
::wait for page to load
timeout /t 5 /nobreak >nul
taskkill /f %newPIDlist% /T > NUL 2>&1

However, note that this won't close the tab, it will just kill the process of the tab, causing an error message to pop up. As discussed here, closing a single google chrome tab from the command line is not possible.

Community
  • 1
  • 1
Dennis van Gils
  • 3,487
  • 2
  • 14
  • 35
  • I think there may be more than one tab per process as well sometimes, so this may have unexpected consequences. – Joey Apr 25 '16 at 19:54
  • You're right, however this is as far as I know the only reason to get the right one, as for instance `wmic process call create` doesn't give back the PID from Google Chrome. – Dennis van Gils Apr 25 '16 at 20:09