How Can I Start Windows Explorer Opened to a Specific Folder?

How Can I Start Windows Explorer Opened to a Specific Folder?

  • Comments 8
  • Likes
Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I start Windows Explorer opened to a specific folder?

-- CD

SpacerHey, Scripting Guy! AnswerScript Center

Hey, CD. We’re going to let you in on one of the Scripting Guys’ most sordid secrets: sometimes the Scripting Guys don’t agree with each other. No, it’s true; really. For example, some of the Scripting Guys think that soccer is a real sport; even worse, at least one of the Scripting Guys thinks that stock car racing is a real sport! Oh, brother.

Note. Following a brief … get-together … the Scripting Guy who writes this column has agreed that he was wrong and that both soccer and stock car racing are real sports after all. Sort of.

Here’s another area in which the Scripting Guys disagree. At least one of the Scripting Guys believes that system administrators and IT professionals would never stoop to using the Windows graphical user interface (GUI). And he’s not the only Microsoft-type who feels that way; we know of at least one Program Manager who, whenever he wants to start Word, opens a command window and types winword.exe at the command prompt. Other Scripting Guys would argue that system administrators don’t care whether they use the command line or a GUI tool: they just want to get the job done as quickly and as easily as possible.

So is one side right and one side wrong? Of course! However, just as we … agreed … that soccer and stock car racing were sports, we’ve also agreed that the command line and the GUI are equally good ways of getting Windows Explorer to open up with the focus on a specific folder. Therefore, we’re going to give you two different scripts to choose from, one command-line oriented, one GUI-oriented.

Let’s start with the command-line script first. To get this script (which we’ll call my-script.vbs) to work all you need to do is call the script from the command prompt, passing the name of the folder you want opened as the lone argument. In other words, to open Windows Explorer focused on C:\Scripts you’d type the following from the command prompt:

my-script.vbs "c:\scripts"

Are the double quotes required around the folder path? In this case, no. However, if there are any blank spaces in the path then the double quotes are required. This command string won’t work:

my-script.vbs c:\documents and settings\kmyer

Any time you pass a script an argument that includes a blank space the entire argument must be enclosed in double quotes (or else). In other words:

my-script.vbs "c:\documents and settings\kmyer"

That’s just the way the command shell works.

So what script are we running here? Good question. Turns out it’s this one:

Set objShell = CreateObject("Wscript.Shell")

strPath = Wscript.Arguments(0)

strPath = "explorer.exe /e," & strPath
objShell.Run strPath

As usual, there’s really not that much to this. We start out by creating an instance of the Wscript.Shell object; this is the Windows Script Host object we can use to run scripts or executable files from within another script. We then take the first argument supplied to the script (c:\scripts) and stash that in a variable named strPath:

strPath = Wscript.Arguments(0)

So far so good, huh? In a moment, we’re going to use the Run method to start Windows Explorer. Before we do that, however, we should note that the Run method in WSH is pretty much the same thing as the Run dialog box. If you wanted to use the Run dialog box to start Windows Explorer (with the focus on the C:\Scripts folder) you’d type in this:

explorer.exe /e,c:\scripts

As it turns out, that’s the same syntax we use to start Windows Explorer using the Run method. All we have to do is construct that command and then execute it:

strPath = "explorer.exe /e," & strPath
objShell.Run strPath

In line 1 we’re taking the command explorer.exe /e, and tacking on the folder path (which is stored in the variable strPath); strPath will then take on the value explorer.exe /e,c:\scripts. In line 2 we call the Run method, passing the variable strPath as the command we want to run. If all goes well (and in scripting things always go well, right?), Windows Explorer will open up with the focus on C:\Scripts:

Hey, Scripting Guy!


Cool.

So what’s wrong with that? Nothing; it works just fine. The only disadvantage to this script is the fact that it requires you to type in the full path to the folder; that can be a problem when you’re trying to open the folder C:\Documents and Settings\Default User\Application Data\Microsoft\SystemCertificates\My\Certificates. But as system administrators wedded to the command line, what choice to we have?

Well, you could always try the GUI approach (don’t worry; we won’t tell anyone):

Const WINDOW_HANDLE = 0
Const NO_OPTIONS = 0

Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.BrowseForFolder _
    (WINDOW_HANDLE, "Select a folder:", NO_OPTIONS)       

Set objFolderItem = objFolder.Self
strPath = objFolderItem.Path

objShell.Explore strPath

This script doesn’t require you to do any typing whatsoever. Instead, you simply start the script and it shows you the Browse For Folder dialog box:

Hey, Scripting Guy!


Select a folder, click OK, and you’re on your way.

So how does this script work? Well, we start out by defining a pair of constants: WINDOW_HANDLE and NO_OPTIONS. WINDOW_HANDLE is a constant required by the BrowseForFolder method; NO_OPTIONS simply informs the script that we want to display a standard Browse For Folder dialog box. After that we create an instance of the Shell.Application object and then use this line of code to display the dialog box:

Set objFolder = objShell.BrowseForFolder _
    (WINDOW_HANDLE, "Select a folder:", NO_OPTIONS)

After the dialog box is displayed it will simply wait until we select a folder and then click OK. When that happens we use these two lines of code to: 1) create an object reference to the selected folder; and, 2) store the folder Path in the variable strPath:

Set objFolderItem = objFolder.Self
strPath = objFolderItem.Path

All we have to do now is call the Explore method, which exists solely to open Windows Explorer. By passing the variable strPath as the lone parameter that will cause Windows Explorer to open up with the focus on C:\Scripts:

objShell.Explore strPath

The moral of the story? Now you can use the command prompt to open up Windows Explorer or you can use the GUI to open Windows Explorer; it’s entirely up to you. And if you want to watch soccer or stock car racing the next time you feel like watching a sporting event, well, so be it. (Of course, you won’t be watching a real sporting event, but ….)

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • So how do you have the "BrowseForFolder" come up with a specific initial folder?

  • hi,

    if i cancel, i have a error ?

    How can i solve this, i'm a newbie about wsh vbs

  • Great Article!  Did exactly what I needed to do!

  • I've just problems to start with a fprpc folder.

    I'll keep digging....

  • Good work. Thanks for the info

  • thanks!

    can you tell me how to have the folder sort by date modified when opened with this command?

  • Thanks, that was really detailed and helpful!

  • Just where do i enter these lines at?

    Well, you could always try the GUI appro

    ach (don’t worry; we won’t tell anyone):

    Const WINDOW_HANDLE = 0

    Const NO_OPTIONS = 0

    Set objShell = CreateObject("Shell.Application")

    Set objFolder = objShell.BrowseForFolder _

       (WINDOW_HANDLE, "Select a folder:", NO_OPTIONS)      

    Set objFolderItem = objFolder.Self

    strPath = objFolderItem.Path

    objShell.Explore strPath