easy4mer
easy4mer

Reputation: 75

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.

Upvotes: 3

Views: 17894

Answers (2)

Naoufal EL JAOUHARI
Naoufal EL JAOUHARI

Reputation: 81

Try this:

Set WshShell = CreateObject("WScript.Shell")

WshShell.Run Target

Upvotes: 6

John Coleman
John Coleman

Reputation: 51978

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.

Upvotes: 2

Related Questions