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.
Related
How do I create a shortcut of a batch file and configure if to run in minimized mode? When I create a shortcut to a batch file I have to manually configure it to run in minimized mode manually. Any idea how do I write a script to change it to run as "minimized" mode
#npocmaka's shortcutjs.bat is a complete solution but it has about 200 lines. So, I have created a small VBScript for the purpose. You need to modify it according to your purpose.
'======PART 1: elivate to admin. required to save the batch file from part 2 in C drive
If Not WScript.Arguments.Named.Exists("elevate") Then
CreateObject("Shell.Application").ShellExecute WScript.FullName _
, WScript.ScriptFullName & " /elevate", "", "runas", 1
WScript.Quit
End If
'======PART 2: create the test batch file on the fly
Set objFSO=CreateObject("Scripting.FileSystemObject")
outFile = "c:\test.cmd"
Set objFile = objFSO.CreateTextFile(outFile,True)
objFile.Write "pause" & vbCrLf
objFile.Close
'=======PART 3: create the shortcut of the batch file
set WshShell = CreateObject("Wscript.shell")
strDesktop = WshShell.SpecialFolders("Desktop")
set oMyShortcut = WshShell.CreateShortcut(strDesktop + "\test.lnk")
oMyShortcut.WindowStyle = 7
OMyShortcut.TargetPath = "c:\test.cmd"
oMyShortCut.Save
Part 1 and 2 are optional and they are just to give an idea about what to do if you also want to create the batch file on the fly. Part 3 is the required code to create a shortcut using VBS.
You can run VBS script from cmd: cscript shortcut.vbs after you save the code above as shortcut.vbs
If you want to pass some argument about your batch file location, see this question, Can I pass an argument to a VBScript (vbs file launched with cscript)?
Then you can also use your code like cscript shortcut.vbs "C:\test.cmd" and reuse the same VBScript to create different shortcuts.
For other available options like adding an icon to your shortcut, adding hotkey support, setting Working Directory etc. please see this link
If I understand you correctly. You will need to use VB script to create shortcut. I don't believe batch script can create shortcut
https://support.microsoft.com/en-us/help/244677/how-to-create-a-desktop-shortcut-with-the-windows-script-host
see example 2: the WindowsStyle parameter define the windows size.
oMyShortCut.WindowStyle = 7 <-- 7= minimized.
Good luck
Binh
Try with shortcutjs.bat:
shortcutjs.bat -linkfile tst6.lnk -target "%cd%\myscript.bat" -windowstyle 7 -adminpermissions yes
-adminpermissions yes is optional if you want to run the bat as administrator. You'll need the full path to your script. possible modes are 1 for normal, 3 for maximized and 7 for minimized.
I have a very simple script, which only prints current directory. That's the code:
set WshShell = WScript.CreateObject("WScript.Shell")
Wscript.Echo (WshShell.CurrentDirectory)
This script is called from .exe file. It works fine until the calling executable was run directly. If I create a link to exe-file and launch it, then it runs my .vbs and it prints the directory of link, not the .exe itself! How can I fix this?
Get help from FileSystemObject, (vbscript example) :
scriptdir=CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName)
Wscript.Echo scriptdir
OK, maybe it's somehow clumsy, but I've discovered a workable solution. The idea is simple: get full script name and a short one. Then subtract the second from the first.
set WshShell = WScript.CreateObject("WScript.Shell")
Wscript.Echo (Left(WScript.ScriptFullName, Len(WScript.ScriptFullName) - Len(WScript.ScriptName)))
I am trying to build a VBScript to automatically run some .exe files. The problem is that the script and the .exe files are on a flashdrive, so it needs to find the current drive letter by itself. I can do it on a batch file using %~d0, but I like some of the functions of VBScript better, especially the ability to send keystrokes. Anyways, I found a whole list of VBScript commands, but I am no expert and I need help with the syntax. So far I have it set to open the task manager and press some keys to have it select the "performance tab" of the task manager:
Dim Act :Set Act = CreateObject("Wscript.Shell")
Act.Run("taskmgr.exe")
Success = Act.AppActivate("taskmgr")
Wscript.Sleep 250
Act.SendKeys "{TAB 5}" :WScript.Sleep 500
Act.SendKeys "{RIGHT 3}" :WScript.Sleep 500
I'd like to know what command I need to use to tell the script to use the drive letter where the script was executed from (USB drive).
Use the .ScriptFullName property to get the full file spec of the running script and apply .GetParentFolderName for the folder's path or .GetDriveName for just the drive letter.
>> Set oFS = CreateObject("Scripting.FileSystemObject")
>> s = WScript.ScriptFullName
>> WScript.Echo oFS.GetParentFolderName(s), oFS.GetDriveName(s)
>>
M:\bin M:
cf. here
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
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.