Kill explorer.exe with windows title - vbscript

I'm new with programing and my question is now, how i can close some specific explorer.exe windows. My Problem is, i have a program that call some windows:
Option Explicit
Dim shell, expl1, expl2, expl3, Terminate
Dim uprgExplorer
set shell = WScript.CreateObject("WScript.Shell")
set expl1 = shell.exec("C:\WINDOWS\explorer.exe c:\Documents and Settings")
set expl2 = shell.exec("C:\WINDOWS\explorer.exe C:\WINDOWS\system32\CCM\Cache")
set expl3 = shell.exec("C:\WINDOWS\explorer.exe c:\SCRIPTS\LOG")
Now i will kill only this 3 windows NOT the explorer.exe.
Can some one help me?
Greetings,
matthias

You could use the SendKeys function to close the Explorer windows:
set shell = WScript.CreateObject("WScript.Shell")
set expl1 = shell.exec("C:\WINDOWS\explorer.exe c:\tmp")
MsgBox "Explorer started."
success = shell.appactivate("c:\tmp")
if success then shell.sendkeys "%{F4}"
You might also want to have a look at AutoHotkey, which allows you to record macros and manipulate windows.

Related

Can I pause execution of an application that was started by a VBscript?

I launch the Unified Functional Testing application and tell it to run a script:
Dim uftApp
Dim WshShell
Set uftApp = CreateObject("QuickTest.Application")
If NOT uftApp.Launched Then
uftApp.Launch
End if
uftApp.Visible = True
uftApp.WindowState = "Minimized"
uftApp.Open "C:\Users\smithjohn\Desktop\UFT Repository\Project 1\MyUFTScript", False
uftApp.Test.Environment.Value("ForTeam") = "TEAM A"
uftApp.Test.Run
uftApp.Test.Close
uftApp.Quit
Set uftApp = Nothing
This launches UFT and begins to run MyUFTScript. Since this script be can be quite long and take hours to run I want to be able to pause the execution. I've written another small vbscript file that looks like this:
Dim qtApp
Set qtApp = CreateObject("QuickTest.Application")
qtApp.Test.Pause
Set qtApp = Nothing
I then create a shortcut to that vbs file and give it a shortcut key, "cntl + alt + p". When I lauch UFT myself my pause script works perfectly, but when the top most vbs file is used to launch and run UFT my pause.vbs file wont run at all - I am not able to pause execution.
Am I doing something wrong?
Use GetObject instead of CreateObject. this should work for you.
Dim qtApp
Set qtApp = GetObject("","QuickTest.Application")
qtApp.Test.Pause
Set qtApp = Nothing

vbscript open folder in same explorer window

I am not so good in VBScript at all, but thanks to Google I was able to put together script which is able to open file path in explorer.exe
I would like to open the specific path in same window not in the new one. Is VBScript able to do it?
Here is my code:
Dim SH, FolderToOpen
Set SH = WScript.CreateObject("WScript.Shell")
FolderToOpen = "C:\path\to\my\folder"
SH.Run FolderToOpen
Set SH = Nothing
Thank you for your advice.
Try this:
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run Target
Here is a hackish approach using SendKeys that will work if the open instance of explorer.exe has the focus:
Set WshShell = WScript.CreateObject("WScript.Shell")
target = "C:/programs"
WshShell.SendKeys "%d"
WshShell.SendKeys target
WshShell.SendKeys "{ENTER}"
This will work if you e.g. have the above code (with the intended target) in a script in one folder. Click on the script icon and it will send you to the target folder.
[On Edit] An explanation of how it works: If you are using Windows Explorer and type Alt+d (which is what SendKeys "%d" simulates) then the focus is shifted to the address bar. For years I have been using this trick to open a command prompt in the current folder (Alt - d then type cmd then press Enter and the prompt opens with the open folder as the working directory). When I saw this question I wondered if essentially the same trick (but automated with VBScript) would work for navigation purposes and was pleasantly surprised when it worked as intended the very first time. Alt-d is a useful keyboard shortcut to keep in mind.

Executable running in background

I am trying to run a simple batch script via Jenkins (which in turn calls a VBscript). The script which i am executing in Jenkins is:
cd "C:\Product\workspace"
cscript Test.vbs
The test.vbs is simple code which calls an exe in console mode
Dim objShell
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "cmd /K C:\Product\workspace\Product.exe -c -dir C:\ProductDir", 1
Set objShell = Nothing
The parameter 1 : Activates and displays a window. If the window is minimized or maximized, the system restores it to its original size and position.
The problem which I am facing is I am not able to see the cmd.exe and the Product.exe installer. Though the process explorer shows cmd.exe and Product.exe running. I don't get why I its not running in foreground and only in background.
How can I get the exe to run in foreground?
When I tried running directly on VM, I can see it running in foreground. Cant understand this situation. Any light on this?
Thanks.
Are you running Jenkins slave agent as headless service on windows? I remember in this case the GUI would have problem. You should run the agent with jnlp when you add the slave VM. This works perfect with me.
Here's another way to skin the cat using VBScript.
I experienced the same issue trying schedule a task to launch Internet Explorer into the foreground. I was using WScript's Run method with the 3 window option to force it to be maximized. I just couldn't force it to come up in the foreground.
I FINALLY got that to work with WScript's AppActivate method. The trick was to monitor AppActivate's return value in a loop to ensure the application is fully launched with the correct TITLE before using AppActivate to bring it to the foreground.
AppActivate Method
Here's my example script:
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "iexplore.exe https://www.google.com", 3, false
WScript.Sleep 2000
While WshShell.AppActivate("Internet Explorer") = FALSE
WScript.Sleep 1000
Wend
WshShell.AppActivate "Internet Explorer"
WScript.Quit
=========================
Note: AppActivate will choose the closest match for the application TITLE (or process ID, which is not as simple). You don't have to have the complete TITLE. I'm showing "Internet Explorer" here, but I was able to use the TITLE of the web site that I was redirecting to ("Google" would work OK in this example). So, if you don't want to pull up any random instance of an application you may already have open, be as specific as possible. A CMD.EXE TITLE wouldn't be your best bet.
AppActivate works especially well for CMD/COMMAND windows, as (mentioned previously) you can use the TITLE batch file command to specify a unique window title.
I think im late but I did this and it worked:
Dim objShell
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "cmd /K C:\Product\workspace\Product.exe -c -dir C:\ProductDir", 0, False Rem 0: run in background, False: exit without waiting process to stop, True to wait for process
Set objShell = Nothing
You can use the .visible property in order to display the applications that are running and bring them to the foreground e.g. objShell.Visible = True
Example below of how I used it when launching an application:
Dim objQtpApp
Set objQtpApp = CreateObject("QuickTest.Application")
'make QTP visible
objQtpApp.Visible = True

.vbs script won't run batch / how to run batch silently

I have a batch file that I want to run silently.
So, i was goolging around and come up with following code...
invMybatchfile.vbs
Dim curPath
curPath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")
Set WshShell = CreateObject("WScript.Shell")
cmds=WshShell.RUN(sCurPath & "\Mybatchfile.bat", 0, True)
This code DOES work on my desktop (Window 7 32bit), but for some reason, this script does not work on server computer(Windows Server 2008 R2). When I click invMybatchfile.vbs, dos windows pops up and closes right away and Mybatchfile.bat is not executed at all. (If i just run Mybathfile.bat on server computer, it works fine, so batch file is not the problem here.)
So my question is, does anyone know why I am having this problem?
OR
is there any other ways to launch batch file silently(not minimized, i know this way) with out using .vbs file or installing other software?
Thanks for your help
JB
The code of your example never will run in any machine because you are declaring the variable name as "curPath" and assign the vlaue to "CurPath" but you are trying to use it with the name "sCurPath" (And that variable doesn't exist on your code).
Also you don't need to set the current working dir because when you launch a file, the shell searchs for the file in the working dir, so this is all the code what you need, in only one line:
CreateObject("WScript.Shell").RUN "Mybatchfile.bat", 0, True
But if you are interested to store or to use the current working directory for any strange reason you have a method that returns the current dir:
.CurrentDirectory
Then you can use the method this way:
Set Shell = CreateObject("WScript.Shell")
Shell.RUN Shell.CurrentDirectory & "\Mybatchfile.bat", 0, True
...Or store the current dir in a var this way:
Set Shell = CreateObject("WScript.Shell")
CurDir = Shell.CurrentDirectory & "\"
Shell.RUN CurDir & "Mybatchfile.bat", 0, True
MSDN: http://msdn.microsoft.com/en-us/library/3cc5edzd%28v=vs.84%29.aspx
EDIT:
About running silent files, you can do it too using NirCMD commandline application.
NirCMD exec hide "Path to Batch File\Batch File.bat"
Official site: http://www.nirsoft.net/utils/nircmd2.html
I think you need to run it through cmd.exe:
WshShell.Run("%COMSPEC% /c " & CurPath & "\Mybatchfile.bat", 0, True)
Check similar questions here and here for more information.
Have you tried using RunAs?
cmds=WshShell.RUN("runas /noprofile /user:mymachine\administrator " _
& sCurPath & "\Mybatchfile.bat", 0, True)
Since it works on Windows 7 but not on Server 2008 R2, It sounds like a permissions issue to me.

Can I compell MSBuild.exe to finish in VBS (64-bit Windows 7)?

ong story with code samples:
I'm doing automated test builds using vb scripting and I see the following line execute differently in 32-bit and 64-bit Windows 7:
CmdLine ("cmd /c C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe C:\Users\tester_kafka\Documents\VisualStudio2012\Projects\CFX_Manager_UITest\CFX_Manager_UITest.sln")
where CmdLine is a function like this:
Function CmdLine(strCommand)
Set WshShell = CreateObject("WScript.Shell")
Set WshShellExec = WshShell.Exec(strCommand)
Do While WshShellExec.Status = 0
WScript.Sleep 100
Loop
End Function
In the 32-bit Windows 7 system, the build completes and the vb script proceeds to completion. In the 64-bit Windows 7 system, the cmd window remains open until closed manually, then the script proceeds to completion. The goal is, of course, to automate the script in 64-bit to proceed automatically.
I have tried adding the & exit to the line, and the /nr:false switch to MSBuild.exe and messing with the quotes (not sure what else to try, frankly...). Should I be doing something simpler? And why should the OS make that much difference?
Any information would be welcome, even if it's not a solution. Thank you all.
Try this:
Function CmdLine(strCommand)
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run strCommand, , true
End Function

Resources