I am creating a batch file to handle firefox tabs.
I want to close the last tab in firefox using windows command.
I have tried this command using title name
taskkill /IM firefox.exe /FI "WindowTitle eq localhost*"
It closes all the window
I have searched in windows command, there are no direct way.
Is there any possibilities ?
You will have to use VBScript code to send Ctrl+w keys to Firefox.
Save this code as closeActiveTab.vbs and run it with cscript closeActiveTab.vbs in your batch file or embed the code inside.
Dim Shell, WMI, query, process
Set Shell = CreateObject("WScript.Shell")
Set WMI = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\.\root\cimv2")
query = "SELECT ProcessId FROM Win32_Process WHERE Name = 'firefox.exe'"
For Each process In WMI.ExecQuery(query)
Shell.AppActivate process.ProcessId
WScript.Sleep 100
Shell.SendKeys "^w"
Next
Based on another answer.
Related
So, I'm trying to make a security system using vbs that would terminate a process.
Ex:
If you see "cmd.exe"
Terminate cmd.exe
I have coded everything except for the terminating part. I have searched many places but all they do is first run that file and then terminate it. I want to terminate the existing running "cmd.exe"
I tried running a batch file that terminates all "cmd.exe" processes but it just terminated itself leaving the other "cmd.exe" open.
Powershell is not an option too because it's one of the process I want to terminate.
Regards,
A Viper
Running TaskKill Command with VB Script
TaskKill command can terminate all existing processes whose running in same image name.
For example, if the process you want to be terminated is "cmd.exe", you can use TaskKill as below in your script:
Dim WshShell : Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "CMD /C TASKKILL /F /IM cmd.exe", 0, False
Above script is executed using Windows Script Host, which is not a process you want to be terminated, so this should work.
I'd like to use "sendkeys" to an rdp session, but I think I'd first need to put the focus on the RDP window on my desktop. How can I determine that dynamically?
If I use AppActivate, how can I determine the name of the RDP session?
AppActivate is the only window manipulation function available to script. As a general rule programs don't mess with other programs' windows.
However tasklist /v lists the main window title for a program.
This code gets it from CMD.
Set Inp = WScript.Stdin
Set Outp = Wscript.Stdout
Set cmd = CreateObject("Wscript.Shell").Exec("cmd")
cmd.stdin.writeline "tasklist /fi ""imagename eq mstsc.exe"" /v"
wscript.sleep 2000
cmd.stdin.writeline "exit"
Do While Not cmd.stdout.AtEndOfStream
results = cmd.stdout.readall
If err.number <> 0 then Exit Do
wscript.echo results
Loop
Also see my article here on how to access API from VBS using VB.NET.
https://social.msdn.microsoft.com/Forums/en-US/df0248cb-612f-4e2f-9665-11c68c401458/this-is-how-to-call-win32-api-calls-in-vbscript-and-jscript-sample-windows-api-functions?forum=scripting
and How to find the window Title of Active(foreground) window using Window Script Host
How can one open a batch window with some predefined text after the prompt and not invoking the command?
Say I want to invoke notepad.exe with a filename t.txt.
I would create a cmd file with this line:
start notepad "t.txt"
But I want the file to be opened specified by the user.
So the cmd file should just open a cmd window and "type" start notepad without actually executing this.
You can do it using a Vbscript:
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "cmd", 9 'opens cmd.exe
WScript.Sleep 500 'gives cmd a time to load
WshShell.SendKeys "start notepad"
If you want this within your cmd file, try this:
#if (#X)==(#Y) #end /*
start cmd.exe
cscript //E:JScript //nologo "%~f0"
exit/b
*/
var obj = new ActiveXObject("WScript.Shell");
obj.SendKeys("start notepad");
Normally you'd have to launch a command prompt window: start cmd rather than starting notepad directly. But neither start nor cmd offer the function you desire. Would adding a simple echo to your batch file with instructions and a pause prior to actually issuing the command work?
Echo When the other operation completes return to this window
Pause
start notepad "t.txt"
This will provide the echo, then wait for the user to press any key before actually attempting to launch notepad with that filename.
I've searched this and some pages came which weren't really useful or were too complicated (I am not a skilled batch file programmer!)! What I need is to run a batch file in hidden form (no console window). The batch file will not be called from external application or code. It will be clicked on by the client and then I want no console pages to be shown (only pages which are called by call command should be shown)! The batch file is exactly as follows:
#echo off
call setup.exe
IF EXIST "C:/caillog" goto tracking
IF NOT EXIST "C:/caillog" goto end
:tracking
call dotnet4.exe
call ClientService.msi
goto end
:end
I use VBScripts to open it hidden, like this:
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run("%batchfile%"), 0, True
for e.g the bat file I want to run is run.bat then I'll do like this
objShell.Run("run.bat"), 0, True
Instead of running the batch file run the vb file.
Write it in notepad and save it as *.vbs
If your Windows system supports powershell you can place this infront of "#echo off":
cmd /c powershell -Nop -NonI -Nologo -WindowStyle Hidden "Write-Host"
As others have said, use VBS.
Set WinScriptHost = CreateObject("WScript.Shell")
WinScriptHost.Run Chr(34) & "C:\FilePath" & Chr(34), 0
Set WinScriptHost = Nothing
This is what I use.
How can I terminate process using vbscript. PLEASE NOTE, I need to terminate process that runs under windows 64-bit environment as native 64 (not using select * from win_32_Process)
Thanks,
The Win32_Process class provides access to both 32-bit and 64-bit processes when the script is run from a 64-bit command shell.
If this is not an option for you, you can try using the taskkill command:
Dim oShell : Set oShell = CreateObject("WScript.Shell")
' Launch notepad '
oShell.Run "notepad"
WScript.Sleep 3000
' Kill notepad '
oShell.Run "taskkill /im notepad.exe", , True
Just type in the following command: taskkill /f /im (program name)
To find out the im of your program open task manager and look at the process while your program is running. After the program has run a process will disappear from the task manager; that is your program.
I know I am late to the game LOL, but this is what I did to kill SmartScreen.
Create text file KillSmartScreen.vbs
Edit with Notepad:
Dim oShell : Set oShell = CreateObject("WScript.Shell")
'Kill smartscreen.exe aka Windows Defender SmartScreen
oShell.Run "taskkill /f /im smartscreen.exe"
Double click KillSmartScreen.vbs to run.
Works like a champ.