How to gently close Chrome using CMD or VBS?

天涯浪子 提交于 2020-06-25 06:03:53

问题


I'm having a little problem about closing gently Chrome in order to automatically clean cache.

I have a website that is not refreshing correctly, but it does if I clean the cache.

For now I have a .bat file with the following code:

taskkill /F /IM chrome.exe /T > nul
del /q "C:\Users\Francisco\AppData\Local\Google\Chrome\User Data\Default\Cache\*"
FOR /D %%p IN ("C:\Users\Francisco\AppData\Local\Google\Chrome\User Data\Default\Cache\*.*") DO rmdir "%%p" /s /q
timeout 8
start chrome --restore-last-session --start-fullscreen

Now, I know that taskkill /F forces the process to close and that works but as soon as Chrome opens, it shows a message that Chrome wasn't closed correctly and asks me if I want to restore the last session.

If i remove the /f option, Chrome doesn't close:

ERROR: the process with PID 8580 (PID secondary process 8896) Could not finish.
Reason: This process can be terminated only forcibly (with the / F option).

So... I need one of two options;

  1. Is there a way to gently close Chrome like pressing Alt+F4 using CMD or a VBS?2. Is there a way to hide that Chrome restore last session message?

I'd prefer the first option since it's cleaner.


回答1:


you can use powershell to close all windows with chrome as process name. It won't kill the task but gently close all chrome browser windows, like if you clicked on top-right cross. Here is the command line to put in batch :

powershell -command "Get-Process chrome | ForEach-Object { $_.CloseMainWindow() | Out-Null}"



回答2:


Refer to the answer of @tuxy42 : You can write with vbscript to open chrome browser like this : Open_Chrome_Browser.vbs

Set ws = CreateObject("Wscript.Shell")
siteA = "https://stackoverflow.com/questions/62516355/google-chrome-always-says-google-chrome-was-not-shut-down-properly"
ws.Run "chrome -url " & siteA

And if you want to close it safely : safely_close_chrome_browser.vbs

Set ws = CreateObject("Wscript.Shell")
psCommand = "Cmd /C powershell -command ""Get-Process chrome | ForEach-Object { $_.CloseMainWindow() | Out-Null}"""
ws.run psCommand,0,True


来源:https://stackoverflow.com/questions/43044776/how-to-gently-close-chrome-using-cmd-or-vbs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!